tests: moved few unittest tests to pytest (#6339)

* tests: moved few unittest tests to pytest

* tests: unittest.mock -> pytest-mock in few tests

* tests: test_prompt: unittest.mock -> pytest-mock

* Update tests/unit/test_daemon.py

Co-authored-by: Saugat Pachhai <[email protected]>

Co-authored-by: Saugat Pachhai <[email protected]>
This commit is contained in:
Nikita Kodenko
2021-07-22 18:06:54 +03:00
committed by GitHub
co-authored by Saugat Pachhai
parent cd7c95a785
commit e731e35e6c
2 changed files with 32 additions and 37 deletions
+25 -27
View File
@@ -1,38 +1,36 @@
import inspect
import os
from unittest import TestCase, mock
from dvc import daemon
class TestDaemon(TestCase):
@mock.patch("dvc.daemon._spawn_posix")
@mock.patch("dvc.daemon._spawn_windows")
def test(self, mock_windows, mock_posix):
daemon.daemon(["updater"])
def test_daemon(mocker):
mock_windows = mocker.patch("dvc.daemon._spawn_windows")
mock_posix = mocker.patch("dvc.daemon._spawn_posix")
daemon.daemon(["updater"])
if os.name == "nt":
mock_posix.assert_not_called()
mock_windows.assert_called()
args = mock_windows.call_args[0]
else:
mock_windows.assert_not_called()
mock_posix.assert_called()
args = mock_posix.call_args[0]
if os.name == "nt":
mock_posix.assert_not_called()
mock_windows.assert_called()
args = mock_windows.call_args[0]
else:
mock_windows.assert_not_called()
mock_posix.assert_called()
args = mock_posix.call_args[0]
env = args[1]
self.assertTrue("PYTHONPATH" in env.keys())
env = args[1]
assert "PYTHONPATH" in env.keys()
file_path = os.path.abspath(inspect.stack()[0][1])
file_dir = os.path.dirname(file_path)
test_dir = os.path.dirname(file_dir)
dvc_dir = os.path.dirname(test_dir)
self.assertEqual(env["PYTHONPATH"], dvc_dir)
self.assertEqual(env[daemon.DVC_DAEMON], "1")
file_path = os.path.abspath(inspect.stack()[0][1])
file_dir = os.path.dirname(file_path)
test_dir = os.path.dirname(file_dir)
dvc_dir = os.path.dirname(test_dir)
assert env["PYTHONPATH"] == dvc_dir
assert env[daemon.DVC_DAEMON] == "1"
def test_no_recursive_spawn():
with mock.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"}):
with mock.patch("dvc.daemon._spawn") as mock_spawn:
daemon.daemon(["updater"])
mock_spawn.assert_not_called()
def test_no_recursive_spawn(mocker):
mocker.patch.dict(os.environ, {daemon.DVC_DAEMON: "1"})
mock_spawn = mocker.patch("dvc.daemon._spawn")
daemon.daemon(["updater"])
mock_spawn.assert_not_called()
+7 -10
View File
@@ -1,13 +1,10 @@
from unittest import TestCase, mock
from dvc.prompt import confirm
class TestConfirm(TestCase):
@mock.patch("sys.stdout.isatty", return_value=True)
@mock.patch("dvc.prompt.input", side_effect=EOFError)
def test_eof(self, mock_input, mock_isatty):
ret = confirm("message")
mock_isatty.assert_called()
mock_input.assert_called()
self.assertFalse(ret)
def test_confirm_in_tty_if_stdin_is_closed(mocker):
mock_input = mocker.patch("dvc.prompt.input", side_effect=EOFError)
mock_isatty = mocker.patch("sys.stdout.isatty", return_value=True)
ret = confirm("message")
mock_isatty.assert_called()
mock_input.assert_called()
assert not ret