Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Start adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kegsay committed Nov 21, 2016
1 parent f97511a commit 70a2157
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion synapse/events/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from synapse.api.constants import EventTypes
from . import EventBase

from frozendict import frozendict

import re

# Split strings on "." but not "\." This uses a negative lookbehind assertion for '\'
Expand Down Expand Up @@ -130,7 +132,7 @@ def _copy_field(src, dst, field):
key_to_move = field.pop(-1)
sub_dict = src
for sub_field in field: # e.g. sub_field => "content"
if sub_field in sub_dict and type(sub_dict[sub_field]) == dict:
if sub_field in sub_dict and type(sub_dict[sub_field]) == frozendict:
sub_dict = sub_dict[sub_field]
else:
return
Expand Down
40 changes: 37 additions & 3 deletions tests/events/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
from .. import unittest

from synapse.events import FrozenEvent
from synapse.events.utils import prune_event
from synapse.events.utils import prune_event, serialize_event


def MockEvent(**kwargs):
return FrozenEvent(kwargs)


class PruneEventTestCase(unittest.TestCase):
Expand Down Expand Up @@ -118,11 +122,41 @@ def test_content(self):

class SerializeEventTestCase(unittest.TestCase):

def serialize(self, ev, fields):
return serialize_event(ev, 1924354, event_fields=fields)

def test_event_fields_works_with_keys(self):
pass
self.assertEquals(
self.serialize(
MockEvent(
sender="@alice:localhost",
room_id="!foo:bar"
),
["room_id"]
),
{
"room_id": "!foo:bar",
}
)

def test_event_fields_works_with_nested_keys(self):
pass
self.assertEquals(
self.serialize(
MockEvent(
sender="@alice:localhost",
room_id="!foo:bar",
content={
"body": "A message",
},
),
["content.body"]
),
{
"content": {
"body": "A message",
}
}
)

def test_event_fields_works_with_dot_keys(self):
pass
Expand Down

0 comments on commit 70a2157

Please sign in to comment.