def test_generic_M2M_add(self) -> None: self.set_models({ "fake_model_a/1": { "fake_model_b_generic_mm": [3] }, "fake_model_a/2": {}, "fake_model_b/3": { "fake_model_a_generic_mm": ["fake_model_a/1"] }, }) handler = SingleRelationHandler( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_mm, field_name="fake_model_b_generic_mm", instance={ "id": 2, "fake_model_b_generic_mm": [3] }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/3/fake_model_a_generic_mm"): { "type": "add", "value": [get_fqid("fake_model_a/1"), get_fqid("fake_model_a/2")], "modified_element": get_fqid("fake_model_a/2"), } } assert result == expected
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) result = list(write_request_elements) expected = [ { "events": [ { "type": "create", "fqid": get_fqid("meeting/42"), "fields": { "committee_id": 5914213969, "name": "name_zusae6aD0a", }, }, { "type": "update", "fqid": get_fqid("committee/5914213969"), "fields": { "meeting_ids": [7816466305, 3908439961, 42] }, }, ], "information": { get_fqid("meeting/42"): ["Object created"], get_fqid("committee/5914213969"): ["Object attached to meeting"], }, "user_id": self.user_id, }, ] self.assertEqual(result, expected)
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("motion/2995885358"), "fields": { "title": "title_pheK0Ja3ai", "last_modified": round(time.time()), "statute_paragraph_id": None, }, }, { "type": "update", "fqid": get_fqid("motion_statute_paragraph/8264607531"), "fields": { "motion_ids": [] }, }, ], "information": { get_fqid("motion/2995885358"): ["Object updated"], get_fqid("motion_statute_paragraph/8264607531"): ["Object attachment to motion reset"], }, "user_id": self.user_id, }, ] self.assertEqual(list(write_request_elements), expected)
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "delete", "fqid": get_fqid("topic/1312354708") }, { "type": "update", "fqid": get_fqid("meeting/7816466305"), "fields": { "topic_ids": [] }, }, ], "information": { get_fqid("topic/1312354708"): ["Object deleted"], get_fqid("meeting/7816466305"): ["Object attachment to topic reset"], }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)
def test_perform_correct_2(self) -> None: write_request_elements = self.action.perform(self.valid_payload_2, user_id=self.user_id) expected = [{ "events": [ { "type": "update", "fqid": get_fqid("motion/2995885358"), "fields": { "last_modified": round(time.time()), "supporter_ids": [7268025091], }, }, { "type": "update", "fqid": get_fqid("user/7268025091"), "fields": { "supported_motion_5562405520_ids": [2995885358] }, }, ], "information": { get_fqid("motion/2995885358"): ["Object updated"], get_fqid("user/7268025091"): ["Object attached to motion"], }, "user_id": self.user_id, }] self.assertEqual( list(write_request_elements), expected, )
def test_perform_correct_2(self) -> None: write_request_elements = self.action.perform(self.valid_payload_2, user_id=self.user_id) result = list(write_request_elements) expected = [ { "events": [ { "type": "delete", "fqid": get_fqid("topic/1312354708") }, { "type": "update", "fqid": get_fqid("meeting/7816466305"), "fields": { "topic_ids": [] }, }, ], "information": { get_fqid("topic/1312354708"): ["Object deleted"], get_fqid("meeting/7816466305"): ["Object attachment to topic reset"], }, "user_id": self.user_id, }, { "events": [ { "type": "delete", "fqid": get_fqid("topic/6259289755") }, { "type": "update", "fqid": get_fqid("mediafile/3549387598"), "fields": { "attachment_ids": [] }, }, { "type": "update", "fqid": get_fqid("meeting/3611987967"), "fields": { "topic_ids": [6375863023] }, }, ], "information": { get_fqid("topic/6259289755"): ["Object deleted"], get_fqid("meeting/3611987967"): ["Object attachment to topic reset"], get_fqid("mediafile/3549387598"): ["Object attachment to topic reset"], }, "user_id": self.user_id, }, ] self.assertEqual(result, expected)
def test_perform_correct_1(self) -> None: expected = [ { "events": [ { "type": "create", "fqid": get_fqid("committee/42"), "fields": { "organisation_id": 1, "name": "name_ieth5Ha1th" }, }, { "type": "update", "fqid": get_fqid("organisation/1"), "fields": { "committee_ids": [5914213969, 42] }, }, ], "information": { get_fqid("committee/42"): ["Object created"], get_fqid("organisation/1"): ["Object attached to committee"], }, "user_id": self.user_id, }, ] write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) result = list(write_request_elements) self.assertEqual(result, expected)
def test_perform_correct_4(self) -> None: write_request_elements = self.action.perform(self.valid_payload_4, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("topic/6259289755"), "fields": { "attachment_ids": self.attachments }, }, { "type": "update", "fqid": get_fqid(f"mediafile/{self.attachments[1]}"), "fields": { "attachment_ids": [get_fqid("topic/6259289755")] }, }, ], "information": { get_fqid("topic/6259289755"): ["Object updated"], get_fqid(f"mediafile/{self.attachments[1]}"): ["Object attached to topic"], }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)
def test_perform_correct_3(self) -> None: write_request_elements = self.action.perform(self.valid_payload_3, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("topic/6259289755"), "fields": { "title": "title_Ashae0quei", "attachment_ids": [] }, }, { "type": "update", "fqid": get_fqid("mediafile/3549387598"), "fields": { "attachment_ids": [] }, }, ], "information": { get_fqid("topic/6259289755"): ["Object updated"], get_fqid("mediafile/3549387598"): ["Object attachment to topic reset"], }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "delete", "fqid": get_fqid("meeting/3908439961") }, { "type": "update", "fqid": get_fqid("committee/5914213969"), "fields": { "meeting_ids": [7816466305] }, }, ], "information": { get_fqid("meeting/3908439961"): ["Object deleted"], get_fqid("committee/5914213969"): ["Object attachment to meeting reset"], }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)
def test_generic_O2M_add(self) -> None: self.create_model("fake_model_a/1", {"fake_model_b_generic_om": 3}) self.create_model("fake_model_a/2", {}) self.create_model( "fake_model_b/3", {"fake_model_a_generic_mo": [get_fqid("fake_model_a/1")]}) handler = RelationsHandler( database=self.datastore, model=FakeModelA(), id=2, field=FakeModelA.fake_model_b_generic_om, field_name="fake_model_b_generic_om", obj={"fake_model_b_generic_om": 3}, ) result = handler.perform() expected = { get_fqfield("fake_model_b/3/fake_model_a_generic_mo"): { "type": "add", "value": [get_fqid("fake_model_a/1"), get_fqid("fake_model_a/2")], "modified_element": get_fqid("fake_model_a/2"), } } assert result == expected
def test_prepare_dataset_2(self) -> None: dataset = self.action.prepare_dataset(self.valid_payload_2) self.assertEqual( dataset["data"], [{ "instance": self.valid_payload_2[0], "new_id": 42, "relations": { get_fqfield("meeting/4002059810/topic_ids"): { "type": "add", "value": [42], }, get_fqfield(f"mediafile/{self.attachments[0]}/attachment_ids"): { "type": "add", "value": [ get_fqid("topic/6259289755"), get_fqid("topic/42"), ], }, get_fqfield(f"mediafile/{self.attachments[1]}/attachment_ids"): { "type": "add", "value": [get_fqid("topic/42")] }, }, }], )
def xtest_generic_O2O_replace(self) -> None: self.create_model("fake_model_a/1", {}) self.set_models({ "fake_model_a/2": { "fake_model_b_generic_oo": 3 }, "fake_model_b/3": { "fake_model_a_generic_oo": "fake_model_a/2" }, }) handler = SingleRelationHandler( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_oo, field_name="fake_model_b_generic_oo", instance={ "id": 1, "fake_model_b_generic_oo": 3 }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/3/fake_model_a_generic_oo"): { "type": "add", "value": get_fqid("fake_model_a/1"), "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def create_model(self, fqid: str, data: Dict[str, Any], deleted: bool = False) -> None: data["id"] = get_id_from_fqid(fqid) request = WriteRequestElement( events=[Event(type="create", fqid=get_fqid(fqid), fields=data)], information={}, user_id=0, ) if deleted: request["events"].append(Event(type="delete", fqid=get_fqid(fqid))) self.datastore.write(request)
def get_create_request( self, fqid: str, data: Dict[str, Any] = {}, deleted: bool = False ) -> WriteRequest: data["id"] = get_id_from_fqid(fqid) self.validate_fields(fqid, data) request = WriteRequest( events=[Event(type=EventType.Create, fqid=get_fqid(fqid), fields=data)], information={}, user_id=0, locked_fields={}, ) if deleted: request.events.append(Event(type=EventType.Delete, fqid=get_fqid(fqid))) return request
def test_generic_M2M_delete(self) -> None: self.set_models({ "fake_model_a/1": { "fake_model_b_generic_mm": [2] }, "fake_model_b/2": { "fake_model_a_generic_mm": ["fake_model_a/1"] }, }) handler = SingleRelationHandlerWithContext( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_mm, field_name="fake_model_b_generic_mm", instance={ "id": 1, "fake_model_b_generic_mm": [] }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_mm"): { "type": "remove", "value": [], "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def set_models(self, models: Dict[str, Dict[str, Any]]) -> None: """ Can be used to set multiple models at once, independent of create or update. """ response = self.datastore.get_many([ GetManyRequest( get_fqid(fqid).collection, [get_fqid(fqid).id], ["id"]) for fqid in models.keys() ]) for fqid_str, model in models.items(): fqid = get_fqid(fqid_str) collection_map = response.get(fqid.collection) if collection_map and fqid.id in collection_map: self.update_model(fqid_str, model) else: self.create_model(fqid_str, model)
def get_model(self, fqid: str) -> Dict[str, Any]: model = self.datastore.get( get_fqid(fqid), get_deleted_models=DeletedModelsBehaviour.ALL_MODELS) self.assertTrue(model) self.assertEqual(model.get("id"), get_id_from_fqid(fqid)) return model
def test_generic_O2O_delete(self) -> None: self.set_models({ "fake_model_a/1": { "fake_model_b_generic_oo": 2 }, "fake_model_b/2": { "fake_model_a_generic_oo": "fake_model_a/1" }, }) handler = SingleRelationHandler( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_oo, field_name="fake_model_b_generic_oo", instance={ "id": 1, "fake_model_b_generic_oo": None }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_oo"): { "type": "remove", "value": None, "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def get_update_request(self, fqid: str, data: Dict[str, Any]) -> WriteRequest: self.validate_fields(fqid, data) request = WriteRequest( events=[Event(type=EventType.Update, fqid=get_fqid(fqid), fields=data)], information={}, user_id=0, locked_fields={}, ) return request
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("motion/2995885358"), "fields": { "last_modified": round(time.time()), "category_id": None, "block_id": 4740630442, }, }, { "type": "update", "fqid": get_fqid("motion_block/4116433002"), "fields": { "motion_ids": [] }, }, { "type": "update", "fqid": get_fqid("motion_block/4740630442"), "fields": { "motion_ids": [2995885358] }, }, { "type": "update", "fqid": get_fqid("motion_category/8734727380"), "fields": { "motion_ids": [] }, }, ], "information": { get_fqid("motion/2995885358"): ["Object updated"], get_fqid("motion_block/4116433002"): ["Object attachment to motion reset"], get_fqid("motion_block/4740630442"): ["Object attached to motion"], get_fqid("motion_category/8734727380"): ["Object attachment to motion reset"], }, "user_id": self.user_id, }, ] self.assertEqual( list(write_request_elements), expected, )
def update_model(self, fqid: str, data: Dict[str, Any]) -> None: self.validate_fields(fqid, data) request = WriteRequestElement( events=[ Event(type=EventType.Update, fqid=get_fqid(fqid), fields=data) ], information={}, user_id=0, ) self.datastore.write(request)
def get_model(self, fqid: str) -> Dict[str, Any]: model = self.datastore.get( get_fqid(fqid), mapped_fields=[], get_deleted_models=DeletedModelsBehaviour.ALL_MODELS, lock_result=False, use_changed_models=False, ) self.assertTrue(model) self.assertEqual(model.get("id"), get_id_from_fqid(fqid)) return model
def set_models(self, models: Dict[str, Dict[str, Any]]) -> None: """ Can be used to set multiple models at once, independent of create or update. """ response = self.datastore.get_many( [ GetManyRequest(get_fqid(fqid).collection, [get_fqid(fqid).id], ["id"]) for fqid in models.keys() ], lock_result=False, ) requests: List[WriteRequest] = [] for fqid_str, model in models.items(): fqid = get_fqid(fqid_str) collection_map = response.get(fqid.collection) if collection_map and fqid.id in collection_map: requests.append(self.get_update_request(fqid_str, model)) else: requests.append(self.get_create_request(fqid_str, model)) self.datastore.write(requests)
def test_generic_O2M_empty(self) -> None: self.set_models({"fake_model_a/1": {}, "fake_model_b/2": {}}) handler = SingleRelationHandlerWithContext( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_om, field_name="fake_model_b_generic_om", instance={ "id": 1, "fake_model_b_generic_om": 2 }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_mo"): { "type": "add", "value": [get_fqid("fake_model_a/1")], "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def test_generic_O2O_empty(self) -> None: self.create_model("fake_model_a/1", {}) self.create_model("fake_model_b/2", {}) handler = RelationsHandler( database=self.datastore, model=FakeModelA(), id=1, field=FakeModelA.fake_model_b_generic_oo, field_name="fake_model_b_generic_oo", obj={"fake_model_b_generic_oo": 2}, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_oo"): { "type": "add", "value": get_fqid("fake_model_a/1"), "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def test_generic_M2M_empty(self) -> None: self.create_model("fake_model_a/1", {}) self.create_model("fake_model_b/2", {}) handler = SingleRelationHandler( datastore=self.datastore, field=FakeModelA.fake_model_b_generic_mm, field_name="fake_model_b_generic_mm", instance={ "id": 1, "fake_model_b_generic_mm": [2] }, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_mm"): { "type": "add", "value": [get_fqid("fake_model_a/1")], "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def test_generic_M2M_delete(self) -> None: self.create_model("fake_model_a/1", {"fake_model_b_generic_mm": [2]}) self.create_model( "fake_model_b/2", {"fake_model_a_generic_mm": [get_fqid("fake_model_a/1")]}) handler = RelationsHandler( database=self.datastore, model=FakeModelA(), id=1, field=FakeModelA.fake_model_b_generic_mm, field_name="fake_model_b_generic_mm", obj={"fake_model_b_generic_mm": []}, ) result = handler.perform() expected = { get_fqfield("fake_model_b/2/fake_model_a_generic_mm"): { "type": "remove", "value": [], "modified_element": get_fqid("fake_model_a/1"), } } assert result == expected
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("meeting/7816466305"), "fields": { "name": "name_GeiduDohx0" }, }, ], "information": { get_fqid("meeting/7816466305"): ["Object updated"] }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)
def test_perform_correct_1(self) -> None: write_request_elements = self.action.perform(self.valid_payload_1, user_id=self.user_id) expected = [ { "events": [ { "type": "update", "fqid": get_fqid("topic/1312354708"), "fields": { "title": "title_ahbuQu9ooz", "text": "text_thuF7Ahxee", }, }, ], "information": { get_fqid("topic/1312354708"): ["Object updated"] }, "user_id": self.user_id, }, ] result = list(write_request_elements) self.assertEqual(result, expected)