def test_get_model_dict_many_to_many_is_referenced(): taste = Taste(name="Bitter") taste.save() flavor_one = Flavor(name="name1", label="label1") flavor_one.save() flavor_one.taste_set.add(taste) actual = serializer._get_model_dict(flavor_one) expected = { "pk": 1, "name": "name1", "label": "label1", "parent": None, "decimal_value": None, "float_value": None, "uuid": str(flavor_one.uuid), "date": None, "datetime": None, "time": None, "duration": None, "taste_set": [taste.pk], "origins": [], } assert expected == actual
def test_get_model_dict_many_to_many_references_model(): taste = Taste(name="Bitter") taste.save() flavor_one = Flavor(name="name1", label="label1") flavor_one.save() flavor_one.taste_set.add(taste) actual = serializer._get_model_dict(taste) expected = {"name": taste.name, "flavor": [flavor_one.pk], "pk": taste.pk} assert expected == actual
class FakeComponent(UnicornView): string = "property_view" integer = 99 datetime = datetime(2020, 1, 1) array: List[str] = [] model = Flavor(name="initial-flavor") queryset = Flavor.objects.none() taste = Taste(name="bitter")
def test_set_property_value_many_to_many_is_referenced(): component = FakeComponent(component_name="test", component_id="12345678") component.model.save() assert component.model.taste_set.count() == 0 taste = Taste(name="Bitter") taste.save() flavor = Flavor(name="test-flavor") flavor.save() flavor.taste_set.add(taste) data = {"model": {}} set_property_value( component, "model.taste_set", [taste.pk], data, ) assert data["model"]["taste_set"] == [taste.pk] assert component.model.taste_set.count() == 1
def test_get_model_dict_many_to_many_is_referenced_prefetched( django_assert_num_queries, ): flavor_one = Flavor(name="name1", label="label1") flavor_one.save() taste1 = Taste(name="Bitter") taste1.save() taste2 = Taste(name="Bitter2") taste2.save() taste3 = Taste(name="Bitter3") taste3.save() flavor_one.taste_set.add(taste1) flavor_one.taste_set.add(taste2) flavor_one.taste_set.add(taste3) expected = { "pk": 1, "name": "name1", "label": "label1", "parent": None, "decimal_value": None, "float_value": None, "uuid": str(flavor_one.uuid), "date": None, "datetime": None, "time": None, "duration": None, "taste_set": [taste1.pk, taste2.pk, taste3.pk], "origins": [], } flavor_one = (Flavor.objects.prefetch_related("taste_set").filter( id=flavor_one.id).first()) # prefetch_related should reduce the database calls with django_assert_num_queries(1): actual = serializer._get_model_dict(flavor_one) assert expected == actual
def test_model_many_to_many_with_excludes(django_assert_num_queries): flavor_one = Flavor(name="name1", label="label1") flavor_one.save() taste1 = Taste(name="Bitter1") taste1.save() taste2 = Taste(name="Bitter2") taste2.save() taste3 = Taste(name="Bitter3") taste3.save() flavor_one.taste_set.add(taste1) flavor_one.taste_set.add(taste2) flavor_one.taste_set.add(taste3) flavor_one = Flavor.objects.prefetch_related( "taste_set", "origins").get(pk=flavor_one.pk) # This shouldn't make any database calls because of the prefetch_related with django_assert_num_queries(0): actual = serializer.dumps( {"flavor": flavor_one}, exclude_field_attributes=( "flavor.taste_set", "flavor.origins", ), ) expected = { "flavor": { "name": "name1", "label": "label1", "parent": None, "float_value": None, "decimal_value": None, "uuid": str(flavor_one.uuid), "datetime": None, "date": None, "time": None, "duration": None, "pk": 1, } } assert expected == json.loads(actual)
def test_model_many_to_many(django_assert_num_queries): flavor_one = Flavor(name="name1", label="label1") flavor_one.save() taste1 = Taste(name="Bitter1") taste1.save() taste2 = Taste(name="Bitter2") taste2.save() taste3 = Taste(name="Bitter3") taste3.save() flavor_one.taste_set.add(taste1) flavor_one.taste_set.add(taste2) flavor_one.taste_set.add(taste3) with django_assert_num_queries(2): actual = serializer.dumps(flavor_one) expected = { "name": "name1", "label": "label1", "parent": None, "float_value": None, "decimal_value": None, "uuid": str(flavor_one.uuid), "datetime": None, "date": None, "time": None, "duration": None, "pk": 1, "taste_set": [taste1.pk, taste2.pk, taste3.pk], "origins": [], } assert expected == json.loads(actual)