Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yelinz committed Jan 10, 2024
1 parent b753c18 commit bdc3b06
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 308 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def migrate_file_references(apps, schema_editor):

class Migration(migrations.Migration):
dependencies = [
("alexandria_core", "0010_mark"),
("alexandria_core", "0012_tag_uuid_schema"),
]

operations = [
Expand Down
179 changes: 43 additions & 136 deletions alexandria/core/tests/snapshots/snap_test_api.py

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion alexandria/core/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def test_api_create(fixture, admin_client, viewset, snapshot):

if viewset.base_name in ["category"]:
with pytest.raises(NotImplementedError):
response = admin_client.post(url, data=json.loads(data))
response = admin_client.post(url, data=data)
else:
with CaptureQueriesContext(connection) as context:
response = admin_client.post(url, data=data, **opts)
Expand Down
65 changes: 0 additions & 65 deletions alexandria/core/tests/test_migration.py

This file was deleted.

115 changes: 73 additions & 42 deletions alexandria/core/tests/test_migrations.py
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
2 changes: 0 additions & 2 deletions alexandria/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,11 @@ def update(self, request, *args, **kwargs):
class FileViewSet(
PermissionViewMixin,
VisibilityViewMixin,
PermissionViewMixin,
AutoPrefetchMixin,
PreloadIncludesMixin,
RelatedMixin,
CreateModelMixin,
RetrieveModelMixin,
DestroyModelMixin,
ListModelMixin,
DestroyModelMixin,
GenericViewSet,
Expand Down
1 change: 1 addition & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
volumes:
- ./:/app
- uploads:/media
- uploads:/media/uploads
depends_on:
- media-files
command:
Expand Down
76 changes: 38 additions & 38 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit bdc3b06

Please sign in to comment.