Repository: django/django
File: tests/composite_pk/test_models.py (Python)

Code before:
        test_cases = (
            # 1, 1, {}
            ({"tenant": self.tenant_1, "id": 1}, {}, (e_tenant_and_id, e_id)),
            ({"tenant_id": self.tenant_1.id, "id": 1}, {}, (e_tenant_and_id, e_id)),
            ({"pk": (self.tenant_1.id, 1)}, {}, (e_tenant_and_id, e_id)),
            # 2, 1, {}
            ({"tenant": self.tenant_2, "id": 1}, {}, (e_id,)),
            ({"tenant_id": self.tenant_2.id, "id": 1}, {}, (e_id,)),
            ({"pk": (self.tenant_2.id, 1)}, {}, (e_id,)),
            # 1, 1, {"tenant"}
            ({"tenant": self.tenant_1, "id": 1}, {"tenant"}, (e_id,)),
            ({"tenant_id": self.tenant_1.id, "id": 1}, {"tenant"}, (e_id,)),
            ({"pk": (self.tenant_1.id, 1)}, {"tenant"}, (e_id,)),
        )

        for kwargs, exclude, messages in test_cases:
            with self.subTest(kwargs):
                with self.assertRaises(ValidationError) as ctx:
                    kwargs["email"] = "user0004@example.com"
                    User(**kwargs).full_clean(exclude=exclude)

                self.assertSequenceEqual(ctx.exception.messages, messages)

    def test_full_clean_update(self):
        with self.assertNumQueries(4):
            self.comment_1.full_clean()

    def test_field_conflicts(self):
        test_cases = (
            ({"pk": (1, 1), "id": 2}, (1, 1)),
            ({"id": 2, "pk": (1, 1)}, (1, 1)),
            ({"pk": (1, 1), "tenant_id": 2}, (1, 1)),
            ({"tenant_id": 2, "pk": (1, 1)}, (1, 1)),
            ({"pk": (2, 2), "tenant_id": 3, "id": 4}, (2, 2)),
            ({"tenant_id": 3, "id": 4, "pk": (2, 2)}, (2, 2)),
        )

        for kwargs, pk in test_cases:
            with self.subTest(kwargs=kwargs):
                user = User(**kwargs)
                self.assertEqual(user.pk, pk)

    def test_validate_unique(self):
        user = User.objects.get(pk=self.user_1.pk)
        user.id = None

        with self.assertRaises(ValidationError) as ctx:
            user.validate_unique()

        self.assertSequenceEqual(
            ctx.exception.messages, ("User with this Email already exists.",)

Review comment (suggestion):
At a glance it's not clear why this would be 4, so perhaps better:
```py
    def test_full_clean_update(self):
        with self.assertNumQueries(1):
            self.comment_1.full_clean(validate_constraints=False)
        with self.assertNumQueries(4):
            self.comment_1.full_clean()
```

Code after:
        test_cases = (
            # 1, 1, {}
            ({"tenant": self.tenant_1, "id": 1}, {}, (e_tenant_and_id, e_id)),
            ({"tenant_id": self.tenant_1.id, "id": 1}, {}, (e_tenant_and_id, e_id)),
            ({"pk": (self.tenant_1.id, 1)}, {}, (e_tenant_and_id, e_id)),
            # 2, 1, {}
            ({"tenant": self.tenant_2, "id": 1}, {}, (e_id,)),
            ({"tenant_id": self.tenant_2.id, "id": 1}, {}, (e_id,)),
            ({"pk": (self.tenant_2.id, 1)}, {}, (e_id,)),
            # 1, 1, {"tenant"}
            ({"tenant": self.tenant_1, "id": 1}, {"tenant"}, (e_id,)),
            ({"tenant_id": self.tenant_1.id, "id": 1}, {"tenant"}, (e_id,)),
            ({"pk": (self.tenant_1.id, 1)}, {"tenant"}, (e_id,)),
        )

        for kwargs, exclude, messages in test_cases:
            with self.subTest(kwargs):
                with self.assertRaises(ValidationError) as ctx:
                    kwargs["email"] = "user0004@example.com"
                    User(**kwargs).full_clean(exclude=exclude)

                self.assertSequenceEqual(ctx.exception.messages, messages)

    def test_full_clean_update(self):
        with self.assertNumQueries(1):
            self.comment_1.full_clean()

    def test_field_conflicts(self):
        test_cases = (
            ({"pk": (1, 1), "id": 2}, (1, 1)),
            ({"id": 2, "pk": (1, 1)}, (1, 1)),
            ({"pk": (1, 1), "tenant_id": 2}, (1, 1)),
            ({"tenant_id": 2, "pk": (1, 1)}, (1, 1)),
            ({"pk": (2, 2), "tenant_id": 3, "id": 4}, (2, 2)),
            ({"tenant_id": 3, "id": 4, "pk": (2, 2)}, (2, 2)),
        )

        for kwargs, pk in test_cases:
            with self.subTest(kwargs=kwargs):
                user = User(**kwargs)
                self.assertEqual(user.pk, pk)

    def test_validate_unique(self):
        user = User.objects.get(pk=self.user_1.pk)
        user.id = None

        with self.assertRaises(ValidationError) as ctx:
            user.validate_unique()

        self.assertSequenceEqual(
            ctx.exception.messages, ("User with this Email already exists.",)