12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103 |
- # Copyright 2018-2021 The Matrix.org Foundation C.I.C.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- import logging
- from frozendict import frozendict
- from synapse.api.constants import EventTypes, Membership
- from synapse.api.room_versions import RoomVersions
- from synapse.storage.state import StateFilter
- from synapse.types import RoomID, UserID
- from tests.unittest import HomeserverTestCase, TestCase
- logger = logging.getLogger(__name__)
- class StateStoreTestCase(HomeserverTestCase):
- def prepare(self, reactor, clock, hs):
- self.store = hs.get_datastores().main
- self.storage = hs.get_storage_controllers()
- self.state_datastore = self.storage.state.stores.state
- self.event_builder_factory = hs.get_event_builder_factory()
- self.event_creation_handler = hs.get_event_creation_handler()
- self.u_alice = UserID.from_string("@alice:test")
- self.u_bob = UserID.from_string("@bob:test")
- self.room = RoomID.from_string("!abc123:test")
- self.get_success(
- self.store.store_room(
- self.room.to_string(),
- room_creator_user_id="@creator:text",
- is_public=True,
- room_version=RoomVersions.V1,
- )
- )
- def inject_state_event(self, room, sender, typ, state_key, content):
- builder = self.event_builder_factory.for_room_version(
- RoomVersions.V1,
- {
- "type": typ,
- "sender": sender.to_string(),
- "state_key": state_key,
- "room_id": room.to_string(),
- "content": content,
- },
- )
- event, context = self.get_success(
- self.event_creation_handler.create_new_client_event(builder)
- )
- self.get_success(self.storage.persistence.persist_event(event, context))
- return event
- def assertStateMapEqual(self, s1, s2):
- for t in s1:
- # just compare event IDs for simplicity
- self.assertEqual(s1[t].event_id, s2[t].event_id)
- self.assertEqual(len(s1), len(s2))
- def test_get_state_groups_ids(self):
- e1 = self.inject_state_event(self.room, self.u_alice, EventTypes.Create, "", {})
- e2 = self.inject_state_event(
- self.room, self.u_alice, EventTypes.Name, "", {"name": "test room"}
- )
- state_group_map = self.get_success(
- self.storage.state.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},
- )
- def test_get_state_groups(self):
- e1 = self.inject_state_event(self.room, self.u_alice, EventTypes.Create, "", {})
- e2 = self.inject_state_event(
- self.room, self.u_alice, EventTypes.Name, "", {"name": "test room"}
- )
- state_group_map = self.get_success(
- self.storage.state.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})
- def test_get_state_for_event(self):
- # this defaults to a linear DAG as each new injection defaults to whatever
- # forward extremities are currently in the DB for this room.
- e1 = self.inject_state_event(self.room, self.u_alice, EventTypes.Create, "", {})
- e2 = self.inject_state_event(
- self.room, self.u_alice, EventTypes.Name, "", {"name": "test room"}
- )
- e3 = self.inject_state_event(
- self.room,
- self.u_alice,
- EventTypes.Member,
- self.u_alice.to_string(),
- {"membership": Membership.JOIN},
- )
- e4 = self.inject_state_event(
- self.room,
- self.u_bob,
- EventTypes.Member,
- self.u_bob.to_string(),
- {"membership": Membership.JOIN},
- )
- e5 = self.inject_state_event(
- self.room,
- self.u_bob,
- EventTypes.Member,
- self.u_bob.to_string(),
- {"membership": Membership.LEAVE},
- )
- # check we get the full state as of the final event
- state = self.get_success(self.storage.state.get_state_for_event(e5.event_id))
- self.assertIsNotNone(e4)
- self.assertStateMapEqual(
- {
- (e1.type, e1.state_key): e1,
- (e2.type, e2.state_key): e2,
- (e3.type, e3.state_key): e3,
- # e4 is overwritten by e5
- (e5.type, e5.state_key): e5,
- },
- state,
- )
- # check we can filter to the m.room.name event (with a '' state key)
- state = self.get_success(
- self.storage.state.get_state_for_event(
- e5.event_id, StateFilter.from_types([(EventTypes.Name, "")])
- )
- )
- self.assertStateMapEqual({(e2.type, e2.state_key): e2}, state)
- # check we can filter to the m.room.name event (with a wildcard None state key)
- state = self.get_success(
- self.storage.state.get_state_for_event(
- e5.event_id, StateFilter.from_types([(EventTypes.Name, None)])
- )
- )
- self.assertStateMapEqual({(e2.type, e2.state_key): e2}, state)
- # check we can grab the m.room.member events (with a wildcard None state key)
- state = self.get_success(
- self.storage.state.get_state_for_event(
- e5.event_id, StateFilter.from_types([(EventTypes.Member, None)])
- )
- )
- self.assertStateMapEqual(
- {(e3.type, e3.state_key): e3, (e5.type, e5.state_key): e5}, state
- )
- # check we can grab a specific room member without filtering out the
- # other event types
- state = self.get_success(
- self.storage.state.get_state_for_event(
- e5.event_id,
- state_filter=StateFilter(
- types=frozendict(
- {EventTypes.Member: frozenset({self.u_alice.to_string()})}
- ),
- include_others=True,
- ),
- )
- )
- self.assertStateMapEqual(
- {
- (e1.type, e1.state_key): e1,
- (e2.type, e2.state_key): e2,
- (e3.type, e3.state_key): e3,
- },
- state,
- )
- # check that we can grab everything except members
- state = self.get_success(
- self.storage.state.get_state_for_event(
- e5.event_id,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset()}),
- include_others=True,
- ),
- )
- )
- self.assertStateMapEqual(
- {(e1.type, e1.state_key): e1, (e2.type, e2.state_key): e2}, state
- )
- #######################################################
- # _get_state_for_group_using_cache tests against a full cache
- #######################################################
- room_id = self.room.to_string()
- group_ids = self.get_success(
- self.storage.state.get_state_groups_ids(room_id, [e5.event_id])
- )
- group = list(group_ids.keys())[0]
- # test _get_state_for_group_using_cache correctly filters out members
- # with types=[]
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset()}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual(
- {
- (e1.type, e1.state_key): e1.event_id,
- (e2.type, e2.state_key): e2.event_id,
- },
- state_dict,
- )
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset()}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({}, state_dict)
- # test _get_state_for_group_using_cache correctly filters in members
- # with wildcard types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: None}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual(
- {
- (e1.type, e1.state_key): e1.event_id,
- (e2.type, e2.state_key): e2.event_id,
- },
- state_dict,
- )
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: None}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual(
- {
- (e3.type, e3.state_key): e3.event_id,
- # e4 is overwritten by e5
- (e5.type, e5.state_key): e5.event_id,
- },
- state_dict,
- )
- # test _get_state_for_group_using_cache correctly filters in members
- # with specific types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=True,
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual(
- {
- (e1.type, e1.state_key): e1.event_id,
- (e2.type, e2.state_key): e2.event_id,
- },
- state_dict,
- )
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=True,
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
- # test _get_state_for_group_using_cache correctly filters in members
- # with specific types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=False,
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
- #######################################################
- # deliberately remove e2 (room name) from the _state_group_cache
- cache_entry = self.state_datastore._state_group_cache.get(group)
- state_dict_ids = cache_entry.value
- self.assertEqual(cache_entry.full, True)
- self.assertEqual(cache_entry.known_absent, set())
- self.assertDictEqual(
- state_dict_ids,
- {
- (e1.type, e1.state_key): e1.event_id,
- (e2.type, e2.state_key): e2.event_id,
- },
- )
- state_dict_ids.pop((e2.type, e2.state_key))
- self.state_datastore._state_group_cache.invalidate(group)
- self.state_datastore._state_group_cache.update(
- sequence=self.state_datastore._state_group_cache.sequence,
- key=group,
- value=state_dict_ids,
- # list fetched keys so it knows it's partial
- fetched_keys=((e1.type, e1.state_key),),
- )
- cache_entry = self.state_datastore._state_group_cache.get(group)
- state_dict_ids = cache_entry.value
- self.assertEqual(cache_entry.full, False)
- self.assertEqual(cache_entry.known_absent, set())
- self.assertDictEqual(state_dict_ids, {})
- ############################################
- # test that things work with a partial cache
- # test _get_state_for_group_using_cache correctly filters out members
- # with types=[]
- room_id = self.room.to_string()
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset()}), include_others=True
- ),
- )
- self.assertEqual(is_all, False)
- self.assertDictEqual({}, state_dict)
- room_id = self.room.to_string()
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset()}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({}, state_dict)
- # test _get_state_for_group_using_cache correctly filters in members
- # wildcard types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: None}), include_others=True
- ),
- )
- self.assertEqual(is_all, False)
- self.assertDictEqual({}, state_dict)
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: None}), include_others=True
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual(
- {
- (e3.type, e3.state_key): e3.event_id,
- (e5.type, e5.state_key): e5.event_id,
- },
- state_dict,
- )
- # test _get_state_for_group_using_cache correctly filters in members
- # with specific types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=True,
- ),
- )
- self.assertEqual(is_all, False)
- self.assertDictEqual({}, state_dict)
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=True,
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
- # test _get_state_for_group_using_cache correctly filters in members
- # with specific types
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=False,
- ),
- )
- self.assertEqual(is_all, False)
- self.assertDictEqual({}, state_dict)
- (state_dict, is_all,) = self.state_datastore._get_state_for_group_using_cache(
- self.state_datastore._state_group_members_cache,
- group,
- state_filter=StateFilter(
- types=frozendict({EventTypes.Member: frozenset({e5.state_key})}),
- include_others=False,
- ),
- )
- self.assertEqual(is_all, True)
- self.assertDictEqual({(e5.type, e5.state_key): e5.event_id}, state_dict)
- class StateFilterDifferenceTestCase(TestCase):
- def assert_difference(
- self, minuend: StateFilter, subtrahend: StateFilter, expected: StateFilter
- ):
- self.assertEqual(
- minuend.approx_difference(subtrahend),
- expected,
- f"StateFilter difference not correct:\n\n\t{minuend!r}\nminus\n\t{subtrahend!r}\nwas\n\t{minuend.approx_difference(subtrahend)}\nexpected\n\t{expected}",
- )
- def test_state_filter_difference_no_include_other_minus_no_include_other(self):
- """
- Tests the StateFilter.approx_difference method
- where, in a.approx_difference(b), both a and b do not have the
- include_others flag set.
- """
- # (wildcard on state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.Create: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.CanonicalAlias: None},
- include_others=False,
- ),
- StateFilter.freeze({EventTypes.Create: None}, include_others=False),
- )
- # (wildcard on state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- self.assert_difference(
- StateFilter.freeze({EventTypes.Member: None}, include_others=False),
- StateFilter.freeze(
- {EventTypes.Member: {"@wombat:spqr"}},
- include_others=False,
- ),
- StateFilter.freeze({EventTypes.Member: None}, include_others=False),
- )
- # (wildcard on state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- )
- # (specific state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.CanonicalAlias: {""}},
- include_others=False,
- ),
- )
- # (specific state keys) - (specific state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- )
- # (specific state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- )
- def test_state_filter_difference_include_other_minus_no_include_other(self):
- """
- Tests the StateFilter.approx_difference method
- where, in a.approx_difference(b), only a has the include_others flag set.
- """
- # (wildcard on state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.Create: None},
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.CanonicalAlias: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Create: None,
- EventTypes.Member: set(),
- EventTypes.CanonicalAlias: set(),
- },
- include_others=True,
- ),
- )
- # (wildcard on state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- # This also shows that the resultant state filter is normalised.
- self.assert_difference(
- StateFilter.freeze({EventTypes.Member: None}, include_others=True),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- EventTypes.Create: {""},
- },
- include_others=False,
- ),
- StateFilter(types=frozendict(), include_others=True),
- )
- # (wildcard on state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=False,
- ),
- StateFilter(
- types=frozendict(),
- include_others=True,
- ),
- )
- # (specific state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.CanonicalAlias: {""},
- EventTypes.Member: set(),
- },
- include_others=True,
- ),
- )
- # (specific state keys) - (specific state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- )
- # (specific state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- )
- def test_state_filter_difference_include_other_minus_include_other(self):
- """
- Tests the StateFilter.approx_difference method
- where, in a.approx_difference(b), both a and b have the include_others
- flag set.
- """
- # (wildcard on state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.Create: None},
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.CanonicalAlias: None},
- include_others=True,
- ),
- StateFilter(types=frozendict(), include_others=False),
- )
- # (wildcard on state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- self.assert_difference(
- StateFilter.freeze({EventTypes.Member: None}, include_others=True),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.CanonicalAlias: None},
- include_others=False,
- ),
- )
- # (wildcard on state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- )
- # (specific state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=True,
- ),
- StateFilter(
- types=frozendict(),
- include_others=False,
- ),
- )
- # (specific state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- EventTypes.Create: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- EventTypes.Create: set(),
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@spqr:spqr"},
- EventTypes.Create: {""},
- },
- include_others=False,
- ),
- )
- # (specific state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- },
- include_others=False,
- ),
- )
- def test_state_filter_difference_no_include_other_minus_include_other(self):
- """
- Tests the StateFilter.approx_difference method
- where, in a.approx_difference(b), only b has the include_others flag set.
- """
- # (wildcard on state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.Create: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None, EventTypes.CanonicalAlias: None},
- include_others=True,
- ),
- StateFilter(types=frozendict(), include_others=False),
- )
- # (wildcard on state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- self.assert_difference(
- StateFilter.freeze({EventTypes.Member: None}, include_others=False),
- StateFilter.freeze(
- {EventTypes.Member: {"@wombat:spqr"}},
- include_others=True,
- ),
- StateFilter.freeze({EventTypes.Member: None}, include_others=False),
- )
- # (wildcard on state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- )
- # (specific state keys) - (wildcard on state keys):
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=True,
- ),
- StateFilter(
- types=frozendict(),
- include_others=False,
- ),
- )
- # (specific state keys) - (specific state keys)
- # This one is an over-approximation because we can't represent
- # 'all state keys except a few named examples'
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr"},
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@spqr:spqr"},
- },
- include_others=False,
- ),
- )
- # (specific state keys) - (no state keys)
- self.assert_difference(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- EventTypes.CanonicalAlias: {""},
- },
- include_others=False,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: set(),
- },
- include_others=True,
- ),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:spqr", "@spqr:spqr"},
- },
- include_others=False,
- ),
- )
- def test_state_filter_difference_simple_cases(self):
- """
- Tests some very simple cases of the StateFilter approx_difference,
- that are not explicitly tested by the more in-depth tests.
- """
- self.assert_difference(StateFilter.all(), StateFilter.all(), StateFilter.none())
- self.assert_difference(
- StateFilter.all(),
- StateFilter.none(),
- StateFilter.all(),
- )
- class StateFilterTestCase(TestCase):
- def test_return_expanded(self):
- """
- Tests the behaviour of the return_expanded() function that expands
- StateFilters to include more state types (for the sake of cache hit rate).
- """
- self.assertEqual(StateFilter.all().return_expanded(), StateFilter.all())
- self.assertEqual(StateFilter.none().return_expanded(), StateFilter.none())
- # Concrete-only state filters stay the same
- # (Case: mixed filter)
- self.assertEqual(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:test", "@alicia:test"},
- "some.other.state.type": {""},
- },
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:test", "@alicia:test"},
- "some.other.state.type": {""},
- },
- include_others=False,
- ),
- )
- # Concrete-only state filters stay the same
- # (Case: non-member-only filter)
- self.assertEqual(
- StateFilter.freeze(
- {"some.other.state.type": {""}}, include_others=False
- ).return_expanded(),
- StateFilter.freeze({"some.other.state.type": {""}}, include_others=False),
- )
- # Concrete-only state filters stay the same
- # (Case: member-only filter)
- self.assertEqual(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:test", "@alicia:test"},
- },
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:test", "@alicia:test"},
- },
- include_others=False,
- ),
- )
- # Wildcard member-only state filters stay the same
- self.assertEqual(
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze(
- {EventTypes.Member: None},
- include_others=False,
- ),
- )
- # If there is a wildcard in the non-member portion of the filter,
- # it's expanded to include ALL non-member events.
- # (Case: mixed filter)
- self.assertEqual(
- StateFilter.freeze(
- {
- EventTypes.Member: {"@wombat:test", "@alicia:test"},
- "some.other.state.type": None,
- },
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze(
- {EventTypes.Member: {"@wombat:test", "@alicia:test"}},
- include_others=True,
- ),
- )
- # If there is a wildcard in the non-member portion of the filter,
- # it's expanded to include ALL non-member events.
- # (Case: non-member-only filter)
- self.assertEqual(
- StateFilter.freeze(
- {
- "some.other.state.type": None,
- },
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze({EventTypes.Member: set()}, include_others=True),
- )
- self.assertEqual(
- StateFilter.freeze(
- {
- "some.other.state.type": None,
- "yet.another.state.type": {"wombat"},
- },
- include_others=False,
- ).return_expanded(),
- StateFilter.freeze({EventTypes.Member: set()}, include_others=True),
- )
|