From d0f3559d9669194084820a4df2e7b2ed3264d35b Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Thu, 12 Dec 2019 18:57:45 -0600 Subject: [PATCH 1/5] tests: updater, use dir helpers --- tests/func/test_updater.py | 52 -------------------------------- tests/unit/test_updater.py | 61 ++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 74 deletions(-) diff --git a/tests/func/test_updater.py b/tests/func/test_updater.py index 4045c0ca6..e69de29bb 100644 --- a/tests/func/test_updater.py +++ b/tests/func/test_updater.py @@ -1,52 +0,0 @@ -import os - -import mock -import pytest - -from dvc.updater import Updater - - -@pytest.fixture -def updater(dvc_repo): - return Updater(dvc_repo.dvc_dir) - - -def test_updater(updater): - # NOTE: only test on travis CRON to avoid generating too much logs - travis = os.getenv("TRAVIS") == "true" - if not travis: - return - - cron = os.getenv("TRAVIS_EVENT_TYPE") == "cron" - if not cron: - return - - env = os.environ.copy() - if env.get("CI"): - del env["CI"] - - with mock.patch.dict(os.environ, env): - updater.check() - updater.check() - updater.check() - - -def test_check_version_newer(updater): - updater.latest = "0.20.8" - updater.current = "0.21.0" - - assert not updater._is_outdated() - - -def test_check_version_equal(updater): - updater.latest = "0.20.8" - updater.current = "0.20.8" - - assert not updater._is_outdated() - - -def test_check_version_outdated(updater): - updater.latest = "0.21.0" - updater.current = "0.20.8" - - assert updater._is_outdated() diff --git a/tests/unit/test_updater.py b/tests/unit/test_updater.py index c8a0ee2de..89ffc3236 100644 --- a/tests/unit/test_updater.py +++ b/tests/unit/test_updater.py @@ -1,40 +1,57 @@ import json import os +import mock +import pytest from dvc import __version__ from dvc.updater import Updater -class MockResponse(object): - def __init__(self, json_data, status_code): - self.json_data = json_data - self.status_code = status_code - - def json(self): - return self.json_data +@pytest.fixture +def updater(dvc): + return Updater(dvc.dvc_dir) -def mocked_requests_get(*args, **kwargs): - class MockResponse: - def __init__(self, json_data, status_code): - self.json_data = json_data - self.status_code = status_code +@mock.patch("requests.get") +def test_fetch(mock_get, updater): + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = {"version": __version__} - def json(self): - return self.json_data - - return MockResponse({"version": __version__}, 200) - - -def test_fetch(dvc_repo, mocker): - updater = Updater(dvc_repo.dvc_dir) assert not os.path.exists(updater.updater_file) - mock_get = mocker.patch("requests.get", side_effect=mocked_requests_get) updater.fetch(detach=False) - mock_get.assert_called_once_with(Updater.URL, timeout=Updater.TIMEOUT_GET) + mock_get.assert_called_once_with(Updater.URL, timeout=Updater.TIMEOUT_GET) assert os.path.isfile(updater.updater_file) + with open(updater.updater_file, "r") as fobj: info = json.load(fobj) + assert info["version"] == __version__ + + +@pytest.mark.parametrize( + "latest, current, result", + [ + ("0.20.8", "0.21.0", False), + ("0.20.8", "0.20.8", False), + ("0.20.8", "0.19.0", True), + ], +) +def test_is_outdated(latest, current, result, updater): + updater.latest = latest + updater.current = current + + assert updater._is_outdated() == result + + +def test_check(updater, monkeypatch): + # NOTE: only test on travis CRON to avoid generating too much logs + if not os.getenv("TRAVIS_EVENT_TYPE") == "cron": + return + + monkeypatch.delenv("CI") + + updater.check() + updater.check() + updater.check() From c09c3aec0cd3a22a3efe099ec69da51df04173e4 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Thu, 12 Dec 2019 19:05:31 -0600 Subject: [PATCH 2/5] remove empty file --- tests/func/test_updater.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/func/test_updater.py diff --git a/tests/func/test_updater.py b/tests/func/test_updater.py deleted file mode 100644 index e69de29bb..000000000 From 4b833f58e56e31d12d818316a2adce614d3a1c16 Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Sun, 15 Dec 2019 13:09:23 -0600 Subject: [PATCH 3/5] tests/updater: use skipif for .check --- tests/unit/test_updater.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/unit/test_updater.py b/tests/unit/test_updater.py index 89ffc3236..d68e33d0a 100644 --- a/tests/unit/test_updater.py +++ b/tests/unit/test_updater.py @@ -45,11 +45,11 @@ def test_is_outdated(latest, current, result, updater): assert updater._is_outdated() == result +@pytest.mark.skipif( + os.getenv("TRAVIS_EVENT_TYPE") != "cron", + reason = "Only run on travis CRON to avoid generating too much logs" def test_check(updater, monkeypatch): - # NOTE: only test on travis CRON to avoid generating too much logs - if not os.getenv("TRAVIS_EVENT_TYPE") == "cron": - return - + monkeypatch.setenv("DVC_TEST", "true") monkeypatch.delenv("CI") updater.check() From 8a14700c12132889eb1be1022f79088ff0dff51b Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 17 Dec 2019 12:18:43 -0600 Subject: [PATCH 4/5] tests/updater: fix problems with .check --- tests/unit/test_updater.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/unit/test_updater.py b/tests/unit/test_updater.py index d68e33d0a..92dc3b037 100644 --- a/tests/unit/test_updater.py +++ b/tests/unit/test_updater.py @@ -48,10 +48,14 @@ def test_is_outdated(latest, current, result, updater): @pytest.mark.skipif( os.getenv("TRAVIS_EVENT_TYPE") != "cron", reason = "Only run on travis CRON to avoid generating too much logs" -def test_check(updater, monkeypatch): - monkeypatch.setenv("DVC_TEST", "true") - monkeypatch.delenv("CI") +) +@mock.patch("dvc.updater.Updater._check") +def test_check(mock_check, updater, monkeypatch): + monkeypatch.delenv("CI", None) + monkeypatch.setenv("DVC_TEST", False) updater.check() updater.check() updater.check() + + assert mock_check.call_count == 3 From e519464aca3bf40892f3f0c66faae4d790da057d Mon Sep 17 00:00:00 2001 From: "Mr. Outis" Date: Tue, 17 Dec 2019 12:21:50 -0600 Subject: [PATCH 5/5] :nail_care: black --- tests/unit/test_updater.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_updater.py b/tests/unit/test_updater.py index 92dc3b037..f0e350528 100644 --- a/tests/unit/test_updater.py +++ b/tests/unit/test_updater.py @@ -47,7 +47,7 @@ def test_is_outdated(latest, current, result, updater): @pytest.mark.skipif( os.getenv("TRAVIS_EVENT_TYPE") != "cron", - reason = "Only run on travis CRON to avoid generating too much logs" + reason="Only run on travis CRON to avoid generating too much logs", ) @mock.patch("dvc.updater.Updater._check") def test_check(mock_check, updater, monkeypatch):