From 15b2fbb9e153598796fd8a34cf2ab84e4eeef7d5 Mon Sep 17 00:00:00 2001 From: David Wobrock Date: Thu, 30 Nov 2017 17:44:12 +0100 Subject: [PATCH] Add tests from command line directly * Test options * Syntax error fix * Ajust the commit id for a test (was wrongly set) --- django_migration_linter/migration_linter.py | 4 +- tests/functional/test_cmd_line_call.py | 102 +++++++++++++++++++- tests/functional/test_migration_linter.py | 8 +- 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 983c7a6..0539f56 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -56,7 +56,7 @@ class MigrationLinter(object): if self.should_ignore_migration(app_name, migration_name): print('IGNORE') - self.nb_ignore += 1 + self.nb_ignored += 1 return sql_statements = self.get_sql( @@ -105,7 +105,7 @@ class MigrationLinter(object): self.nb_total, self.nb_valid, self.nb_erroneous, - self.nb_ignore)) + self.nb_ignored)) @property def has_errors(self): diff --git a/tests/functional/test_cmd_line_call.py b/tests/functional/test_cmd_line_call.py index c102528..87b88de 100644 --- a/tests/functional/test_cmd_line_call.py +++ b/tests/functional/test_cmd_line_call.py @@ -1,4 +1,104 @@ import unittest +from subprocess import Popen, PIPE +from django_migration_linter import utils +from tests import fixtures + class CallLinterFromCommandLineTest(unittest.TestCase): - pass + def test_call_linter_cmd_line_working(self): + cmd = 'django-migration-linter {0}'.format(fixtures.CORRECT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + self.assertTrue(lines[0].endswith('OK')) + self.assertTrue(lines[1].endswith('OK')) + self.assertTrue(lines[2].endswith('OK')) + + def test_call_linter_cmd_line_errors(self): + cmd = 'django-migration-linter {0}'.format( + fixtures.ADD_NOT_NULL_COLUMN_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertNotEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + print(lines) + self.assertTrue(lines[0].endswith('OK')) + self.assertTrue(lines[1].endswith('ERR')) + self.assertTrue('RENAMING tables' in lines[2]) + + def test_call_linter_cmd_line_exclude_apps(self): + cmd = 'django-migration-linter {0} --exclude-apps test_app2'.format( + fixtures.CORRECT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + self.assertTrue(lines[0].endswith('OK')) + self.assertTrue(lines[1].endswith('OK')) + self.assertTrue(lines[2].endswith('IGNORE')) + + def test_call_linter_cmd_line_include_apps(self): + cmd = 'django-migration-linter {0} --include-apps test_app2'.format( + fixtures.CORRECT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + self.assertTrue(lines[0].endswith('IGNORE')) + self.assertTrue(lines[1].endswith('IGNORE')) + self.assertTrue(lines[2].endswith('OK')) + + def test_call_linter_cmd_line_ignore_name(self): + cmd = 'django-migration-linter {0} --ignore-name 0001_initial'.format( + fixtures.CORRECT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + self.assertTrue(lines[0].endswith('IGNORE')) + self.assertTrue(lines[1].endswith('OK')) + self.assertTrue(lines[2].endswith('OK')) + + def test_call_linter_cmd_line_ignore_name_contains(self): + cmd = 'django-migration-linter {0} --ignore-name-contains 0001'.format( + fixtures.CORRECT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 5) + self.assertTrue(lines[0].endswith('IGNORE')) + self.assertTrue(lines[1].endswith('OK')) + self.assertTrue(lines[2].endswith('IGNORE')) + + def test_call_linter_cmd_line_git_id(self): + cmd = 'django-migration-linter {0} d7125d5f4f0cc9623f670a66c54f131acc50032d'.format( + fixtures.MULTI_COMMIT_PROJECT) + fixtures.prepare_git_project(fixtures.MULTI_COMMIT_PROJECT) + + process = Popen( + cmd, shell=True, stdout=PIPE, stderr=PIPE) + process.wait() + self.assertEqual(process.returncode, 0) + lines = list(map(utils.clean_bytes_to_str, process.stdout.readlines())) + self.assertEqual(len(lines), 3) + self.assertTrue(lines[0].endswith('OK')) + self.assertTrue(lines[1].startswith('*** Summary')) diff --git a/tests/functional/test_migration_linter.py b/tests/functional/test_migration_linter.py index 1e0ff1c..2c78c12 100644 --- a/tests/functional/test_migration_linter.py +++ b/tests/functional/test_migration_linter.py @@ -27,33 +27,27 @@ class BackwardcompatibilityDetectionTest(unittest.TestCase): def test_create_table_with_not_null_column(self): test_project_path = fixtures.CREATE_TABLE_WITH_NOT_NULL_COLUMN_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_no_errors(test_project_path) def test_detect_adding_not_null_column(self): test_project_path = fixtures.ADD_NOT_NULL_COLUMN_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_errors(test_project_path) def test_detect_drop_column(self): test_project_path = fixtures.DROP_COLUMN_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_errors(test_project_path) def test_detect_rename_column(self): test_project_path = fixtures.RENAME_COLUMN_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_errors(test_project_path) def test_detect_rename_table(self): test_project_path = fixtures.RENAME_TABLE_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_errors(test_project_path) def test_accept_not_null_column_followed_by_adding_default(self): test_project_path = \ fixtures.ADD_NOT_NULL_COLUMN_FOLLOWED_BY_DEFAULT_PROJECT - fixtures.prepare_git_project(test_project_path) self._test_linter_finds_no_errors(test_project_path) def test_no_specify_git_hash(self): @@ -66,7 +60,7 @@ class BackwardcompatibilityDetectionTest(unittest.TestCase): fixtures.prepare_git_project(test_project_path) self._test_linter_finds_no_errors( test_project_path, - commit_id='1021d98b5943db56122c1f848f371ddc38788d0f') + commit_id='d7125d5f4f0cc9623f670a66c54f131acc50032d') #def test_specify_git_hash_by_tag(self): # test_project_path = fixtures.MULTI_COMMIT_PROJECT