Repository: django/django
File: tests/serializers/test_natural.py (Python)

Code before:
            user_data = json.loads(user_data_str)
            serialized_user = user_data[0]

            self.assertEqual(
                serialized_user.get("pk"),
                user_pk,
                f"[{user.__class__.__name__}] PK must be included "
                f"in the primary object after patch.",
            )

        post_data_str = serializers.serialize(
            format,
            [post],
            indent=2,
            use_natural_foreign_keys=True,
        )

        if is_json:
            post_data = json.loads(post_data_str)
            serialized_post = post_data[0]

            self.assertEqual(
                serialized_post["fields"]["author"],
                user_pk,
                f"[{user.__class__.__name__}] FK must be serialized as PK integer"
                f", not a natural key list.",
            )

        try:
            list(serializers.deserialize(format, user_data_str))
        except Exception as e:
            self.fail(
                f"Deserialization of {user.__class__.__name__} failed in {format}: {e}"
            )

        try:
            list(serializers.deserialize(format, post_data_str))
        except Exception as e:
            self.fail(
                f"Deserialization of {post.__class__.__name__} failed in {format}: {e}"
            )


# Dynamically register tests for each serializer
register_tests(
    NaturalKeySerializerTests,
    "test_%s_natural_key_serializer",
    natural_key_serializer_test,
)
register_tests(
    NaturalKeySerializerTests, "test_%s_serializer_natural_keys", natural_key_test

Review comment (suggestion):
Only running these tests if `is_json` kind of cuts against the design of the surrounding tests, which test export & import instead of asserting on the exact output.

While I like the explicitness of this, I wonder if it would "blend in" to demonstrate a roundtrip instead. This would involve demonstrating when deserializing that objects serialized with integer pk's instead of natural keys cause pk collisions when those integer pk's already exist, but not otherwise.

Code after:
)
register_tests(
    NaturalKeySerializerTests, "test_%s_serializer_natural_keys", natural_key_test
)
register_tests(
    NaturalKeySerializerTests, "test_%s_serializer_natural_pks_mti", natural_pk_mti_test
)
register_tests(
    NaturalKeySerializerTests, "test_%s_forward_references_fks", forward_ref_fk_test
)
register_tests(
    NaturalKeySerializerTests,
    "test_%s_forward_references_fk_errors",
    forward_ref_fk_with_error_test,
)
register_tests(
    NaturalKeySerializerTests, "test_%s_forward_references_m2ms", forward_ref_m2m_test
)
register_tests(
    NaturalKeySerializerTests,
    "test_%s_forward_references_m2m_errors",
    forward_ref_m2m_with_error_test,
)
register_tests(NaturalKeySerializerTests, "test_%s_pk_with_default", pk_with_default)
register_tests(
    NaturalKeySerializerTests,
    "test_%s_fk_as_pk_natural_key_not_called",
    fk_as_pk_natural_key_not_called,
)
register_tests(
    NaturalKeySerializerTests,
    "test_%s_natural_key_opt_out",
    natural_key_opt_out_test,
)