From 08688bd7dd8dfa218ecff03d6c94a753b1be8e59 Mon Sep 17 00:00:00 2001 From: Willem Van Onsem Date: Tue, 16 Aug 2022 07:09:11 +0200 Subject: [PATCH] Refs #33916 -- Added tests for serialization of enum.Flag in migrations. --- tests/migrations/test_writer.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 6ec6d938b9e..bad8775d4eb 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -80,6 +80,11 @@ class IntEnum(enum.IntEnum): B = 2 +class IntFlagEnum(enum.IntFlag): + A = 1 + B = 2 + + class OperationWriterTests(SimpleTestCase): def test_empty_signature(self): operation = custom_migration_operations.operations.TestOperation() @@ -382,6 +387,33 @@ class WriterTests(SimpleTestCase): "default=migrations.test_writer.IntEnum['A'])", ) + def test_serialize_enum_flags(self): + self.assertSerializedResultEqual( + IntFlagEnum.A, + ( + "migrations.test_writer.IntFlagEnum['A']", + {"import migrations.test_writer"}, + ), + ) + self.assertSerializedResultEqual( + IntFlagEnum.B, + ( + "migrations.test_writer.IntFlagEnum['B']", + {"import migrations.test_writer"}, + ), + ) + field = models.IntegerField( + default=IntFlagEnum.A, choices=[(m.value, m) for m in IntFlagEnum] + ) + string = MigrationWriter.serialize(field)[0] + self.assertEqual( + string, + "models.IntegerField(choices=[" + "(1, migrations.test_writer.IntFlagEnum['A']), " + "(2, migrations.test_writer.IntFlagEnum['B'])], " + "default=migrations.test_writer.IntFlagEnum['A'])", + ) + def test_serialize_choices(self): class TextChoices(models.TextChoices): A = "A", "A value"