Skip to content

Commit dba827f

Browse files
committed
test: Add tests on auto id generation
1 parent 99c8fb4 commit dba827f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/test_model.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,41 @@ class Meta:
174174
):
175175
with connection.schema_editor() as schema_editor:
176176
schema_editor.table_sql(MetaOptions)
177+
178+
179+
def test_model_id():
180+
"""
181+
Tests the auto-generated id added by Django.
182+
183+
"""
184+
185+
class SomeModel(CrateModel):
186+
class Meta:
187+
app_label = "ignore"
188+
189+
assert SomeModel._meta.get_field("id")
190+
assert len(SomeModel._meta.fields) == 1
191+
192+
with connection.schema_editor() as schema_editor:
193+
sql, params = schema_editor.column_sql(
194+
SomeModel, SomeModel._meta.get_field("id")
195+
)
196+
assert (
197+
sql
198+
== "INTEGER GENERATED ALWAYS AS CAST((random() * 1.0E9) AS integer) NOT NULL PRIMARY KEY"
199+
)
200+
201+
class SomeModel(CrateModel):
202+
id = models.TextField(primary_key=True)
203+
204+
class Meta:
205+
app_label = "ignore"
206+
207+
assert SomeModel._meta.get_field("id")
208+
assert len(SomeModel._meta.fields) == 1
209+
210+
with connection.schema_editor() as schema_editor:
211+
sql, params = schema_editor.column_sql(
212+
SomeModel, SomeModel._meta.get_field("id")
213+
)
214+
assert sql == "text NOT NULL PRIMARY KEY"

0 commit comments

Comments
 (0)