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

docstrings and unittests for storage.state #3958

Merged
merged 5 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3958.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix docstrings and add tests for state store methods
30 changes: 22 additions & 8 deletions synapse/storage/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,17 @@ def _get_state_group_delta_txn(txn):
)

@defer.inlineCallbacks
def get_state_groups_ids(self, room_id, event_ids):
def get_state_groups_ids(self, _room_id, event_ids):
"""Get the event IDs of all the state for the state groups for the given events

Args:
_room_id (str): id of the room for these events
event_ids (iterable[str]): ids of the events

Returns:
Deferred[dict[int, dict[tuple[str, str], str]]]:
dict of state_group_id -> (dict of (type, state_key) -> event id)
"""
if not event_ids:
defer.returnValue({})

Expand All @@ -270,7 +280,7 @@ def get_state_groups_ids(self, room_id, event_ids):

@defer.inlineCallbacks
def get_state_ids_for_group(self, state_group):
"""Get the state IDs for the given state group
"""Get the event IDs of all the state in the given state group

Args:
state_group (int)
Expand All @@ -286,7 +296,9 @@ def get_state_ids_for_group(self, state_group):
def get_state_groups(self, room_id, event_ids):
""" Get the state groups for the given list of event_ids

The return value is a dict mapping group names to lists of events.
Returns:
Deferred[dict[int, list[EventBase]]]:
dict of state_group_id -> list of state events.
"""
if not event_ids:
defer.returnValue({})
Expand Down Expand Up @@ -324,7 +336,9 @@ def _get_state_groups_from_groups(self, groups, types, members=None):
member events (if True), or to exclude member events (if False)

Returns:
dictionary state_group -> (dict of (type, state_key) -> event id)
Returns:
Deferred[dict[int, dict[tuple[str, str], str]]]:
dict of state_group_id -> (dict of (type, state_key) -> event id)
"""
results = {}

Expand Down Expand Up @@ -732,8 +746,8 @@ def _get_state_for_groups(self, groups, types=None, filtered_types=None):
If None, `types` filtering is applied to all events.

Returns:
Deferred[dict[int, dict[(type, state_key), EventBase]]]
a dictionary mapping from state group to state dictionary.
Deferred[dict[int, dict[tuple[str, str], str]]]:
dict of state_group_id -> (dict of (type, state_key) -> event id)
"""
if types is not None:
non_member_types = [t for t in types if t[0] != EventTypes.Member]
Expand Down Expand Up @@ -788,8 +802,8 @@ def _get_state_for_groups_using_cache(
If None, `types` filtering is applied to all events.

Returns:
Deferred[dict[int, dict[(type, state_key), EventBase]]]
a dictionary mapping from state group to state dictionary.
Deferred[dict[int, dict[tuple[str, str], str]]]:
dict of state_group_id -> (dict of (type, state_key) -> event id)
"""
if types:
types = frozenset(types)
Expand Down
39 changes: 39 additions & 0 deletions tests/storage/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,45 @@ def assertStateMapEqual(self, s1, s2):
self.assertEqual(s1[t].event_id, s2[t].event_id)
self.assertEqual(len(s1), len(s2))

@defer.inlineCallbacks
def test_get_state_groups_ids(self):
e1 = yield self.inject_state_event(
self.room, self.u_alice, EventTypes.Create, '', {}
)
e2 = yield self.inject_state_event(
self.room, self.u_alice, EventTypes.Name, '', {"name": "test room"}
)

state_group_map = yield self.store.get_state_groups_ids(self.room, [e2.event_id])
self.assertEqual(len(state_group_map), 1)
state_map = list(state_group_map.values())[0]
self.assertDictEqual(
state_map,
{
(EventTypes.Create, ''): e1.event_id,
(EventTypes.Name, ''): e2.event_id,
},
)

@defer.inlineCallbacks
def test_get_state_groups(self):
e1 = yield self.inject_state_event(
self.room, self.u_alice, EventTypes.Create, '', {}
)
e2 = yield self.inject_state_event(
self.room, self.u_alice, EventTypes.Name, '', {"name": "test room"}
)

state_group_map = yield self.store.get_state_groups(
self.room, [e2.event_id])
self.assertEqual(len(state_group_map), 1)
state_list = list(state_group_map.values())[0]

self.assertEqual(
{ev.event_id for ev in state_list},
{e1.event_id, e2.event_id},
)

@defer.inlineCallbacks
def test_get_state_for_event(self):

Expand Down