File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments