diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-09-11 10:37:48 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2013-09-11 16:54:59 +0900 |
commit | b9aaf8e51a074724347d3cfb69b351918bae4b98 (patch) | |
tree | a42fc15518000dc876da8d3e1e443608a4e0def9 /unittests.py | |
parent | b1adb9b986f6f1b1c27cb103efbe7fa6e62ac5ef (diff) | |
download | pygerrit-b9aaf8e51a074724347d3cfb69b351918bae4b98.tar.gz |
Fix #5: Move json parsing and error handling into the event factory
Pass the raw string into the event factory and parse it to a json
object there. If the string is not valid json, generate an error
event.
Add a unit test for handling of invalid json on the event stream.
Change-Id: I209a89fd28c3a594b71443fc106e25d58c5cc1ea
Diffstat (limited to 'unittests.py')
-rwxr-xr-x | unittests.py | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/unittests.py b/unittests.py index 358d7f4..1cf617b 100755 --- a/unittests.py +++ b/unittests.py @@ -32,7 +32,8 @@ import unittest from pygerrit.events import PatchsetCreatedEvent, \ RefUpdatedEvent, ChangeMergedEvent, CommentAddedEvent, \ ChangeAbandonedEvent, ChangeRestoredEvent, \ - DraftPublishedEvent, GerritEventFactory, GerritEvent, UnhandledEvent + DraftPublishedEvent, GerritEventFactory, GerritEvent, UnhandledEvent, \ + ErrorEvent from pygerrit.client import GerritClient from setup import REQUIRES as setup_requires @@ -55,10 +56,10 @@ def _create_event(name, gerrit): data, then add as an event in the `gerrit` client. """ - data = open(os.path.join("testdata", name + ".txt")) - json_data = json.loads(data.read().replace("\n", "")) - gerrit.put_event(json_data) - return json_data + testfile = open(os.path.join("testdata", name + ".txt")) + data = testfile.read().replace("\n", "") + gerrit.put_event(data) + return data class TestConsistentDependencies(unittest.TestCase): @@ -249,10 +250,15 @@ class TestGerritEvents(unittest.TestCase): self.assertEquals(event.description, "Event description") def test_unhandled_event(self): - json_data = _create_event("unhandled-event", self.gerrit) + data = _create_event("unhandled-event", self.gerrit) event = self.gerrit.get_event(False) self.assertTrue(isinstance(event, UnhandledEvent)) - self.assertEquals(event.json, json_data) + self.assertEquals(event.json, json.loads(data)) + + def test_invalid_json(self): + _create_event("invalid-json", self.gerrit) + event = self.gerrit.get_event(False) + self.assertTrue(isinstance(event, ErrorEvent)) def test_add_duplicate_event(self): try: |