forked from iterative/dvc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: moved few unittest tests to pytest (iterative#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]>
- Loading branch information
Showing
2 changed files
with
37 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]) | ||
|
||
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()) | ||
|
||
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") | ||
|
||
|
||
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_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] | ||
|
||
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) | ||
assert env["PYTHONPATH"] == dvc_dir | ||
assert env[daemon.DVC_DAEMON] == "1" | ||
|
||
|
||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |