Skip to content

Commit

Permalink
test: improve test cases by adding another app
Browse files Browse the repository at this point in the history
  • Loading branch information
browniebroke committed Oct 17, 2023
1 parent 57aa804 commit 2792b1c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 13 deletions.
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

INSTALLED_APPS = [
"tests.testapp",
"tests.testapp2",
"remake_migrations",
# Force django_migrations creation by having an app with migrations:
"django.contrib.contenttypes",
Expand Down
41 changes: 29 additions & 12 deletions tests/test_remakemigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,31 @@
class RemakeMigrationsTests(TestCase):
@pytest.fixture(autouse=True)
def tmp_path_fixture(self, tmp_path):
migrations_module_name = "migrations" + str(time.time()).replace(".", "")
self.migrations_dir = tmp_path / migrations_module_name
self.migrations_dir.mkdir()
migrations_module_name1 = "migrations" + str(time.time()).replace(".", "")
self.testapp_mig_dir = tmp_path / migrations_module_name1
self.testapp_mig_dir.mkdir()

migrations_module_name2 = "migrations" + str(time.time()).replace(".", "")
self.testapp2_mig_dir = tmp_path / migrations_module_name2
self.testapp2_mig_dir.mkdir()

sys.path.insert(0, str(tmp_path))
try:
with override_settings(
MIGRATION_MODULES={"testapp": migrations_module_name}
MIGRATION_MODULES={
"testapp": migrations_module_name1,
"testapp2": migrations_module_name2,
}
):
yield
finally:
sys.path.pop(0)

def test_success_step_1(self):
(self.migrations_dir / "__init__.py").touch()
initial_0001 = self.migrations_dir / "0001_initial.py"
(self.testapp_mig_dir / "__init__.py").touch()
initial_0001 = self.testapp_mig_dir / "0001_initial.py"
initial_0001.write_text(EMPTY_MIGRATION)
migration_0002 = self.migrations_dir / "0002_something.py"
migration_0002 = self.testapp_mig_dir / "0002_something.py"
migration_0002.write_text(
dedent(
"""\
Expand All @@ -41,11 +49,15 @@ def test_success_step_1(self):
class Migration(migrations.Migration):
dependencies = [
('testapp', '0001_initial'),
('testapp2', '0001_initial'),
]
operations = []
"""
)
)
(self.testapp2_mig_dir / "__init__.py").touch()
initial_0001 = self.testapp2_mig_dir / "0001_initial.py"
initial_0001.write_text(EMPTY_MIGRATION)

out, err, returncode = run_command("remakemigrations", "--step", "1")

Expand All @@ -60,7 +72,7 @@ class Migration(migrations.Migration):
assert graph_json.exists()

def test_success_step_2_and_3(self):
(self.migrations_dir / "__init__.py").touch()
(self.testapp_mig_dir / "__init__.py").touch()
graph_json = Path(__file__).parent / "graph.json"
graph_json.write_text(
dedent(
Expand All @@ -82,13 +94,13 @@ def test_success_step_2_and_3(self):
assert err == ""
assert returncode == 0

dir_files = sorted(os.listdir(self.migrations_dir))
dir_files = sorted(os.listdir(self.testapp_mig_dir))
assert dir_files == [
"0001_initial.py",
"__init__.py",
]

initial_0001 = self.migrations_dir / "0001_initial.py"
initial_0001 = self.testapp_mig_dir / "0001_initial.py"
content = initial_0001.read_text()

assert "from django.db import migrations, models" in content
Expand All @@ -102,15 +114,15 @@ def test_success_step_2_and_3(self):
assert err == ""
assert returncode == 0

dir_files = sorted(os.listdir(self.migrations_dir))
dir_files = sorted(os.listdir(self.testapp_mig_dir))
today = datetime.today()
initial_remade_name = f"0001_remaked_{today:%Y%m%d}_initial.py"
assert dir_files == [
initial_remade_name,
"__init__.py",
]

initial_remade = self.migrations_dir / initial_remade_name
initial_remade = self.testapp_mig_dir / initial_remade_name
content = initial_remade.read_text()

assert "from django.db import migrations, models" in content
Expand All @@ -121,3 +133,8 @@ def test_success_step_2_and_3(self):
"('testapp', '0002_something'), "
"('testapp', '0003_other_thing')]" in content
)
assert (
" dependencies = [\n"
" ('testapp2', '__first__'),\n"
" ]\n" in content
)
5 changes: 4 additions & 1 deletion tests/testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@


class Book(models.Model):
pass
fk = models.ForeignKey(
"testapp2.Author",
on_delete=models.CASCADE,
)
Empty file added tests/testapp2/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/testapp2/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from __future__ import annotations

from django.apps import AppConfig


class TestAppConfig(AppConfig):
name = "tests.testapp2"
verbose_name = "Test App Two"
7 changes: 7 additions & 0 deletions tests/testapp2/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import annotations

from django.db import models


class Author(models.Model):
pass

0 comments on commit 2792b1c

Please sign in to comment.