mirror of
https://github.com/Stone-Red-Code/dvc.git
synced 2026-12-02 23:10:25 +01:00
tests: replace several unittest.mock usages with mocker (#6544)
* tests: several unittest.mock usages replaced with mocker * tests: fixes in tests/unit/repo/test_reproduce.py * tests: rolled back WiP tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import os
|
||||
from unittest import mock
|
||||
from unittest.mock import call
|
||||
|
||||
import pytest
|
||||
@@ -9,17 +8,17 @@ from dvc.main import main
|
||||
from dvc.repo import Repo
|
||||
|
||||
|
||||
@mock.patch("dvc.analytics.send")
|
||||
def test_daemon_analytics(mock_send, tmp_path):
|
||||
def test_daemon_analytics(mocker, tmp_path):
|
||||
mock_send = mocker.patch("dvc.analytics.send")
|
||||
report = os.fspath(tmp_path)
|
||||
assert 0 == main(["daemon", "analytics", report])
|
||||
|
||||
mock_send.assert_called_with(report)
|
||||
|
||||
|
||||
@mock.patch("dvc.analytics.collect_and_send_report")
|
||||
@mock.patch("dvc.analytics.is_enabled", return_value=True)
|
||||
def test_main_analytics(mock_is_enabled, mock_report, tmp_dir, dvc):
|
||||
def test_main_analytics(mocker, tmp_dir, dvc):
|
||||
mock_is_enabled = mocker.patch("dvc.analytics.collect_and_send_report")
|
||||
mock_report = mocker.patch("dvc.analytics.is_enabled", return_value=True)
|
||||
tmp_dir.gen("foo", "text")
|
||||
assert 0 == main(["add", "foo"])
|
||||
assert mock_is_enabled.called
|
||||
@@ -46,8 +45,8 @@ class ANY:
|
||||
return isinstance(other, self.expected_type)
|
||||
|
||||
|
||||
@mock.patch("requests.post")
|
||||
def test_collect_and_send_report(mock_post, dvc, mock_daemon):
|
||||
def test_collect_and_send_report(mocker, dvc, mock_daemon):
|
||||
mock_post = mocker.patch("requests.post")
|
||||
collect_and_send_report()
|
||||
|
||||
assert mock_daemon.call_count == 1
|
||||
|
||||
@@ -13,7 +13,7 @@ from tests.unit.fs.test_repo import make_subrepo
|
||||
from tests.utils import clean_staging
|
||||
|
||||
|
||||
def test_external_repo(erepo_dir):
|
||||
def test_external_repo(erepo_dir, mocker):
|
||||
with erepo_dir.chdir():
|
||||
with erepo_dir.branch("branch", new=True):
|
||||
erepo_dir.dvc_gen("file", "branch", commit="create file on branch")
|
||||
@@ -21,16 +21,17 @@ def test_external_repo(erepo_dir):
|
||||
|
||||
url = os.fspath(erepo_dir)
|
||||
|
||||
with patch.object(Git, "clone", wraps=Git.clone) as mock:
|
||||
with external_repo(url) as repo:
|
||||
with repo.open_by_relpath("file") as fd:
|
||||
assert fd.read() == "master"
|
||||
mock = mocker.patch.object(Git, "clone", wraps=Git.clone)
|
||||
|
||||
with external_repo(url, rev="branch") as repo:
|
||||
with repo.open_by_relpath("file") as fd:
|
||||
assert fd.read() == "branch"
|
||||
with external_repo(url) as repo:
|
||||
with repo.open_by_relpath("file") as fd:
|
||||
assert fd.read() == "master"
|
||||
|
||||
assert mock.call_count == 1
|
||||
with external_repo(url, rev="branch") as repo:
|
||||
with repo.open_by_relpath("file") as fd:
|
||||
assert fd.read() == "branch"
|
||||
|
||||
assert mock.call_count == 1
|
||||
|
||||
|
||||
def test_source_change(erepo_dir):
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import collections
|
||||
import os
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -191,14 +190,14 @@ def test_show_json_hide_missing(mocker, capsys):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("show_hash", [None, True, False])
|
||||
@mock.patch("dvc.command.diff._show_md")
|
||||
def test_diff_show_md_and_hash(mock_show_md, mocker, show_hash):
|
||||
def test_diff_show_md_and_hash(mocker, show_hash):
|
||||
options = ["diff", "--show-md"] + (["--show-hash"] if show_hash else [])
|
||||
args = parse_args(options)
|
||||
cmd = args.func(args)
|
||||
|
||||
diff = {}
|
||||
show_hash = show_hash if show_hash else False
|
||||
mock_show_md = mocker.patch("dvc.command.diff._show_md")
|
||||
mocker.patch("dvc.repo.Repo.diff", return_value=diff.copy())
|
||||
|
||||
assert 0 == cmd.run()
|
||||
|
||||
@@ -336,7 +336,7 @@ def make_subrepo(dir_, scm, config=None):
|
||||
dir_.add_remote(config=config)
|
||||
|
||||
|
||||
def test_subrepos(tmp_dir, scm, dvc):
|
||||
def test_subrepos(tmp_dir, scm, dvc, mocker):
|
||||
tmp_dir.scm_gen(
|
||||
{"dir": {"repo.txt": "file to confuse RepoFileSystem"}},
|
||||
commit="dir/repo.txt",
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
import dvc.objects.db.slow_link_detection
|
||||
@@ -14,9 +12,9 @@ def timeout_immediately(monkeypatch):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def make_remote():
|
||||
def make_remote(mocker):
|
||||
def _make_remote(cache_type=None, should_warn=True):
|
||||
remote = mock.Mock()
|
||||
remote = mocker.Mock()
|
||||
remote.cache_types = cache_type
|
||||
remote.slow_link_warning = should_warn
|
||||
return remote
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from unittest import mock
|
||||
|
||||
|
||||
@mock.patch("dvc.repo.reproduce._reproduce_stage", returns=[])
|
||||
def test_number_reproduces(reproduce_stage_mock, tmp_dir, dvc):
|
||||
def test_number_reproduces(tmp_dir, dvc, mocker):
|
||||
reproduce_stage_mock = mocker.patch(
|
||||
"dvc.repo.reproduce._reproduce_stage", returns=[]
|
||||
)
|
||||
tmp_dir.dvc_gen({"pre-foo": "pre-foo"})
|
||||
|
||||
dvc.run(
|
||||
|
||||
@@ -2,7 +2,6 @@ import os
|
||||
import signal
|
||||
import subprocess
|
||||
import threading
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -18,35 +17,35 @@ TEST_STAGE_DICT = {
|
||||
}
|
||||
|
||||
|
||||
def test_stage_checksum():
|
||||
def test_stage_checksum(mocker):
|
||||
stage = Stage(None, "path", cmd="mycmd")
|
||||
|
||||
with mock.patch.object(stage, "dumpd", return_value=TEST_STAGE_DICT):
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
mocker.patch.object(stage, "dumpd", return_value=TEST_STAGE_DICT)
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
|
||||
|
||||
def test_wdir_default_ignored():
|
||||
def test_wdir_default_ignored(mocker):
|
||||
stage = Stage(None, "path", cmd="mycmd")
|
||||
d = dict(TEST_STAGE_DICT, wdir=".")
|
||||
|
||||
with mock.patch.object(stage, "dumpd", return_value=d):
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
mocker.patch.object(stage, "dumpd", return_value=d)
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
|
||||
|
||||
def test_wdir_non_default_is_not_ignored():
|
||||
def test_wdir_non_default_is_not_ignored(mocker):
|
||||
stage = Stage(None, "path", cmd="mycmd")
|
||||
d = dict(TEST_STAGE_DICT, wdir="..")
|
||||
|
||||
with mock.patch.object(stage, "dumpd", return_value=d):
|
||||
assert stage.compute_md5() == "2ceba15e87f6848aa756502c1e6d24e9"
|
||||
mocker.patch.object(stage, "dumpd", return_value=d)
|
||||
assert stage.compute_md5() == "2ceba15e87f6848aa756502c1e6d24e9"
|
||||
|
||||
|
||||
def test_meta_ignored():
|
||||
def test_meta_ignored(mocker):
|
||||
stage = Stage(None, "path", cmd="mycmd")
|
||||
d = dict(TEST_STAGE_DICT, meta={"author": "Suor"})
|
||||
|
||||
with mock.patch.object(stage, "dumpd", return_value=d):
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
mocker.patch.object(stage, "dumpd", return_value=d)
|
||||
assert stage.compute_md5() == "e9521a22111493406ea64a88cda63e0b"
|
||||
|
||||
|
||||
def test_path_conversion(dvc):
|
||||
@@ -124,21 +123,21 @@ def test_external_outs(tmp_path_factory, dvc):
|
||||
create_stage(Stage, dvc, "path.dvc", outs=[os.fspath(foo)], external=True)
|
||||
|
||||
|
||||
def test_env(dvc):
|
||||
def test_env(dvc, mocker):
|
||||
from dvc.env import DVC_ROOT
|
||||
from dvc.exceptions import DvcException
|
||||
from dvc.stage import create_stage
|
||||
|
||||
stage = create_stage(Stage, dvc, "path.dvc", outs=["foo", "bar"])
|
||||
|
||||
with mock.patch.object(stage, "_read_env", return_value={"foo": "foo"}):
|
||||
env = stage.env()
|
||||
assert env == {DVC_ROOT: dvc.root_dir, "foo": "foo"}
|
||||
mocker.patch.object(stage, "_read_env", return_value={"foo": "foo"})
|
||||
env = stage.env()
|
||||
assert env == {DVC_ROOT: dvc.root_dir, "foo": "foo"}
|
||||
|
||||
def mock_read_env(out, **kwargs):
|
||||
return {"foo": str(out)}
|
||||
|
||||
with pytest.raises(DvcException) as exc:
|
||||
with mock.patch.object(stage, "_read_env", mock_read_env):
|
||||
env = stage.env()
|
||||
mocker.patch.object(stage, "_read_env", mock_read_env)
|
||||
_ = stage.env()
|
||||
assert exc.value == "Conflicting values for env variable"
|
||||
|
||||
@@ -10,17 +10,16 @@ from dvc.cli import parse_args
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_global_dir(tmp_path):
|
||||
def tmp_global_dir(mocker, tmp_path):
|
||||
"""
|
||||
Fixture to prevent modifying the actual global config
|
||||
"""
|
||||
with mock.patch("dvc.config.Config.get_dir", return_value=str(tmp_path)):
|
||||
yield
|
||||
mocker.patch("dvc.config.Config.get_dir", return_value=str(tmp_path))
|
||||
|
||||
|
||||
@mock.patch("dvc.daemon._spawn")
|
||||
@mock.patch("json.dump")
|
||||
def test_collect_and_send_report(mock_json, mock_daemon, tmp_global_dir):
|
||||
def test_collect_and_send_report(mocker, tmp_global_dir):
|
||||
mock_json = mocker.patch("json.dump")
|
||||
mock_daemon = mocker.patch("dvc.daemon._spawn")
|
||||
analytics.collect_and_send_report()
|
||||
report = mock_json.call_args[0][0]
|
||||
|
||||
@@ -67,7 +66,7 @@ def test_send(mock_post, tmp_path):
|
||||
|
||||
analytics.send(str(report_file))
|
||||
assert mock_post.called
|
||||
assert mock_post.call_args.args[0] == url
|
||||
assert mock_post.call_args[0][0] == url
|
||||
assert not report_file.exists()
|
||||
|
||||
|
||||
|
||||
+17
-18
@@ -1,6 +1,4 @@
|
||||
import logging
|
||||
import sys
|
||||
from unittest import mock
|
||||
|
||||
from dvc.progress import Tqdm
|
||||
from dvc.utils import env2bool
|
||||
@@ -15,15 +13,15 @@ def test_quiet_logging(caplog, capsys):
|
||||
assert out_err.err == ""
|
||||
|
||||
|
||||
def test_quiet_logging_disable_false(caplog, capsys):
|
||||
def test_quiet_logging_disable_false(caplog, capsys, mocker):
|
||||
# simulate interactive terminal
|
||||
mocker.patch("sys.stdout.isatty", return_value=True)
|
||||
with caplog.at_level(logging.CRITICAL, logger="dvc"):
|
||||
# simulate interactive terminal
|
||||
with mock.patch.object(sys.stderr, "isatty", return_value=True):
|
||||
for _ in Tqdm(range(10), disable=False):
|
||||
pass
|
||||
out_err = capsys.readouterr()
|
||||
assert out_err.out == ""
|
||||
assert out_err.err == ""
|
||||
for _ in Tqdm(range(10), disable=False):
|
||||
pass
|
||||
out_err = capsys.readouterr()
|
||||
assert out_err.out == ""
|
||||
assert out_err.err == ""
|
||||
|
||||
|
||||
def test_quiet_notty(caplog, capsys):
|
||||
@@ -38,12 +36,13 @@ def test_quiet_notty(caplog, capsys):
|
||||
assert out_err.err == ""
|
||||
|
||||
|
||||
def test_default(caplog, capsys):
|
||||
def test_default(caplog, capsys, mocker):
|
||||
# simulate interactive terminal
|
||||
mocker.patch("sys.stdout.isatty", return_value=True)
|
||||
with caplog.at_level(logging.INFO, logger="dvc"):
|
||||
# simulate interactive terminal
|
||||
with mock.patch.object(sys.stderr, "isatty", return_value=True):
|
||||
for _ in Tqdm(range(10)):
|
||||
pass
|
||||
out_err = capsys.readouterr()
|
||||
assert out_err.out == ""
|
||||
assert "0/10" in out_err.err
|
||||
for _ in Tqdm(range(10)):
|
||||
pass
|
||||
|
||||
out_err = capsys.readouterr()
|
||||
assert out_err.out == ""
|
||||
assert "0/10" in out_err.err
|
||||
|
||||
@@ -2,7 +2,6 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
@@ -12,12 +11,11 @@ from tests.func.parsing.test_errors import escape_ansi
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def tmp_global_dir(tmp_path):
|
||||
def tmp_global_dir(mocker, tmp_path):
|
||||
"""
|
||||
Fixture to prevent modifying the actual global config
|
||||
"""
|
||||
with mock.patch("dvc.config.Config.get_dir", return_value=str(tmp_path)):
|
||||
yield
|
||||
mocker.patch("dvc.config.Config.get_dir", return_value=str(tmp_path))
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
@@ -36,8 +34,8 @@ def mock_tty(mocker):
|
||||
return mocker.patch("sys.stdout.isatty", return_value=True)
|
||||
|
||||
|
||||
@mock.patch("requests.get")
|
||||
def test_fetch(mock_get, updater):
|
||||
def test_fetch(mocker, updater):
|
||||
mock_get = mocker.patch("requests.get")
|
||||
mock_get.return_value.status_code = 200
|
||||
mock_get.return_value.json.return_value = {"version": __version__}
|
||||
|
||||
@@ -70,8 +68,8 @@ def test_is_enabled(dvc, updater, config, result):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("result", [True, False])
|
||||
@mock.patch("dvc.updater.Updater._check")
|
||||
def test_check_update_respect_config(mock_check, result, updater, mocker):
|
||||
def test_check_update_respect_config(result, updater, mocker):
|
||||
mock_check = mocker.patch("dvc.updater.Updater._check")
|
||||
mocker.patch.object(updater, "is_enabled", return_value=result)
|
||||
updater.check()
|
||||
assert result == mock_check.called
|
||||
@@ -139,8 +137,8 @@ def test_check_fetches_on_invalid_data_format(
|
||||
fetch.assert_called_once()
|
||||
|
||||
|
||||
@mock.patch("dvc.updater.Updater._check")
|
||||
def test_check(mock_check, updater):
|
||||
def test_check(mocker, updater):
|
||||
mock_check = mocker.patch("dvc.updater.Updater._check")
|
||||
updater.check()
|
||||
updater.check()
|
||||
updater.check()
|
||||
|
||||
+13
-13
@@ -1,20 +1,20 @@
|
||||
import subprocess
|
||||
from unittest import mock
|
||||
|
||||
import dvc.version
|
||||
|
||||
|
||||
def test_is_release():
|
||||
with mock.patch.object(dvc.version.subprocess, "check_output") as m:
|
||||
m.side_effect = subprocess.CalledProcessError(1, "cmd")
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret is False
|
||||
def test_is_release(mocker):
|
||||
m = mocker.patch.object(dvc.version.subprocess, "check_output")
|
||||
|
||||
m.side_effect = None
|
||||
m.return_value = dvc.version._BASE_VERSION.encode("ascii")
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret
|
||||
m.side_effect = subprocess.CalledProcessError(1, "cmd")
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret is False
|
||||
|
||||
m.return_value = b"630d1741c2d5dd89a3176bd15b63121b905d35c9"
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret is False
|
||||
m.side_effect = None
|
||||
m.return_value = dvc.version._BASE_VERSION.encode("ascii")
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret
|
||||
|
||||
m.return_value = b"630d1741c2d5dd89a3176bd15b63121b905d35c9"
|
||||
ret = dvc.version._is_release(None, dvc.version._BASE_VERSION)
|
||||
assert ret is False
|
||||
|
||||
Reference in New Issue
Block a user