def test_use_changed_models(self) -> None: self.set_additional_models({ "test/2": { "f": 3, "weight": 42 }, "test/3": { "f": 2 }, }) result = self.adapter.get_many( [GetManyRequest(self.collection, [1, 2], ["f"])], ) assert result == { self.collection: { 1: { "f": 1 }, 2: { "f": 3 }, } } self.get_many_mock.assert_called() gmr = self.get_many_mock.call_args[0][0] assert len(gmr) == 1 assert gmr[0] == GetManyRequest(self.collection, [1], ["f"]) self.add_get_many_mock.assert_called()
def test_getMany() -> None: gmr = GetManyRequest(Collection("a"), ids=[1, 2], mapped_fields=["f"]) gmr2 = GetManyRequest(Collection("b"), [1, 2], mapped_fields=["f"]) result = db.getMany([gmr, gmr2]) assert result is not None assert result["a/1"] is not None assert result["a/2"] is not None assert result["b/1"] is not None
def process_field(self, field: Field, field_name: str, instance: Dict[str, Any], action: str) -> RelationUpdates: cml_fields = get_field_list_from_template( cast(List[str], User.committee__management_level.replacement_enum), "committee_$%s_management_level", ) if (field.own_collection.collection != "user" or field_name not in ["group_$_ids", *cml_fields] or ("group_$_ids" in instance and field_name != "group_$_ids")): return {} user_id = instance["id"] fqid = FullQualifiedId(field.own_collection, instance["id"]) db_user = self.datastore.get( fqid, ["committee_ids", "group_$_ids", *cml_fields], use_changed_models=False, raise_exception=False, ) db_committee_ids = set(db_user.get("committee_ids", []) or []) if any(cml_field in instance for cml_field in cml_fields): new_committees_ids = get_set_of_values_from_dict( instance, cml_fields) else: new_committees_ids = get_set_of_values_from_dict( db_user, cml_fields) if "group_$_ids" in instance: meeting_ids = list(map(int, instance.get("group_$_ids", []))) or [] else: meeting_ids = list(map(int, db_user.get("group_$_ids", []))) or [] meeting_collection = Collection("meeting") committee_ids: Set[int] = set( map( lambda x: x.get("committee_id", 0), self.datastore.get_many([ GetManyRequest( meeting_collection, list(meeting_ids), ["committee_id"], ) ]).get(meeting_collection, {}).values(), )) new_committees_ids.update(committee_ids) committee_ids = set( committee_id for meeting_id in meeting_ids if (committee_id := self.datastore.changed_models.get( FullQualifiedId(meeting_collection, meeting_id), {}).get( "committee_id")))
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 _fetch_groups(self, group_ids: List[int]) -> Dict[int, List[Dict[str, Any]]]: """ Helper method to partition the groups by their meeting id. """ if not group_ids: return {} response = self.datastore.get_many( [ GetManyRequest( Collection("group"), group_ids, ["id", "meeting_id", "user_ids"] ) ] ) partitioned_groups: Dict[int, List[Dict[str, Any]]] = defaultdict(list) for group in response.get(Collection("group"), {}).values(): partitioned_groups[group["meeting_id"]].append(group) return partitioned_groups
def test_only_db(self) -> None: self.set_additional_models({"test/1": {"f": 2}}) result = self.adapter.get_many( [GetManyRequest(self.collection, [1, 2], ["f"])], use_changed_models=False, ) assert result == { self.collection: { 1: { "f": 1 }, 2: { "f": 1 }, } } self.get_many_mock.assert_called() self.add_get_many_mock.assert_not_called()
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)