tests: replace spy() with pytest-mock spy

Fixes #3198
This commit is contained in:
Fábio Santos
2020-01-21 14:47:49 +00:00
parent 74bbc3bc04
commit 5dfcda54f9
5 changed files with 40 additions and 31 deletions
+19 -12
View File
@@ -28,7 +28,6 @@ from dvc.utils.fs import path_isin
from dvc.utils.stage import load_stage_file
from tests.basic_env import TestDvc
from tests.utils import get_gitignore_content
from tests.utils import spy
def test_add(tmp_dir, dvc):
@@ -245,9 +244,15 @@ class TestDoubleAddUnchanged(TestDvc):
self.assertEqual(ret, 0)
class TestShouldUpdateStateEntryForFileAfterAdd(TestDvc):
class TestDvcWithMocker(TestDvc):
@pytest.fixture(autouse=True)
def use_mocker(self, mocker):
self.mocker = mocker
class TestShouldUpdateStateEntryForFileAfterAdd(TestDvcWithMocker):
def test(self):
file_md5_counter = spy(dvc.remote.local.file_md5)
file_md5_counter = self.mocker.spy(dvc.remote.local, "file_md5")
with patch.object(dvc.remote.local, "file_md5", file_md5_counter):
ret = main(["config", "cache.type", "copy"])
self.assertEqual(ret, 0)
@@ -274,9 +279,9 @@ class TestShouldUpdateStateEntryForFileAfterAdd(TestDvc):
self.assertEqual(file_md5_counter.mock.call_count, 1)
class TestShouldUpdateStateEntryForDirectoryAfterAdd(TestDvc):
class TestShouldUpdateStateEntryForDirectoryAfterAdd(TestDvcWithMocker):
def test(self):
file_md5_counter = spy(dvc.remote.local.file_md5)
file_md5_counter = self.mocker.spy(dvc.remote.local, "file_md5")
with patch.object(dvc.remote.local, "file_md5", file_md5_counter):
ret = main(["config", "cache.type", "copy"])
@@ -320,11 +325,13 @@ class TestAddCommit(TestDvc):
self.assertEqual(len(os.listdir(self.dvc.cache.local.cache_dir)), 1)
class TestShouldCollectDirCacheOnlyOnce(TestDvc):
class TestShouldCollectDirCacheOnlyOnce(TestDvcWithMocker):
def test(self):
from dvc.remote.local import RemoteLOCAL
get_dir_checksum_counter = spy(RemoteLOCAL.get_dir_checksum)
get_dir_checksum_counter = self.mocker.spy(
RemoteLOCAL, "get_dir_checksum"
)
with patch.object(
RemoteLOCAL, "get_dir_checksum", get_dir_checksum_counter
):
@@ -477,15 +484,15 @@ def test_should_cleanup_after_failed_add(tmp_dir, scm, dvc, repo_template):
assert "/bar" not in gitignore_content
class TestShouldNotTrackGitInternalFiles(TestDvc):
class TestShouldNotTrackGitInternalFiles(TestDvcWithMocker):
def test(self):
stage_creator_spy = spy(dvc.repo.add._create_stages)
stage_creator_spy = self.mocker.spy(dvc.repo.add, "_create_stages")
with patch.object(dvc.repo.add, "_create_stages", stage_creator_spy):
ret = main(["add", "-R", self.dvc.root_dir])
self.assertEqual(0, ret)
created_stages_filenames = stage_creator_spy.mock.call_args[0][0]
created_stages_filenames = stage_creator_spy.mock.call_args[0][1]
for fname in created_stages_filenames:
self.assertNotIn(".git", fname)
@@ -572,7 +579,7 @@ def test_readding_dir_should_not_unprotect_all(tmp_dir, dvc, mocker):
dvc.add("dir")
tmp_dir.gen("dir/new_file", "new_file_content")
unprotect_spy = spy(RemoteLOCAL.unprotect)
unprotect_spy = mocker.spy(RemoteLOCAL, "unprotect")
mocker.patch.object(RemoteLOCAL, "unprotect", unprotect_spy)
dvc.add("dir")
@@ -587,7 +594,7 @@ def test_should_not_checkout_when_adding_cached_copy(tmp_dir, dvc, mocker):
shutil.copy("bar", "foo")
copy_spy = spy(dvc.cache.local.copy)
copy_spy = mocker.spy(dvc.cache.local, "copy")
mocker.patch.object(dvc.cache.local, "copy", copy_spy)
dvc.add("foo")
+7 -2
View File
@@ -28,7 +28,6 @@ from dvc.utils import file_md5
from dvc.utils.stage import dump_stage_file
from dvc.utils.stage import load_stage_file
from tests.basic_env import TestDvc
from tests.utils import spy
from tests.remotes import (
Azure,
@@ -586,8 +585,14 @@ class TestRecursiveSyncOperations(Local, TestDataCloudBase):
class TestCheckSumRecalculation(TestDvc):
@pytest.fixture(autouse=True)
def use_mocker(self, mocker):
self.mocker = mocker
def test(self):
test_get_file_checksum = spy(RemoteLOCAL.get_file_checksum)
test_get_file_checksum = self.mocker.spy(
RemoteLOCAL, "get_file_checksum"
)
with patch.object(
RemoteLOCAL, "get_file_checksum", test_get_file_checksum
):
+7 -2
View File
@@ -8,7 +8,6 @@ import dvc
from dvc.main import main
from dvc.utils.fs import makedirs
from tests.basic_env import TestDvc
from tests.utils import spy
class TestCmdImport(TestDvc):
@@ -42,6 +41,10 @@ class TestDefaultOutput(TestDvc):
class TestShouldRemoveOutsBeforeImport(TestDvc):
@pytest.fixture(autouse=True)
def use_mocker(self, mocker):
self.mocker = mocker
def setUp(self):
super().setUp()
tmp_dir = self.mkdtemp()
@@ -50,7 +53,9 @@ class TestShouldRemoveOutsBeforeImport(TestDvc):
fobj.write("content")
def test(self):
remove_outs_call_counter = spy(dvc.stage.Stage.remove_outs)
remove_outs_call_counter = self.mocker.spy(
dvc.stage.Stage, "remove_outs"
)
with patch.object(
dvc.stage.Stage, "remove_outs", remove_outs_call_counter
):
+7 -2
View File
@@ -22,7 +22,6 @@ from dvc.utils.fs import path_isin, remove
from dvc.utils.fs import makedirs
from dvc.utils.fs import walk_files
from tests.basic_env import TestDir
from tests.utils import spy
class TestMtimeAndSize(TestDir):
@@ -45,6 +44,10 @@ class TestMtimeAndSize(TestDir):
class TestContainsLink(TestCase):
@pytest.fixture(autouse=True)
def use_mocker(self, mocker):
self.mocker = mocker
def test_should_raise_exception_on_base_path_not_in_path(self):
with self.assertRaises(BasePathNotInCheckedPathException):
contains_symlink_up_to(os.path.join("foo", "path"), "bar")
@@ -72,7 +75,9 @@ class TestContainsLink(TestCase):
@patch.object(System, "is_symlink", return_value=False)
def test_should_call_recursive_on_no_condition_matched(self, _):
contains_symlink_spy = spy(contains_symlink_up_to)
contains_symlink_spy = self.mocker.spy(
dvc.utils.fs, "contains_symlink_up_to"
)
with patch.object(
dvc.utils.fs, "contains_symlink_up_to", contains_symlink_spy
):
-13
View File
@@ -2,22 +2,9 @@ import os
from contextlib import contextmanager
from filecmp import dircmp
from mock import MagicMock
from dvc.scm import Git
def spy(method_to_decorate):
mock = MagicMock()
def wrapper(self, *args, **kwargs):
mock(*args, **kwargs)
return method_to_decorate(self, *args, **kwargs)
wrapper.mock = mock
return wrapper
def get_gitignore_content():
with open(Git.GITIGNORE, "r") as gitignore:
return gitignore.read().splitlines()