Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
q0w committed Oct 17, 2022
1 parent 92dde76 commit 4a73735
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 16 deletions.
10 changes: 3 additions & 7 deletions src/django_linear_migrations/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from importlib import import_module, reload
from pathlib import Path
from types import ModuleType
from typing import Generator, Iterable
from typing import Generator, Iterable, cast

from django.apps import AppConfig, apps
from django.conf import settings
Expand Down Expand Up @@ -111,12 +111,8 @@ def names(self) -> set[str]:
def plan(self) -> list[tuple[str, str]]:
loader = MigrationLoader(connections[DEFAULT_DB_ALIAS])
nodes = [key for key in loader.graph.leaf_nodes() if key[0] in self.app_label]
plan = []
for node in nodes:
for migration in loader.graph.forwards_plan(node):
if migration not in plan:
plan.append(migration)
return plan
plan = loader.graph._generate_plan(nodes, at_end=True)
return cast(list[tuple[str, str]], plan)


def check_max_migration_files(
Expand Down
36 changes: 30 additions & 6 deletions tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import sys
import time
from textwrap import dedent

import pytest
from django.test import SimpleTestCase, override_settings
from django.test import TestCase, override_settings

from django_linear_migrations.apps import check_max_migration_files


class CheckMaxMigrationFilesTests(SimpleTestCase):
class CheckMaxMigrationFilesTests(TestCase):
@pytest.fixture(autouse=True)
def tmp_path_fixture(self, tmp_path):
migrations_module_name = "migrations" + str(time.time()).replace(".", "")
Expand Down Expand Up @@ -88,8 +89,24 @@ def test_dlm_E003(self):

def test_dlm_E004(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "0002_updates.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)
(self.migrations_dir / "0002_updates.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)
(self.migrations_dir / "max_migration.txt").write_text("0001_initial\n")

result = check_max_migration_files()
Expand All @@ -102,9 +119,16 @@ def test_dlm_E004(self):
)

def test_okay(self):
migrations_txt = dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "0002_updates.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(migrations_txt)
(self.migrations_dir / "0002_updates.py").write_text(migrations_txt)
(self.migrations_dir / "max_migration.txt").write_text("0002_updates\n")

result = check_max_migration_files()
Expand Down
69 changes: 66 additions & 3 deletions tests/test_create_max_migration_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import time
from io import StringIO
from textwrap import dedent

import pytest
from django.core.management import call_command
Expand Down Expand Up @@ -98,7 +99,15 @@ def test_success_dry_run(self):

def test_success(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)

out, err, returncode = self.call_command()

Expand All @@ -121,7 +130,15 @@ def test_success_already_exists(self):

def test_success_recreate(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)
(self.migrations_dir / "max_migration.txt").write_text("0001_initial\n")

out, err, returncode = self.call_command("--recreate")
Expand All @@ -143,7 +160,15 @@ def test_success_recreate_dry_run(self):

def test_success_specific_app_label(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)

out, err, returncode = self.call_command("testapp")

Expand All @@ -168,3 +193,41 @@ def test_success_ignored_app_label(self):
assert out == "No max_migration.txt files need creating.\n"
assert err == ""
assert returncode == 0

def test_success_custom_migration_name(self):
(self.migrations_dir / "__init__.py").touch()
(self.migrations_dir / "0001_initial.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
pass
"""
)
)
(self.migrations_dir / "custom_name.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [('testapp', '0001_initial')]
"""
)
)
(self.migrations_dir / "0002_updates.py").write_text(
dedent(
"""
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [('testapp', 'custom_name')]
"""
)
)

out, err, returncode = self.call_command()

assert out == "Created max_migration.txt for testapp.\n"
assert err == ""
assert returncode == 0
max_migration_txt = self.migrations_dir / "max_migration.txt"
assert max_migration_txt.read_text() == "0002_updates\n"

0 comments on commit 4a73735

Please sign in to comment.