Skip to content

Commit e6a3201

Browse files
committed
test: test get_model_id
1 parent dede814 commit e6a3201

3 files changed

Lines changed: 64 additions & 3 deletions

File tree

tests/openedx_django_lib/__init__.py

Whitespace-only changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
Tests for our django typing utils
3+
"""
4+
from typing import assert_type
5+
6+
from tests.test_django_app.models import MyTypedModel, RelatedTypedModel
7+
8+
from openedx_django_lib.typing import get_model_id
9+
10+
11+
def test_get_model_id() -> None:
12+
"""
13+
Test that get_model_id behaves as expected, both at runtime and during typechecking.
14+
"""
15+
my_model = MyTypedModel()
16+
related_model = RelatedTypedModel(my_model=my_model)
17+
18+
# Sanity checks
19+
assert_type(my_model, MyTypedModel)
20+
assert_type(my_model.id, MyTypedModel.ID)
21+
assert_type(related_model.my_model, MyTypedModel)
22+
assert_type(related_model.my_model.id, MyTypedModel.ID)
23+
assert_type(related_model.my_model_id, MyTypedModel.ID)
24+
25+
# get_model_id on a model returns its id
26+
assert get_model_id(my_model) == my_model.id
27+
assert_type(get_model_id(my_model), MyTypedModel.ID)
28+
assert get_model_id(related_model.my_model) == my_model.id
29+
assert_type(get_model_id(related_model.my_model), MyTypedModel.ID)
30+
31+
# get_model_id on an id returns itself
32+
assert get_model_id(my_model.id) == my_model.id
33+
assert_type(get_model_id(my_model.id), MyTypedModel.ID)
34+
assert get_model_id(related_model.my_model.id) == my_model.id
35+
assert_type(get_model_id(related_model.my_model.id), MyTypedModel.ID)

tests/test_django_app/models.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
"""
22
Models that are only for use in tests.
3-
4-
These models are specifically for testing the `containers` API.
53
"""
64

7-
from typing import override
5+
from typing import override, NewType
86

97
from django.core.exceptions import ValidationError
108
from django.db import models
@@ -16,6 +14,34 @@
1614
PublishableEntityMixin,
1715
PublishableEntityVersionMixin,
1816
)
17+
from openedx_django_lib.fields import TypedBigAutoField
18+
19+
20+
class MyTypedModel(models.Model):
21+
"""
22+
A model with nothing but a typed ID field.
23+
"""
24+
MyTypedModelID = NewType("MyTypedModelID", int)
25+
type ID = MyTypedModelID
26+
27+
class IDField(TypedBigAutoField[ID]):
28+
pass
29+
30+
id = IDField(primary_key=True)
31+
32+
33+
class RelatedTypedModel(models.Model):
34+
"""
35+
A model with nothing but a typed ID field and an FK to another typed model.
36+
"""
37+
MyRelatedModelID = NewType("MyRelatedModelID", int)
38+
type ID = MyRelatedModelID
39+
40+
class IDField(TypedBigAutoField[ID]):
41+
pass
42+
43+
id = IDField(primary_key=True)
44+
my_model = models.ForeignKey(MyTypedModel, on_delete=models.CASCADE)
1945

2046

2147
class TestEntity(PublishableEntityMixin):

0 commit comments

Comments
 (0)