diff options
| author | David Pursehouse <david.pursehouse@sonyericsson.com> | 2011-10-26 18:19:03 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-07-24 14:44:05 +0900 |
| commit | 0e343379c4e3be9b4f041181602c63e7361c7764 (patch) | |
| tree | 8d24d66e3dceceba8307db5dd3ad580af1722123 /tests | |
| parent | f53af8cc06df79e4065a5f5d88d7d51a636711b7 (diff) | |
| download | pygerrit-0e343379c4e3be9b4f041181602c63e7361c7764.tar.gz | |
Minor refactoring and adding initial unit tests
Added constants for the event types and replaced debug prints with
exceptions.
Added initial unit tests covering the attach and detach methods.
Tidied up the docstrings a bit.
Diffstat (limited to 'tests')
| -rwxr-xr-x | tests/gerrit_stream/command-unittest | 3 | ||||
| -rwxr-xr-x | tests/gerrit_stream/unittest_gerrit_stream/README | 1 | ||||
| -rwxr-xr-x | tests/gerrit_stream/unittest_gerrit_stream/command | 3 | ||||
| -rwxr-xr-x | tests/gerrit_stream/unittest_gerrit_stream/status | 1 | ||||
| -rwxr-xr-x | tests/gerrit_stream/unittest_gerrit_stream/unittests.py | 117 |
5 files changed, 125 insertions, 0 deletions
diff --git a/tests/gerrit_stream/command-unittest b/tests/gerrit_stream/command-unittest new file mode 100755 index 0000000..d188609 --- /dev/null +++ b/tests/gerrit_stream/command-unittest @@ -0,0 +1,3 @@ +export PYTHONPATH=$PYTHONPATH:`pwd` + +$TESTDIR/unittests.py diff --git a/tests/gerrit_stream/unittest_gerrit_stream/README b/tests/gerrit_stream/unittest_gerrit_stream/README new file mode 100755 index 0000000..a46d206 --- /dev/null +++ b/tests/gerrit_stream/unittest_gerrit_stream/README @@ -0,0 +1 @@ +Runs the unit tests for the gerrit_stream module. diff --git a/tests/gerrit_stream/unittest_gerrit_stream/command b/tests/gerrit_stream/unittest_gerrit_stream/command new file mode 100755 index 0000000..d188609 --- /dev/null +++ b/tests/gerrit_stream/unittest_gerrit_stream/command @@ -0,0 +1,3 @@ +export PYTHONPATH=$PYTHONPATH:`pwd` + +$TESTDIR/unittests.py diff --git a/tests/gerrit_stream/unittest_gerrit_stream/status b/tests/gerrit_stream/unittest_gerrit_stream/status new file mode 100755 index 0000000..573541a --- /dev/null +++ b/tests/gerrit_stream/unittest_gerrit_stream/status @@ -0,0 +1 @@ +0 diff --git a/tests/gerrit_stream/unittest_gerrit_stream/unittests.py b/tests/gerrit_stream/unittest_gerrit_stream/unittests.py new file mode 100755 index 0000000..c84c932 --- /dev/null +++ b/tests/gerrit_stream/unittest_gerrit_stream/unittests.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + +from gerrit_stream import GerritStream, GerritStreamError + + +class ListenerWithNoHandler(): + """ Dummy listener class with no event handler. + """ + def __init__(self): + pass + + +class ListenerWithInvalidHandler(): + """ Dummy listener class with invalid event handler. + """ + def __init__(self): + pass + + def on_gerrit_event(self): + pass + + +class ListenerWithValidHandler(): + """ Dummy listener class. + """ + def __init__(self): + pass + + def on_gerrit_event(self, event): + pass + + +class TestGerritStream(unittest.TestCase): + """ Test that the GerritStream class behaves correctly. + """ + + def test_listener_no_handler(self): + """ Test that an exception is raised if a listener is + attached without an event handler method. + """ + g = GerritStream() + l = ListenerWithNoHandler() + self.assertRaises(GerritStreamError, g.attach, l) + + def test_listener_invalid_handler(self): + """ Test that an exception is raised if a listener is + attached with an invalid event handler method. + """ + g = GerritStream() + l = ListenerWithInvalidHandler() + self.assertRaises(GerritStreamError, g.attach, l) + + def test_listener_valid_handler(self): + """ Test that a valid listener can be added. + """ + g = GerritStream() + l = ListenerWithValidHandler() + self.assertEquals(len(g.listeners), 0) + g.attach(l) + self.assertEquals(len(g.listeners), 1) + self.assertEquals(g.listeners[0], l) + + def test_add_same_listener_multiple_times(self): + """ Test that the same listener will only be added once. + """ + g = GerritStream() + l = ListenerWithValidHandler() + self.assertEquals(len(g.listeners), 0) + g.attach(l) + self.assertEquals(len(g.listeners), 1) + self.assertEquals(g.listeners[0], l) + g.attach(l) + self.assertEquals(len(g.listeners), 1) + self.assertEquals(g.listeners[0], l) + + def test_add_multiple_listeners(self): + """ Test that multiple listeners can be added. + """ + g = GerritStream() + l = ListenerWithValidHandler() + self.assertEquals(len(g.listeners), 0) + g.attach(l) + self.assertEquals(len(g.listeners), 1) + self.assertEquals(g.listeners[0], l) + l2 = ListenerWithValidHandler() + g.attach(l2) + self.assertEquals(len(g.listeners), 2) + self.assertEquals(g.listeners[0], l) + self.assertEquals(g.listeners[1], l2) + + def test_detach_listener(self): + """ Test that a listener can be detached. + """ + g = GerritStream() + l = ListenerWithValidHandler() + self.assertEquals(len(g.listeners), 0) + g.attach(l) + self.assertEquals(len(g.listeners), 1) + self.assertEquals(g.listeners[0], l) + g.detach(l) + self.assertEquals(len(g.listeners), 0) + + def test_detach_not_attached_listener(self): + """ Test that the class behaves correctly if a not-attached + listener is detached. + """ + g = GerritStream() + l = ListenerWithValidHandler() + self.assertEquals(len(g.listeners), 0) + g.detach(l) + self.assertEquals(len(g.listeners), 0) + +if __name__ == '__main__': + unittest.main() |
