From 6976bb7c78853bd080916b64eaa902d248ef7d2d Mon Sep 17 00:00:00 2001 From: Samuel Paccoud - DINUM Date: Fri, 28 Feb 2025 07:54:12 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85(backend)=20fix=20migration=20test=20u?= =?UTF-8?q?sing=20model=20factory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Migration tests should not import and use factories or models directly from the code because they would not be in sync with the database in the state that each state needs to test it. Instead the migrator object passed as argument allows us to retrieve a minimal version of the models in sync with the state of the database that we are testing. What we get is a minimal model and we need to simulate all the methods that we could have on the real model and that are needed for testing. --- .../test_0018_update_blank_title.py | 35 -------------- ...test_migrations_0018_update_blank_title.py | 47 +++++++++++++++++++ 2 files changed, 47 insertions(+), 35 deletions(-) delete mode 100644 src/backend/core/tests/migrations/test_0018_update_blank_title.py create mode 100644 src/backend/core/tests/migrations/test_migrations_0018_update_blank_title.py diff --git a/src/backend/core/tests/migrations/test_0018_update_blank_title.py b/src/backend/core/tests/migrations/test_0018_update_blank_title.py deleted file mode 100644 index 1ac36c6e..00000000 --- a/src/backend/core/tests/migrations/test_0018_update_blank_title.py +++ /dev/null @@ -1,35 +0,0 @@ -import pytest - -from core import factories - - -@pytest.mark.django_db -def test_update_blank_title_migration(migrator): - """ - Test that the migration fixes the titles of documents that are - "Untitled document", "Unbenanntes Dokument" or "Document sans titre" - """ - migrator.apply_initial_migration(("core", "0017_add_fields_for_soft_delete")) - - english_doc = factories.DocumentFactory(title="Untitled document") - german_doc = factories.DocumentFactory(title="Unbenanntes Dokument") - french_doc = factories.DocumentFactory(title="Document sans titre") - other_doc = factories.DocumentFactory(title="My document") - - assert english_doc.title == "Untitled document" - assert german_doc.title == "Unbenanntes Dokument" - assert french_doc.title == "Document sans titre" - assert other_doc.title == "My document" - - # Apply the migration - migrator.apply_tested_migration(("core", "0018_update_blank_title")) - - english_doc.refresh_from_db() - german_doc.refresh_from_db() - french_doc.refresh_from_db() - other_doc.refresh_from_db() - - assert english_doc.title == None - assert german_doc.title == None - assert french_doc.title == None - assert other_doc.title == "My document" diff --git a/src/backend/core/tests/migrations/test_migrations_0018_update_blank_title.py b/src/backend/core/tests/migrations/test_migrations_0018_update_blank_title.py new file mode 100644 index 00000000..192103f4 --- /dev/null +++ b/src/backend/core/tests/migrations/test_migrations_0018_update_blank_title.py @@ -0,0 +1,47 @@ +import pytest + +from core import models + + +@pytest.mark.django_db +def test_update_blank_title_migration(migrator): + """ + Test that the migration fixes the titles of documents that are + "Untitled document", "Unbenanntes Dokument" or "Document sans titre" + """ + old_state = migrator.apply_initial_migration( + ("core", "0017_add_fields_for_soft_delete") + ) + OldDocument = old_state.apps.get_model("core", "Document") + + old_english_doc = OldDocument.objects.create( + title="Untitled document", depth=1, path="0000001" + ) + old_german_doc = OldDocument.objects.create( + title="Unbenanntes Dokument", depth=1, path="0000002" + ) + old_french_doc = OldDocument.objects.create( + title="Document sans titre", depth=1, path="0000003" + ) + old_other_doc = OldDocument.objects.create( + title="My document", depth=1, path="0000004" + ) + + assert old_english_doc.title == "Untitled document" + assert old_german_doc.title == "Unbenanntes Dokument" + assert old_french_doc.title == "Document sans titre" + assert old_other_doc.title == "My document" + + # Apply the migration + new_state = migrator.apply_tested_migration(("core", "0018_update_blank_title")) + NewDocument = new_state.apps.get_model("core", "Document") + + new_english_doc = NewDocument.objects.get(pk=old_english_doc.pk) + new_german_doc = NewDocument.objects.get(pk=old_german_doc.pk) + new_french_doc = NewDocument.objects.get(pk=old_french_doc.pk) + new_other_doc = NewDocument.objects.get(pk=old_other_doc.pk) + + assert new_english_doc.title == None + assert new_german_doc.title == None + assert new_french_doc.title == None + assert new_other_doc.title == "My document"