diff options
author | matrix.org <matrix@matrix.org> | 2014-08-12 15:10:52 +0100 |
---|---|---|
committer | matrix.org <matrix@matrix.org> | 2014-08-12 15:10:52 +0100 |
commit | 4f475c7697722e946e39e42f38f3dd03a95d8765 (patch) | |
tree | 076d96d3809fb836c7245fd9f7960e7b75888a77 /tests/events | |
download | synapse-4f475c7697722e946e39e42f38f3dd03a95d8765.tar.xz |
Reference Matrix Home Server
Diffstat (limited to 'tests/events')
-rw-r--r-- | tests/events/__init__.py | 1 | ||||
-rw-r--r-- | tests/events/test_events.py | 172 |
2 files changed, 173 insertions, 0 deletions
diff --git a/tests/events/__init__.py b/tests/events/__init__.py new file mode 100644 index 0000000000..40a96afc6f --- /dev/null +++ b/tests/events/__init__.py @@ -0,0 +1 @@ +# -*- coding: utf-8 -*- diff --git a/tests/events/test_events.py b/tests/events/test_events.py new file mode 100644 index 0000000000..11d3d09c96 --- /dev/null +++ b/tests/events/test_events.py @@ -0,0 +1,172 @@ +# -*- coding: utf-8 -*- +from synapse.api.events import SynapseEvent + +import unittest + + +class SynapseTemplateCheckTestCase(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_top_level_keys(self): + template = { + "person": {}, + "friends": ["string"] + } + + content = { + "person": {"name": "bob"}, + "friends": ["jill", "mike"] + } + + event = MockSynapseEvent(template) + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + "friends": ["jill"], + "enemies": ["mike"] + } + event = MockSynapseEvent(template) + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + # missing friends + "enemies": ["mike", "jill"] + } + self.assertFalse(event.check_json(content, raises=False)) + + def test_lists(self): + template = { + "person": {}, + "friends": [{"name":"string"}] + } + + content = { + "person": {"name": "bob"}, + "friends": ["jill", "mike"] # should be in objects + } + + event = MockSynapseEvent(template) + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "person": {"name": "bob"}, + "friends": [{"name": "jill"}, {"name": "mike"}] + } + self.assertTrue(event.check_json(content, raises=False)) + + def test_nested_lists(self): + template = { + "results": { + "families": [ + { + "name": "string", + "members": [ + {} + ] + } + ] + } + } + + content = { + "results": { + "families": [ + { + "name": "Smith", + "members": [ + "Alice", "Bob" # wrong types + ] + } + ] + } + } + + event = MockSynapseEvent(template) + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "results": { + "families": [ + { + "name": "Smith", + "members": [ + {"name": "Alice"}, {"name": "Bob"} + ] + } + ] + } + } + self.assertTrue(event.check_json(content, raises=False)) + + def test_nested_keys(self): + template = { + "person": { + "attributes": { + "hair": "string", + "eye": "string" + }, + "age": 0, + "fav_books": ["string"] + } + } + event = MockSynapseEvent(template) + + content = { + "person": { + "attributes": { + "hair": "brown", + "eye": "green", + "skin": "purple" + }, + "age": 33, + "fav_books": ["lotr", "hobbit"], + "fav_music": ["abba", "beatles"] + } + } + + self.assertTrue(event.check_json(content, raises=False)) + + content = { + "person": { + "attributes": { + "hair": "brown" + # missing eye + }, + "age": 33, + "fav_books": ["lotr", "hobbit"], + "fav_music": ["abba", "beatles"] + } + } + + self.assertFalse(event.check_json(content, raises=False)) + + content = { + "person": { + "attributes": { + "hair": "brown", + "eye": "green", + "skin": "purple" + }, + "age": 33, + "fav_books": "nothing", # should be a list + } + } + + self.assertFalse(event.check_json(content, raises=False)) + + +class MockSynapseEvent(SynapseEvent): + + def __init__(self, template): + self.template = template + + def get_content_template(self): + return self.template + |