From 1c28412ebd9777f05910305d1ee57234607765af Mon Sep 17 00:00:00 2001 From: Felix Date: Sun, 23 Dec 2018 23:28:35 +0100 Subject: [PATCH] Improve the cache React to Comments and add a Test --- django_migration_linter/cache.py | 13 ++++------- django_migration_linter/migration_linter.py | 1 - tests/functional/test_cache.py | 26 ++++++++++++++++++--- tests/functional/test_cmd_line_call.py | 1 - 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/django_migration_linter/cache.py b/django_migration_linter/cache.py index ca03b2d..e0f00ea 100644 --- a/django_migration_linter/cache.py +++ b/django_migration_linter/cache.py @@ -33,18 +33,15 @@ class Cache(dict): def load(self): try: - f = open(self.filename, 'rb') - tmp_dict = pickle.load(f) - self.update(tmp_dict) - f.close() + with open(self.filename, 'rb') as f: + tmp_dict = pickle.load(f) + self.update(tmp_dict) except IOError: pass def save(self): - print(self) - f = open(self.filename, 'wb') - pickle.dump(self, f, 2) - f.close() + with open(self.filename, 'wb') as f: + pickle.dump(self, f, protocol=pickle.HIGHEST_PROTOCOL) def md5(self, app_name, migration): path = compose_migration_path(self.django_folder, app_name, migration) diff --git a/django_migration_linter/migration_linter.py b/django_migration_linter/migration_linter.py index 1614f8a..e3d1fd3 100644 --- a/django_migration_linter/migration_linter.py +++ b/django_migration_linter/migration_linter.py @@ -91,7 +91,6 @@ class MigrationLinter(object): if self.should_ignore_migration(app_name, migration_name): print('IGNORE') - self.new_cache[md5hash] = {'result': 'IGNORE'} self.nb_ignored += 1 return diff --git a/tests/functional/test_cache.py b/tests/functional/test_cache.py index 7c68ed4..f6352e1 100644 --- a/tests/functional/test_cache.py +++ b/tests/functional/test_cache.py @@ -65,7 +65,7 @@ class CacheTest(unittest.TestCase): # Start the Linter again -> should use cache now. linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT) - with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql)as sql_mock: + with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock: linter.lint_all_migrations() sql_mock.assert_not_called() @@ -77,7 +77,7 @@ class CacheTest(unittest.TestCase): os.remove(cache_file) linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT) - with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql)as sql_mock: + with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock: linter.lint_all_migrations() sql_mock.assert_called() @@ -93,10 +93,30 @@ class CacheTest(unittest.TestCase): # Start the Linter again -> should use cache now. linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT) - with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql)as sql_mock: + with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock: linter.lint_all_migrations() sql_mock.assert_not_called() + def test_cache_ignored_command_line(self): + cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_ignore_migration.pickle') + if os.path.exists(cache_file): + os.remove(cache_file) + linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT, + ignore_name_contains='0001') + + with mock.patch.object(MigrationLinter, 'get_sql', wraps=linter.get_sql) as sql_mock: + linter.lint_all_migrations() + sql_mock.assert_called() + + cache = Cache( + fixtures.IGNORE_MIGRATION_PROJECT, + DEFAULT_CACHE_PATH + ) + cache.load() + + self.assertNotIn('63230606af0eccaef7f1f78c537c624c', cache) + self.assertEqual(cache['5c5ca1780a9f28439c1defc1f32af894']['result'], 'IGNORE') + def test_cache_modified(self): cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_alter_column.pickle') if os.path.exists(cache_file): diff --git a/tests/functional/test_cmd_line_call.py b/tests/functional/test_cmd_line_call.py index d15a9cd..b8ddabe 100644 --- a/tests/functional/test_cmd_line_call.py +++ b/tests/functional/test_cmd_line_call.py @@ -172,7 +172,6 @@ class CallLinterFromCommandLineTest(unittest.TestCase): def test_call_linter_cmd_line_no_cache(self): cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_correct_project.pickle') - print(cache_file) if os.path.exists(cache_file): os.remove(cache_file)