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

Code before:
        Comment.objects.create(tenant=self.tenant, id=2)
        with self.assertNumQueries(1):
            comments = list(Comment.objects.select_related("user").order_by("pk"))
            self.assertEqual(len(comments), 2)
            self.assertEqual(comments[0].user, self.user)
            self.assertIsNone(comments[1].user)

    def test_model_forms(self):
        fields = ["tenant", "id", "user_id", "text", "integer"]
        self.assertEqual(list(CommentForm.base_fields), fields)

        form = modelform_factory(Comment, fields="__all__")
        self.assertEqual(list(form().fields), fields)

        with self.assertRaisesMessage(
            FieldError, "Unknown field(s) (pk) specified for Comment"
        ):
            self.assertIsNone(modelform_factory(Comment, fields=["pk"]))

    def test_totally_ordered(self):
        """
        QuerySet.totally_ordered returns True when ordering by all fields of a
        composite primary key and False when ordering by a subset.
        """
        qs_ordered = Token.objects.order_by("tenant_id", "id")
        self.assertTrue(qs_ordered.totally_ordered)
        qs_partial = Token.objects.order_by("tenant_id")
        self.assertFalse(qs_partial.totally_ordered)


class CompositePKFixturesTests(TestCase):
    fixtures = ["tenant"]

    def test_objects(self):
        tenant_1, tenant_2, tenant_3 = Tenant.objects.order_by("pk")
        self.assertEqual(tenant_1.id, 1)
        self.assertEqual(tenant_1.name, "Tenant 1")
        self.assertEqual(tenant_2.id, 2)
        self.assertEqual(tenant_2.name, "Tenant 2")
        self.assertEqual(tenant_3.id, 3)
        self.assertEqual(tenant_3.name, "Tenant 3")

        user_1, user_2, user_3, user_4 = User.objects.order_by("pk")
        self.assertEqual(user_1.id, 1)
        self.assertEqual(user_1.tenant_id, 1)
        self.assertEqual(user_1.pk, (user_1.tenant_id, user_1.id))
        self.assertEqual(user_1.email, "user0001@example.com")
        self.assertEqual(user_2.id, 2)
        self.assertEqual(user_2.tenant_id, 1)
        self.assertEqual(user_2.pk, (user_2.tenant_id, user_2.id))
        self.assertEqual(user_2.email, "user0002@example.com")

Review comment (suggestion):
Here and just below, let's prefer `assertIs` (from the style guidelines in the docs).

Code after:
        Comment.objects.create(tenant=self.tenant, id=2)
        with self.assertNumQueries(1):
            comments = list(Comment.objects.select_related("user").order_by("pk"))
            self.assertEqual(len(comments), 2)
            self.assertEqual(comments[0].user, self.user)
            self.assertIsNone(comments[1].user)

    def test_model_forms(self):
        fields = ["tenant", "id", "user_id", "text", "integer"]
        self.assertEqual(list(CommentForm.base_fields), fields)

        form = modelform_factory(Comment, fields="__all__")
        self.assertEqual(list(form().fields), fields)

        with self.assertRaisesMessage(
            FieldError, "Unknown field(s) (pk) specified for Comment"
        ):
            self.assertIsNone(modelform_factory(Comment, fields=["pk"]))

    def test_totally_ordered(self):
        """
        QuerySet.totally_ordered returns True when ordering by all fields of a
        composite primary key and False when ordering by a subset.
        """
        qs_ordered = Token.objects.order_by("tenant_id", "id")
        self.assertIs(qs_ordered.totally_ordered, True)
        qs_partial = Token.objects.order_by("tenant_id")
        self.assertIs(qs_partial.totally_ordered, False)


class CompositePKFixturesTests(TestCase):
    fixtures = ["tenant"]

    def test_objects(self):
        tenant_1, tenant_2, tenant_3 = Tenant.objects.order_by("pk")
        self.assertEqual(tenant_1.id, 1)
        self.assertEqual(tenant_1.name, "Tenant 1")
        self.assertEqual(tenant_2.id, 2)
        self.assertEqual(tenant_2.name, "Tenant 2")
        self.assertEqual(tenant_3.id, 3)
        self.assertEqual(tenant_3.name, "Tenant 3")

        user_1, user_2, user_3, user_4 = User.objects.order_by("pk")
        self.assertEqual(user_1.id, 1)
        self.assertEqual(user_1.tenant_id, 1)
        self.assertEqual(user_1.pk, (user_1.tenant_id, user_1.id))
        self.assertEqual(user_1.email, "user0001@example.com")
        self.assertEqual(user_2.id, 2)
        self.assertEqual(user_2.tenant_id, 1)
        self.assertEqual(user_2.pk, (user_2.tenant_id, user_2.id))
        self.assertEqual(user_2.email, "user0002@example.com")