Skip to content

Commit 9181ed1

Browse files
committed
✅ Add tests for token model migration to fix coverage
1 parent f0d2010 commit 9181ed1

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/objecttypes/token/tests/test_migrations.py

+62
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import datetime
2+
13
from django.core.management import call_command
24
from django.db import connection
35
from django.db.migrations.executor import MigrationExecutor
@@ -50,6 +52,66 @@ def tearDownClass(cls) -> None:
5052
call_command("migrate", verbosity=0, database=connection._alias)
5153

5254

55+
class SwitchToNewTokenAuthModelTestCase(BaseMigrationTest):
56+
app = "token"
57+
migrate_from = "0005_tokenauth"
58+
migrate_to = "0006_copy_token_auth"
59+
60+
def test_migrate_to_new_model(self):
61+
OldTokenAuth = self.old_app_state.get_model("token", "OldTokenAuth")
62+
63+
token = OldTokenAuth.objects.create(
64+
token="foo",
65+
contact_person="john doe",
66+
email="john@example.com",
67+
organization="ACME",
68+
last_modified=datetime(2024, 1, 1),
69+
created=datetime(2024, 1, 1),
70+
application="Foo",
71+
administration="Bar",
72+
)
73+
74+
self.assertEqual(token.pk, "foo")
75+
76+
self._perform_migration()
77+
78+
TokenAuth = self.apps.get_model("token", "TokenAuth")
79+
token = TokenAuth.objects.get()
80+
81+
self.assertTrue(isinstance(token.pk, int))
82+
self.assertEqual(token.token, "foo")
83+
84+
85+
class SwitchToOldTokenAuthModelTestCase(BaseMigrationTest):
86+
app = "token"
87+
migrate_from = "0006_copy_token_auth"
88+
migrate_to = "0005_tokenauth"
89+
90+
def test_migrate_to_old_model(self):
91+
TokenAuth = self.old_app_state.get_model("token", "TokenAuth")
92+
93+
token = TokenAuth.objects.create(
94+
token="foo",
95+
contact_person="john doe",
96+
email="john@example.com",
97+
organization="ACME",
98+
last_modified=datetime(2024, 1, 1),
99+
created=datetime(2024, 1, 1),
100+
application="Foo",
101+
administration="Bar",
102+
)
103+
104+
self.assertTrue(isinstance(token.pk, int))
105+
106+
self._perform_migration()
107+
108+
OldTokenAuth = self.apps.get_model("token", "OldTokenAuth")
109+
token = OldTokenAuth.objects.get()
110+
111+
self.assertEqual(token.pk, "foo")
112+
self.assertEqual(token.token, "foo")
113+
114+
53115
class TestTokenAuthUniqueness(BaseMigrationTest):
54116
app = "token"
55117
migrate_from = "0008_alter_tokenauth_token"

0 commit comments

Comments
 (0)