From 5dfcda54f923a8994b47ec56c6a9f90eb69685e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio=20Santos?= Date: Tue, 21 Jan 2020 14:47:49 +0000 Subject: [PATCH] tests: replace spy() with pytest-mock spy Fixes #3198 --- tests/func/test_add.py | 31 +++++++++++++++++++------------ tests/func/test_data_cloud.py | 9 +++++++-- tests/func/test_import_url.py | 9 +++++++-- tests/unit/utils/test_fs.py | 9 +++++++-- tests/utils/__init__.py | 13 ------------- 5 files changed, 40 insertions(+), 31 deletions(-) diff --git a/tests/func/test_add.py b/tests/func/test_add.py index 1c885ead7..824fce4af 100644 --- a/tests/func/test_add.py +++ b/tests/func/test_add.py @@ -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") diff --git a/tests/func/test_data_cloud.py b/tests/func/test_data_cloud.py index 748a7fe63..9abd456aa 100644 --- a/tests/func/test_data_cloud.py +++ b/tests/func/test_data_cloud.py @@ -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 ): diff --git a/tests/func/test_import_url.py b/tests/func/test_import_url.py index ae59e3aa5..0fbd930d3 100644 --- a/tests/func/test_import_url.py +++ b/tests/func/test_import_url.py @@ -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 ): diff --git a/tests/unit/utils/test_fs.py b/tests/unit/utils/test_fs.py index 0c0e19a0b..88166d455 100644 --- a/tests/unit/utils/test_fs.py +++ b/tests/unit/utils/test_fs.py @@ -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 ): diff --git a/tests/utils/__init__.py b/tests/utils/__init__.py index a40a91f2c..9e5448abb 100644 --- a/tests/utils/__init__.py +++ b/tests/utils/__init__.py @@ -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()