Skip to content

Commit 0f70f74

Browse files
Improve test coverage (#43)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a06a5ec commit 0f70f74

File tree

21 files changed

+143
-191
lines changed

21 files changed

+143
-191
lines changed

.github/workflows/migrations.yml

Lines changed: 0 additions & 55 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020

2121
steps:
2222
- uses: actions/[email protected]
23+
with:
24+
submodules: migration_fixer/tests/demo
2325

2426
- name: Set up Python ${{ matrix.python-version }}
2527
uses: actions/[email protected]

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "migration_fixer/tests/demo"]
2+
path = migration_fixer/tests/demo
3+
url = [email protected]:tj-django/demo.git

Makefile

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,34 +75,32 @@ increase-version: guard-PART ## Increase project version
7575
@bump2version $(PART)
7676
@git switch -c main
7777

78-
install-wheel: ## Install wheel
78+
install-wheel: clean ## Install wheel
7979
@echo "Installing wheel..."
8080
@pip install wheel
8181

82-
install: clean requirements.txt install-wheel ## install the package to the active Python's site-packages
82+
install: requirements.txt install-wheel ## install the package to the active Python's site-packages
8383
@pip install -r requirements.txt
8484

85-
install-dev: clean requirements_dev.txt install-wheel ## Install local dev packages
85+
install-dev: requirements_dev.txt install-wheel ## Install local dev packages
8686
@pip install -e .'[development]' -r requirements_dev.txt
8787

88-
install-docs: clean install-wheel
88+
install-docs: install-wheel
8989
@pip install -e .'[docs]'
9090

91-
install-test: clean install-wheel
91+
install-test: install-wheel
9292
@pip install -e .'[test]'
9393

94-
install-lint: clean install-wheel
94+
install-lint: install-wheel
9595
@pip install -e .'[lint]'
9696

97-
install-deploy: clean install-wheel
97+
install-deploy: install-wheel
9898
@pip install -e .'[deploy]'
9999

100-
migrations: ## Run django migrations without user input.
101-
@echo "Generating migrations..."
102-
@python manage.py makemigrations --fix --no-input
100+
test: install-test
101+
@pytest --basetemp={envtmpdir}
103102

104-
migrate: ## Run django migrate without user input
105-
@echo "Applying migrations..."
106-
@python manage.py migrate --no-input
103+
migrations:
104+
@python manage.py makemigrations
107105

108106
.PHONY: clean clean-test clean-pyc clean-build docs help install-wheel install-docs install-dev install install-lint install-test install-deploy migrations

demo/apps.py

Lines changed: 0 additions & 6 deletions
This file was deleted.

demo/migrations/0001_initial.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

demo/models.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

django_migration_fixer/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"django.contrib.auth",
4646
"django.contrib.contenttypes",
4747
"migration_fixer",
48-
"demo",
48+
"migration_fixer.tests.demo",
4949
]
5050

5151
# Database

migration_fixer/management/commands/makemigrations.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77

88
from django.apps import apps
99
from django.conf import settings
10-
from django.core.management.base import CommandError, no_translations
10+
from django.core.management.base import CommandError
1111
from django.core.management.commands.makemigrations import Command as BaseCommand
1212
from django.db import DEFAULT_DB_ALIAS, connections, router
1313
from django.db.migrations.loader import MigrationLoader
1414

15-
from migration_fixer.utils import fix_migration, fix_numbered_migration, run_command
15+
from migration_fixer.utils import (
16+
fix_migration,
17+
fix_numbered_migration,
18+
no_translations,
19+
run_command,
20+
)
1621

1722

1823
class Command(BaseCommand):
24+
success_msg = "Successfully fixed migrations."
25+
1926
def add_arguments(self, parser):
2027
parser.add_argument(
2128
"--fix",
@@ -199,25 +206,30 @@ def handle(self, *app_labels, **options):
199206

200207
seed_split = last_remote_filename.split("_")
201208

202-
if (
203-
seed_split
204-
and len(seed_split) > 1
205-
and str(seed_split[0]).isdigit()
206-
):
207-
fix_numbered_migration(
208-
app_label=app_label,
209-
migration_path=migration_path,
210-
seed=int(seed_split[0]),
211-
start_name=last_remote_filename,
212-
changed_files=changed_files,
213-
)
209+
try:
210+
if (
211+
seed_split
212+
and len(seed_split) > 1
213+
and str(seed_split[0]).isdigit()
214+
):
215+
fix_numbered_migration(
216+
app_label=app_label,
217+
migration_path=migration_path,
218+
seed=int(seed_split[0]),
219+
start_name=last_remote_filename,
220+
changed_files=changed_files,
221+
)
222+
else:
223+
fix_migration(
224+
app_label=app_label,
225+
migration_path=migration_path,
226+
start_name=last_remote_filename,
227+
changed_files=changed_files,
228+
)
229+
except (ValueError, IndexError, TypeError) as e:
230+
self.stderr.write(f"Error: {e}")
214231
else:
215-
fix_migration(
216-
app_label=app_label,
217-
migration_path=migration_path,
218-
start_name=last_remote_filename,
219-
changed_files=changed_files,
220-
)
232+
self.stdout.write(self.success_msg)
221233

222234
else:
223235
return super(Command, self).handle(*app_labels, **options)

migration_fixer/tests/base.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,23 @@
99

1010
class BaseCommandTestCase(with_metaclass(abc.ABCMeta, TestCase)):
1111
cmd_class: Type[BaseCommand]
12-
cmd_name = None
1312
expected_output: str
1413
base_kwargs = {"verbosity": 0}
1514

16-
@classmethod
17-
@abc.abstractmethod
18-
def setup_command_data(cls, *arg, **kwargs):
19-
pass
20-
2115
@classmethod
2216
def setup_command(cls, *args, **kwargs):
23-
cmd = cls.cmd_name
24-
25-
if cls.cmd_class is not None and issubclass(cls.cmd_class, BaseCommand):
26-
cmd = cls.cmd_class()
17+
cmd = cls.cmd_class()
2718

28-
if cmd is not None:
29-
out = StringIO()
19+
out = StringIO()
3020

31-
kwargs["stdout"] = out
32-
kwargs.update(cls.base_kwargs)
21+
kwargs["stdout"] = out
22+
kwargs.update(cls.base_kwargs)
3323

34-
call_command(cmd, *args, **kwargs)
24+
call_command(cmd, *args, **kwargs)
3525

36-
output = out.getvalue()
26+
output = out.getvalue()
3727

38-
if cls.expected_output is not None and cls.expected_output not in output:
39-
raise AssertionError(
40-
f"Expected: {repr(cls.expected_output)} != {repr(output)}",
41-
)
28+
if cls.expected_output is not None and cls.expected_output not in output:
29+
raise AssertionError(
30+
f"Expected: {repr(cls.expected_output)} != {repr(output)}",
31+
)

0 commit comments

Comments
 (0)