Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests for deprecated style pk #1626

Merged
merged 7 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
30 changes: 18 additions & 12 deletions tests/test_bulk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
47 changes: 37 additions & 10 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)


Expand All @@ -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,
)


Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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",
)


Expand Down Expand Up @@ -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()]
Expand Down Expand Up @@ -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):
Expand Down