Improve the cache

React to Comments and add a Test
This commit is contained in:
Felix committed 2018-12-23 23:28:45 +01:00
1 parent 03e48b7575
commit 1c28412ebd
4 files changed
+28 -13

No files matched your search

+5 -8
View File
@@ -33,18 +33,15 @@ class Cache(dict):
def load(self): def load(self):
try: try:
f = open(self.filename, 'rb') with open(self.filename, 'rb') as f:
tmp_dict = pickle.load(f) tmp_dict = pickle.load(f)
self.update(tmp_dict) self.update(tmp_dict)
f.close()
except IOError: except IOError:
pass pass
def save(self): def save(self):
print(self) with open(self.filename, 'wb') as f:
f = open(self.filename, 'wb') pickle.dump(self, f, protocol=pickle.HIGHEST_PROTOCOL)
pickle.dump(self, f, 2)
f.close()
def md5(self, app_name, migration): def md5(self, app_name, migration):
path = compose_migration_path(self.django_folder, app_name, migration) path = compose_migration_path(self.django_folder, app_name, migration)
@@ -91,7 +91,6 @@ class MigrationLinter(object):
if self.should_ignore_migration(app_name, migration_name): if self.should_ignore_migration(app_name, migration_name):
print('IGNORE') print('IGNORE')
self.new_cache[md5hash] = {'result': 'IGNORE'}
self.nb_ignored += 1 self.nb_ignored += 1
return return
+23 -3
View File
@@ -65,7 +65,7 @@ class CacheTest(unittest.TestCase):
# Start the Linter again -> should use cache now. # Start the Linter again -> should use cache now.
linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT) 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() linter.lint_all_migrations()
sql_mock.assert_not_called() sql_mock.assert_not_called()
@@ -77,7 +77,7 @@ class CacheTest(unittest.TestCase):
os.remove(cache_file) os.remove(cache_file)
linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT) 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() linter.lint_all_migrations()
sql_mock.assert_called() sql_mock.assert_called()
@@ -93,10 +93,30 @@ class CacheTest(unittest.TestCase):
# Start the Linter again -> should use cache now. # Start the Linter again -> should use cache now.
linter = MigrationLinter(fixtures.IGNORE_MIGRATION_PROJECT) 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() linter.lint_all_migrations()
sql_mock.assert_not_called() 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): def test_cache_modified(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_alter_column.pickle') cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_project_alter_column.pickle')
if os.path.exists(cache_file): if os.path.exists(cache_file):
-1
View File
@@ -172,7 +172,6 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
def test_call_linter_cmd_line_no_cache(self): def test_call_linter_cmd_line_no_cache(self):
cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_correct_project.pickle') cache_file = os.path.join(DEFAULT_CACHE_PATH, 'test_correct_project.pickle')
print(cache_file)
if os.path.exists(cache_file): if os.path.exists(cache_file):
os.remove(cache_file) os.remove(cache_file)