Skip to content

Commit

Permalink
tests: moved few unittest tests to pytest (iterative#6339)
Browse files Browse the repository at this point in the history
* 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
nik123 and skshetry authored Jul 22, 2021
1 parent cd7c95a commit e731e35
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
62 changes: 30 additions & 32 deletions tests/unit/test_daemon.py
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()
17 changes: 7 additions & 10 deletions tests/unit/test_prompt.py
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

0 comments on commit e731e35

Please sign in to comment.