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 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 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 test_nested_structured_relation(self) -> None:
     meeting_id = 222
     self.create_model("fake_model_a/333", {})
     self.create_model("fake_model_b/111", {"meeting_id": meeting_id})
     self.create_model("fake_model_c/444", {"foreign_key_field": 111})
     field = cast(
         fields.BaseRelationField,
         FakeModelC().get_field("structured_relation_field"),
     )
     relations_handler = SingleRelationHandler(
         datastore=self.datastore,
         field=field,
         field_name="structured_relation_field",
         instance={
             "id": 444,
             "structured_relation_field": 333
         },
     )
     result = relations_handler.perform()
     self.assertEqual(
         result,
         {
             get_fqfield("fake_model_a/333/fake_model_c_$_ids"): {
                 "type": "add",
                 "value": [str(meeting_id)],
                 "modified_element": str(meeting_id),
             },
             get_fqfield(f"fake_model_a/333/fake_model_c_${meeting_id}_ids"):
             {
                 "type": "add",
                 "value": [444],
                 "modified_element": 444,
             },
         },
     )
 def test_M2M_delete(self) -> None:
     self.set_models({
         "fake_model_a/1": {
             "fake_model_b_mm": [2]
         },
         "fake_model_b/2": {
             "fake_model_a_mm": [1]
         },
     })
     handler = SingleRelationHandler(
         datastore=self.datastore,
         field=FakeModelA.fake_model_b_mm,
         field_name="fake_model_b_mm",
         instance={
             "id": 1,
             "fake_model_b_mm": []
         },
     )
     result = handler.perform()
     expected = {
         get_fqfield("fake_model_b/2/fake_model_a_mm"): {
             "type": "remove",
             "value": [],
             "modified_element": 1,
         }
     }
     assert result == expected
 def test_O2O_empty(self) -> None:
     self.set_models({"fake_model_a/1": {}, "fake_model_b/2": {}})
     handler = SingleRelationHandler(
         datastore=self.datastore,
         field=FakeModelA.fake_model_b_oo,
         field_name="fake_model_b_oo",
         instance={
             "id": 1,
             "fake_model_b_oo": 2
         },
     )
     result = handler.perform()
     expected = {
         get_fqfield("fake_model_b/2/fake_model_a_oo"): {
             "type": "add",
             "value": 1,
             "modified_element": 1,
         }
     }
     assert result == expected
 def test_O2M_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_om,
         field_name="fake_model_b_om",
         instance={
             "id": 1,
             "fake_model_b_om": 2
         },
     )
     result = handler.perform()
     expected = {
         get_fqfield("fake_model_b/2/fake_model_a_mo"): {
             "type": "add",
             "value": [1],
             "modified_element": 1,
         }
     }
     assert result == expected
 def test_simple_structured_relation(self) -> None:
     meeting_id = 222
     self.set_models({
         "fake_model_a/333": {},
         "fake_model_b/111": {
             "meeting_id": meeting_id
         }
     })
     field = cast(
         fields.BaseRelationField,
         FakeModelB().get_field("structured_relation_field"),
     )
     relations_handler = SingleRelationHandler(
         datastore=self.datastore,
         field=field,
         field_name="structured_relation_field",
         instance={
             "id": 111,
             "structured_relation_field": 333
         },
     )
     result = relations_handler.perform()
     self.assertEqual(
         result,
         {
             get_fqfield("fake_model_a/333/fake_model_b_$_ids"): {
                 "type": "add",
                 "value": [str(meeting_id)],
                 "modified_element": str(meeting_id),
             },
             get_fqfield(f"fake_model_a/333/fake_model_b_${meeting_id}_ids"):
             {
                 "type": "add",
                 "value": [111],
                 "modified_element": 111,
             },
         },
     )