Skip to content

Commit

Permalink
[#293] add migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonny Bakker committed Nov 28, 2024
1 parent 715bcbe commit 61d43d8
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/openklant/components/token/tests/test_migrations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from django.core.management import call_command
from django.db.migrations.state import StateApps
from django.test import TransactionTestCase


class BaseMigrationTest(TransactionTestCase):
app: str
migrate_from: str # The migration before the one we want to test
migrate_to: str # The migration we want to test

setting_overrides: dict = {}

old_app_state: StateApps
app_state: StateApps

def setUp(self) -> None:
"""
Setup the migration test by reversing to `migrate_from` state,
then applying the `migrate_to` state.
"""
assert self.app is not None, "You must define the `app` attribute"
assert self.migrate_from is not None, "You must define `migrate_from`"
assert self.migrate_to is not None, "You must define `migrate_to`"

# Step 1: Set up the MigrationExecutor
executor = MigrationExecutor(connection)

# Step 2: Reverse to the starting migration state
migrate_from = [(self.app, self.migrate_from)]
old_migrate_state = executor.migrate(migrate_from)

self.old_app_state = old_migrate_state.apps

def _perform_migration(self) -> None:
migrate_to = [(self.app, self.migrate_to)]

executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload the graph in case of dependency changes
executor.migrate(migrate_to)

self.apps = executor.loader.project_state(migrate_to).apps

@classmethod
def tearDownClass(cls) -> None:
super().tearDownClass()

# reset to latest migration
call_command("migrate", verbosity=0, database=connection._alias)


class TestTokenAuthUniqueness(BaseMigrationTest):
app = "token"
migrate_from = "0001_initial"
migrate_to = "0002_tokenauth_identifier_alter_tokenauth_token"

def test_migrate_tokens_to_unique_values(self):
TokenAuth = self.old_app_state.get_model("token", "TokenAuth")

TokenAuth.objects.create(
token="aa018d1c576c9dae33be1e549f739f2834ebc811",
contact_person="Person 1",
email="[email protected]",
)

TokenAuth.objects.create(
token="aa018d1c576c9dae33be1e549f739f2834ebc811",
contact_person="Person 2",
email="[email protected]",
)

TokenAuth.objects.create(
token="ab700d6bf906c2b4b42a961c529657314c6a8246",
contact_person="Other person",
email="[email protected]",
)

self._perform_migration()

TokenAuth = self.apps.get_model("token", "TokenAuth")

tokens = TokenAuth.objects.all()

self.assertEqual(tokens.count(), 3)

test_token = tokens.get(email="[email protected]")
duplicate_token = tokens.get(email="[email protected]")
unrelated_token = tokens.get(email="[email protected]")

self.assertNotEqual(
test_token.token, "aa018d1c576c9dae33be1e549f739f2834ebc811"
)
self.assertNotEqual(
duplicate_token.token, "aa018d1c576c9dae33be1e549f739f2834ebc811"
)
self.assertNotEqual(test_token.token, duplicate_token.token)

# unrelated token should be not be mutated
self.assertEqual(
unrelated_token.token, "ab700d6bf906c2b4b42a961c529657314c6a8246"
)

def test_migrate_tokens_to_unique_identifiers(self):
TokenAuth = self.old_app_state.get_model("token", "TokenAuth")

TokenAuth.objects.create(
token="aa018d1c576c9dae33be1e549f739f2834ebc811",
contact_person="Person 1",
email="[email protected]",
)

TokenAuth.objects.create(
token="ab700d6bf906c2b4b42a961c529657314c6a8246",
contact_person="Other person",
email="[email protected]",
)

self._perform_migration()

TokenAuth = self.apps.get_model("token", "TokenAuth")

tokens = TokenAuth.objects.all()

self.assertEqual(tokens.count(), 2)

first_token = tokens[0]
last_token = tokens[1]

self.assertTrue(first_token.token)
self.assertTrue(last_token.token)

self.assertNotEqual(first_token.identifier, last_token.identifier)

0 comments on commit 61d43d8

Please sign in to comment.