From 7160e75a36b9f07be2d99551838bdb323d0b142b Mon Sep 17 00:00:00 2001 From: Andrey Bondar Date: Fri, 24 May 2024 19:45:31 +0300 Subject: [PATCH] Fix tests for deprecated style pk (#1626) * Fix tests for deprecated style pk * Fix tests for deprecated style pk * Fix tests for deprecated style pk * Fix tests for deprecated style pk * Fix tests for deprecated style pk * Fix tests for deprecated style pk * Fix tests for deprecated style pk --- tests/test_basic.py | 4 ++-- tests/test_bulk.py | 30 +++++++++++++++++------------ tests/testmodels.py | 47 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/tests/test_basic.py b/tests/test_basic.py index 189897776..f09f7554f 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -23,8 +23,8 @@ async def test_basic(self): ) async def test_basic_oldstyle(self): - obj = await OldStyleModel.create(name="Test") + obj = await OldStyleModel.create(external_id=123) assert obj.pk assert OldStyleModel._meta.fields_map["id"].pk - assert OldStyleModel._meta.fields_map["name"].index + assert OldStyleModel._meta.fields_map["external_id"].index diff --git a/tests/test_bulk.py b/tests/test_bulk.py index efdbb13d0..d5392e96c 100644 --- a/tests/test_bulk.py +++ b/tests/test_bulk.py @@ -73,28 +73,34 @@ async def test_bulk_create_with_specified(self): @test.requireCapability(dialect=NotEQ("mssql")) async def test_bulk_create_mix_specified(self): + predefined_start = 40000 + predefined_end = 40150 + undefined_count = 100 + await UniqueName.bulk_create( - [UniqueName(id=id_) for id_ in range(1000, 1100)] + [UniqueName() for _ in range(100)] + [UniqueName(id=id_) for id_ in range(predefined_start, predefined_end)] + + [UniqueName() for _ in range(undefined_count)] ) all_ = await UniqueName.all().order_by("id").values("id", "name") - assert len(all_) == 200 + predefined_count = predefined_end - predefined_start + assert len(all_) == (predefined_count + undefined_count) - if all_[0]["id"] == 1000: - assert sorted(all_[:100], key=lambda x: x["id"]) == [ - {"id": id_, "name": None} for id_ in range(1000, 1100) + if all_[0]["id"] == predefined_start: + assert sorted(all_[:predefined_count], key=lambda x: x["id"]) == [ + {"id": id_, "name": None} for id_ in range(predefined_start, predefined_end) ] - inc = all_[100]["id"] - assert sorted(all_[100:], key=lambda x: x["id"]) == [ - {"id": val + inc, "name": None} for val in range(100) + inc = all_[predefined_count]["id"] + assert sorted(all_[predefined_count:], key=lambda x: x["id"]) == [ + {"id": val + inc, "name": None} for val in range(undefined_count) ] else: inc = all_[0]["id"] - assert sorted(all_[:100], key=lambda x: x["id"]) == [ - {"id": val + inc, "name": None} for val in range(100) + assert sorted(all_[:undefined_count], key=lambda x: x["id"]) == [ + {"id": val + inc, "name": None} for val in range(undefined_count) ] - assert sorted(all_[100:], key=lambda x: x["id"]) == [ - {"id": id_, "name": None} for id_ in range(1000, 1100) + assert sorted(all_[undefined_count:], key=lambda x: x["id"]) == [ + {"id": id_, "name": None} for id_ in range(predefined_start, predefined_end) ] async def test_bulk_create_uuidpk(self): diff --git a/tests/testmodels.py b/tests/testmodels.py index e6ac6d87f..f4f78d00a 100644 --- a/tests/testmodels.py +++ b/tests/testmodels.py @@ -101,7 +101,10 @@ class Event(Model): "models.Reporter", null=True ) participants: fields.ManyToManyRelation["Team"] = fields.ManyToManyField( - "models.Team", related_name="events", through="event_team", backward_key="idEvent" + "models.Team", + related_name="events", + through="event_team", + backward_key="idEvent", ) modified = fields.DatetimeField(auto_now=True) token = fields.TextField(default=generate_token) @@ -132,7 +135,10 @@ class Address(Model): street = fields.CharField(max_length=128) event: fields.OneToOneRelation[Event] = fields.OneToOneField( - "models.Event", on_delete=fields.CASCADE, related_name="address", primary_key=True + "models.Event", + on_delete=fields.CASCADE, + related_name="address", + primary_key=True, ) @@ -143,7 +149,10 @@ class Dest_null(Model): class O2O_null(Model): name = fields.CharField(max_length=64) event: fields.OneToOneNullableRelation[Event] = fields.OneToOneField( - "models.Dest_null", on_delete=fields.CASCADE, related_name="address_null", null=True + "models.Dest_null", + on_delete=fields.CASCADE, + related_name="address_null", + null=True, ) @@ -413,7 +422,10 @@ class UUIDFkRelatedNullSourceModel(Model): id = fields.UUIDField(primary_key=True, source_field="i") name = fields.CharField(max_length=50, null=True, source_field="j") model: fields.ForeignKeyNullableRelation[UUIDPkSourceModel] = fields.ForeignKeyField( - "models.UUIDPkSourceModel", related_name="children_null", source_field="k", null=True + "models.UUIDPkSourceModel", + related_name="children_null", + source_field="k", + null=True, ) class Meta: @@ -424,7 +436,10 @@ class UUIDM2MRelatedSourceModel(Model): id = fields.UUIDField(primary_key=True, source_field="e") value = fields.TextField(default="test", source_field="f") models: fields.ManyToManyRelation[UUIDPkSourceModel] = fields.ManyToManyField( - "models.UUIDPkSourceModel", related_name="peers", forward_key="e", backward_key="h" + "models.UUIDPkSourceModel", + related_name="peers", + forward_key="e", + backward_key="h", ) class Meta: @@ -620,7 +635,10 @@ class SourceFields(Model): eyedee = fields.IntField(primary_key=True, source_field="sometable_id", description="Da PK") # A regular comment chars = fields.CharField( - max_length=50, source_field="some_chars_table", db_index=True, description="Some chars" + max_length=50, + source_field="some_chars_table", + db_index=True, + description="Some chars", ) #: A docstring comment blip = fields.CharField(max_length=50, default="BLIP", source_field="da_blip") @@ -742,7 +760,10 @@ class Principal(Model): id = fields.IntField(primary_key=True) name = fields.TextField() school: fields.OneToOneRelation[School] = fields.OneToOneField( - "models.School", on_delete=fields.CASCADE, related_name="principal", to_field="id" + "models.School", + on_delete=fields.CASCADE, + related_name="principal", + to_field="id", ) @@ -780,10 +801,16 @@ class ValidatorModel(Model): max_value = fields.IntField(null=True, validators=[MaxValueValidator(20.0)]) min_value = fields.IntField(null=True, validators=[MinValueValidator(10.0)]) max_value_decimal = fields.DecimalField( - max_digits=12, decimal_places=3, null=True, validators=[MaxValueValidator(Decimal("2.0"))] + max_digits=12, + decimal_places=3, + null=True, + validators=[MaxValueValidator(Decimal("2.0"))], ) min_value_decimal = fields.DecimalField( - max_digits=12, decimal_places=3, null=True, validators=[MinValueValidator(Decimal("1.0"))] + max_digits=12, + decimal_places=3, + null=True, + validators=[MinValueValidator(Decimal("1.0"))], ) comma_separated_integer_list = fields.CharField( max_length=100, null=True, validators=[CommaSeparatedIntegerListValidator()] @@ -870,7 +897,7 @@ class Pair(Model): class OldStyleModel(Model): id = fields.IntField(pk=True) - name = fields.CharField(max_length=255, index=True) + external_id = fields.IntField(index=True) def camelize_var(var_name: str):