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

Commit 70a2157

Browse files
committed
Start adding some tests
1 parent f97511a commit 70a2157

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

synapse/events/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from synapse.api.constants import EventTypes
1717
from . import EventBase
1818

19+
from frozendict import frozendict
20+
1921
import re
2022

2123
# Split strings on "." but not "\." This uses a negative lookbehind assertion for '\'
@@ -130,7 +132,7 @@ def _copy_field(src, dst, field):
130132
key_to_move = field.pop(-1)
131133
sub_dict = src
132134
for sub_field in field: # e.g. sub_field => "content"
133-
if sub_field in sub_dict and type(sub_dict[sub_field]) == dict:
135+
if sub_field in sub_dict and type(sub_dict[sub_field]) == frozendict:
134136
sub_dict = sub_dict[sub_field]
135137
else:
136138
return

tests/events/test_utils.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
from .. import unittest
1818

1919
from synapse.events import FrozenEvent
20-
from synapse.events.utils import prune_event
20+
from synapse.events.utils import prune_event, serialize_event
21+
22+
23+
def MockEvent(**kwargs):
24+
return FrozenEvent(kwargs)
2125

2226

2327
class PruneEventTestCase(unittest.TestCase):
@@ -118,11 +122,41 @@ def test_content(self):
118122

119123
class SerializeEventTestCase(unittest.TestCase):
120124

125+
def serialize(self, ev, fields):
126+
return serialize_event(ev, 1924354, event_fields=fields)
127+
121128
def test_event_fields_works_with_keys(self):
122-
pass
129+
self.assertEquals(
130+
self.serialize(
131+
MockEvent(
132+
sender="@alice:localhost",
133+
room_id="!foo:bar"
134+
),
135+
["room_id"]
136+
),
137+
{
138+
"room_id": "!foo:bar",
139+
}
140+
)
123141

124142
def test_event_fields_works_with_nested_keys(self):
125-
pass
143+
self.assertEquals(
144+
self.serialize(
145+
MockEvent(
146+
sender="@alice:localhost",
147+
room_id="!foo:bar",
148+
content={
149+
"body": "A message",
150+
},
151+
),
152+
["content.body"]
153+
),
154+
{
155+
"content": {
156+
"body": "A message",
157+
}
158+
}
159+
)
126160

127161
def test_event_fields_works_with_dot_keys(self):
128162
pass

0 commit comments

Comments
 (0)