#23 Add tests for caching

This commit is contained in:
Felix committed 2018-12-18 15:33:26 +01:00
1 parent 178d8036c2
commit b3815b6b98
6 files changed
+165 -1

No files matched your search

+85
View File
@@ -0,0 +1,85 @@
# Copyright 2018 3YOURMIND GmbH
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import shutil
import tempfile
import unittest
from unittest import mock
from django_migration_linter.utils import get_default_cache_file
from tests import fixtures
from django_migration_linter import MigrationLinter
from tests.fixtures import ADD_NOT_NULL_COLUMN_PROJECT
class CacheTest(unittest.TestCase):
MIGRATION_FILE = os.path.join(ADD_NOT_NULL_COLUMN_PROJECT, 'test_app', 'migrations', '0001_create_table.py')
fd, temp_path = tempfile.mkstemp()
def setUp(self):
shutil.copy2(self.MIGRATION_FILE, self.temp_path)
def tearDown(self):
shutil.copy2(self.temp_path, self.MIGRATION_FILE)
os.remove(self.temp_path)
def test_cache_normal(self):
if os.path.exists(get_default_cache_file('test_project_add_not_null_column')):
os.remove(get_default_cache_file('test_project_add_not_null_column'))
linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT)
linter.lint_all_migrations()
cache = linter.get_cache()
self.assertEqual(cache['hash']['result'], 'OK')
self.assertEqual(cache['hashERR']['result'], 'ERR')
self.assertDictEqual(cache['hashERR']['errors'], ...)
with mock.patch(
'subprocess.Popen'
) as popen_mock:
linter.lint_all_migrations()
self.assertEqual(popen_mock.call_count, 2)
with mock.patch(
'subprocess.Popen'
) as popen_mock:
linter.lint_all_migrations()
self.assertEqual(popen_mock.call_count, 0)
self.assertFalse(linter.has_errors())
def test_cache_ignored(self):
pass
def test_cache_modified(self):
if os.path.exists(get_default_cache_file('test_project_add_not_null_column')):
os.remove(get_default_cache_file('test_project_add_not_null_column'))
linter = MigrationLinter(fixtures.ADD_NOT_NULL_COLUMN_PROJECT)
linter.lint_all_migrations()
cache = linter.get_cache()
self.assertEqual(cache['hash']['result'], 'OK')
self.assertEqual(cache['hashERR']['result'], 'ERR')
self.assertDictEqual(cache['hashERR']['errors'], ...)
with open( self.MIGRATION_FILE, "a") as f:
f.write("# modification at the end of the file\n")
linter.lint_all_migrations()
cache = linter.get_cache()
self.assertNotIn('hash', cache)
self.assertEqual(cache['newhash']['result'], 'OK')
+45
View File
@@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import unittest
from subprocess import Popen, PIPE
from django_migration_linter import utils
from django_migration_linter.utils import get_default_cache_file
from tests import fixtures
import sys
@@ -115,6 +117,49 @@ class CallLinterFromCommandLineTest(unittest.TestCase):
self.assertTrue(lines[1].endswith('OK'))
self.assertTrue(lines[2].endswith('IGNORE'))
def test_call_linter_cmd_line_cache(self):
if os.path.exists(get_default_cache_file('test_correct_project')):
os.remove(get_default_cache_file('test_correct_project'))
cmd = '{0} {1}'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertTrue(os.path.exists(get_default_cache_file('test_correct_project')))
def test_call_linter_cmd_line_cache_path(self):
if os.path.exists('/tmp/migration-linter-cache-tests'):
os.remove('/tmp/migration-linter-cache-tests')
cmd = '{0} {1} --cache-path={2}'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT,
'/tmp/migration-linter-cache-tests'
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
self.assertTrue(os.path.exists('/tmp/migration-linter-cache-tests'))
def test_call_linter_cmd_line_no_cache(self):
if os.path.exists(get_default_cache_file('test_correct_project')):
os.remove(get_default_cache_file('test_correct_project'))
cmd = '{0} {1} --no-cache'.format(
self.linter_exec,
fixtures.CORRECT_PROJECT
)
process = Popen(
cmd, shell=True, stdout=PIPE, stderr=PIPE)
process.wait()
self.assertEqual(process.returncode, 0)
self.assertFalse(os.path.exists(get_default_cache_file('test_correct_project')))
def test_call_linter_cmd_line_git_id(self):
cmd = '{0} {1} d7125d5f4f0cc9623f670a66c54f131acc50032d'.format(
self.linter_exec,