Skip to content

Commit 62507c8

Browse files
committed
test: test get_model_id (doesn't work yet)
1 parent dede814 commit 62507c8

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

tests/openedx_django_lib/__init__.py

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Tests for our django typing utils
3+
"""
4+
from typing import NewType, assert_type
5+
6+
from django.db import models
7+
8+
from openedx_django_lib.fields import TypedBigAutoField
9+
from openedx_django_lib.typing import get_model_id
10+
11+
12+
class Foo(models.Model):
13+
"""
14+
A model with a typed ID field
15+
"""
16+
FooID = NewType("FooID", int)
17+
type ID = FooID
18+
19+
class IDField(TypedBigAutoField[ID]):
20+
pass
21+
22+
id = IDField(primary_key=True)
23+
24+
25+
def test_get_model_id() -> None:
26+
"""
27+
Test that get_model_id behaves as expected, both at runtime and during typechecking.
28+
"""
29+
foo = Foo()
30+
31+
# Sanity checks
32+
assert_type(foo, Foo)
33+
assert_type(foo.id, Foo.ID)
34+
35+
# get_model_id on a model returns its id
36+
assert get_model_id(foo) == foo.id
37+
assert_type(get_model_id(foo), Foo.ID)
38+
39+
# get_model_id on an id returns itself
40+
assert get_model_id(foo.id) == foo.id
41+
assert_type(get_model_id(foo.id), Foo.ID)
42+

0 commit comments

Comments
 (0)