Skip to content

Commit

Permalink
Add unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamghill authored and lorinkoz committed Oct 2, 2020
1 parent d133fc8 commit 5808d54
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion dunm_sandbox/tests/test_command.py
@@ -1,6 +1,6 @@
from io import StringIO
from unittest.mock import patch

from django.conf import settings
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase, TransactionTestCase, override_settings
Expand Down Expand Up @@ -57,6 +57,34 @@ def test_migration_parents(self):
)


class UnmigrateCommandCleanTestCase(TransactionTestCase):
"""
Tests 'unmigrate' management command with --clean
"""

@patch("os.remove")
@override_settings(DEBUG=True) # Django always uses DEBUG=False, need to skip the debug check
def test_full_unmigrate_by_commit(self, mocked_remove):
unmigrate = UnmigrateCommand()
unmigrate.from_argv = True # Need to skip argv check
for commit, expected_migrations in COMMITS.items():
expected_migrations.sort() # We know tuple-sorting our migrations yields chronological order
expected_parents = []
if expected_migrations:
expected_parents = PARENTS[expected_migrations[0]]
with StringIO() as stdout:
call_command(unmigrate, commit, clean=True, stdout=stdout)
stdout.seek(0)
stdout_str = stdout.read()
self.assertTrue(not expected_parents or "Unapplying " in stdout_str)
self.assertTrue(not expected_parents or "Remove " in stdout_str)
self.assertTrue(mocked_remove.called)
with StringIO() as stdout:
call_command("migrate", stdout=stdout)
stdout.seek(0)
self.assertTrue(not expected_parents or "Applying" in stdout.read())


class UnmigrateCommandTestCase(TransactionTestCase): # In order to test migrations, must be TransactionTestCase
"""
Tests 'unmigrate' management command
Expand Down

0 comments on commit 5808d54

Please sign in to comment.