-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
164 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,83 @@ | ||
from collections import defaultdict | ||
|
||
import pytest | ||
from django.db import connection | ||
from django.db.migrations.executor import MigrationExecutor | ||
|
||
import uuid | ||
|
||
@pytest.fixture() | ||
def post_migrate_to_current_state(transactional_db): | ||
""" | ||
Apply all current migrations after test has run. | ||
|
||
In migration-tests, the `transactional_db` fixture seems to fail to apply the | ||
newest migrations for all apps, leading to flaky tests. This fixture makes sure, | ||
that all apps are migrated to their most current state. | ||
""" | ||
def is_valid_uuid(val): | ||
try: | ||
yield | ||
finally: | ||
executor = MigrationExecutor(connection) | ||
migrations_dict = defaultdict(list) | ||
for app, migration in executor.loader.disk_migrations.keys(): | ||
migrations_dict[app].append(migration) | ||
|
||
migrations_list = [ | ||
(app, max(migrations)) for app, migrations in migrations_dict.items() | ||
] | ||
executor.loader.build_graph() | ||
executor.migrate(migrations_list) | ||
executor.loader.build_graph() | ||
|
||
|
||
def test_0011_file_path(post_migrate_to_current_state): | ||
executor = MigrationExecutor(connection) | ||
migrate_from = [("alexandria_core", "0010_mark")] | ||
migrate_to = [("alexandria_core", "0011_file_content")] | ||
executor.loader.build_graph() | ||
executor.migrate(migrate_from) | ||
|
||
old_apps = executor.loader.project_state(migrate_from).apps | ||
OldFile = old_apps.get_model("alexandria_core", "file") | ||
OldDocument = old_apps.get_model("alexandria_core", "document") | ||
uuid.UUID(str(val)) | ||
return True | ||
except ValueError: # pragma: no cover | ||
return False | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_tag_pk_migration(migrator): | ||
"""Ensures that the initial migration works.""" | ||
old_state = migrator.apply_initial_migration(("alexandria_core", "0010_mark")) | ||
|
||
Tag = old_state.apps.get_model("alexandria_core", "Tag") | ||
Document = old_state.apps.get_model("alexandria_core", "Document") | ||
Synonym = old_state.apps.get_model("alexandria_core", "TagSynonymGroup") | ||
|
||
synonym = Synonym.objects.create() | ||
|
||
tag1 = Tag.objects.create(slug="apple", name="Apple", tag_synonym_group=synonym) | ||
tag2 = Tag.objects.create(slug="banana", name="Banana", tag_synonym_group=synonym) | ||
tag3 = Tag.objects.create(slug="melon", name="Melon") | ||
old_ids = {tag1.pk, tag2.pk, tag3.pk} | ||
|
||
doc1 = Document.objects.create(title="Sand") | ||
doc2 = Document.objects.create(title="Dirt") | ||
|
||
doc1.tags.add(tag1, tag3) | ||
doc2.tags.add(tag2) | ||
|
||
assert Tag.objects.count() == 3 | ||
|
||
new_state = migrator.apply_tested_migration( | ||
("alexandria_core", "0012_tag_uuid_schema") | ||
) | ||
|
||
# After the initial migration is done, we can use the model state: | ||
Tag = new_state.apps.get_model("alexandria_core", "Tag") | ||
Document = new_state.apps.get_model("alexandria_core", "Document") | ||
tags = Tag.objects.all() | ||
doc1 = Document.objects.get(title="Sand") | ||
doc2 = Document.objects.get(title="Dirt") | ||
|
||
assert tags.count() == 3 | ||
assert set(tags.values_list("pk", flat=True)).intersection(old_ids) == set() | ||
|
||
assert tags.get(name="Apple").tag_synonym_group.pk == synonym.pk | ||
assert tags.get(name="Banana").tag_synonym_group.pk == synonym.pk | ||
assert tags.get(name="Melon").tag_synonym_group is None | ||
|
||
assert doc1.tags.count() == 2 | ||
assert doc2.tags.count() == 1 | ||
|
||
assert doc1.tags.filter(name="Apple").exists() | ||
assert doc1.tags.filter(name="Melon").exists() | ||
assert doc2.tags.filter(name="Banana").exists() | ||
|
||
for tag in tags: | ||
assert is_valid_uuid(tag.id) | ||
|
||
|
||
@pytest.mark.django_db() | ||
def test_0013_file_content(migrator): | ||
old_state = migrator.apply_initial_migration( | ||
("alexandria_core", "0012_tag_uuid_schema") | ||
) | ||
|
||
OldFile = old_state.apps.get_model("alexandria_core", "file") | ||
OldDocument = old_state.apps.get_model("alexandria_core", "document") | ||
doc = OldDocument.objects.create() | ||
OldFile.objects.create(name="existing_file", document=doc) | ||
|
||
executor.loader.build_graph() | ||
executor.migrate(migrate_to) | ||
new_apps = executor.loader.project_state(migrate_to).apps | ||
new_state = migrator.apply_tested_migration( | ||
("alexandria_core", "0013_file_content") | ||
) | ||
|
||
NewFile = new_apps.get_model("alexandria_core", "file") | ||
NewFile = new_state.apps.get_model("alexandria_core", "file") | ||
_file = NewFile.objects.first() | ||
assert _file.name in _file.content.url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.