From 4a73735c9cf46f65cb3e906b47994cb661eba543 Mon Sep 17 00:00:00 2001 From: q0w <43147888+q0w@users.noreply.github.com> Date: Mon, 17 Oct 2022 15:19:29 +0300 Subject: [PATCH] Add tests --- src/django_linear_migrations/apps.py | 10 ++-- tests/test_checks.py | 36 ++++++++++--- tests/test_create_max_migration_files.py | 69 ++++++++++++++++++++++-- 3 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/django_linear_migrations/apps.py b/src/django_linear_migrations/apps.py index 30a8ec2..e8c659b 100644 --- a/src/django_linear_migrations/apps.py +++ b/src/django_linear_migrations/apps.py @@ -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 @@ -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( diff --git a/tests/test_checks.py b/tests/test_checks.py index 59f2714..39bdeb9 100644 --- a/tests/test_checks.py +++ b/tests/test_checks.py @@ -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(".", "") @@ -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() @@ -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() diff --git a/tests/test_create_max_migration_files.py b/tests/test_create_max_migration_files.py index 0471ccc..5bfef69 100644 --- a/tests/test_create_max_migration_files.py +++ b/tests/test_create_max_migration_files.py @@ -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 @@ -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() @@ -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") @@ -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") @@ -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"