|
| 1 | +import random |
| 2 | + |
| 3 | +import mock |
| 4 | +import requests_mock as req_mock |
| 5 | +from packaging import version |
| 6 | + |
| 7 | +from up42.version import version_control |
| 8 | + |
| 9 | +fake_latest_version = "10.0.0" |
| 10 | +fake_installed_version = "1.0.0" |
| 11 | + |
| 12 | + |
| 13 | +class TestBuildMessage: |
| 14 | + def test_should_build_warn_message(self): |
| 15 | + assert ( |
| 16 | + version_control.build_outdated_version_message(fake_installed_version, fake_latest_version) |
| 17 | + == f"You're using an outdated version of the UP42 Python SDK: v{fake_installed_version}. A newer version is available: v{fake_latest_version}.\nPlease upgrade to the latest version using **pip install --upgrade up42-py** or conda **conda update -c conda-forge up42-py**." # pylint: disable=line-too-long # noqa: E501 |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +class TestCheckLatestVersion: |
| 22 | + def test_should_warn_if_not_latest_version( |
| 23 | + self, |
| 24 | + requests_mock: req_mock.Mocker, |
| 25 | + ): |
| 26 | + warn = mock.MagicMock() |
| 27 | + requests_mock.get(url="https://pypi.org/pypi/up42-py/json", json={"info": {"version": fake_latest_version}}) |
| 28 | + message = "not latest version" |
| 29 | + build_warning_message = mock.MagicMock(return_value=message) |
| 30 | + version_control.check_is_latest_version(fake_installed_version, warn, build_warning_message) |
| 31 | + build_warning_message.assert_called_with(fake_installed_version, version.Version(fake_latest_version)) |
| 32 | + warn.assert_called_with(message) |
| 33 | + |
| 34 | + def test_should_ignore_http_error_exception( |
| 35 | + self, |
| 36 | + requests_mock: req_mock.Mocker, |
| 37 | + ): |
| 38 | + requests_mock.get(url="https://pypi.org/pypi/up42-py/json", status_code=random.randint(400, 599)) |
| 39 | + unused = mock.MagicMock() |
| 40 | + version_control.check_is_latest_version(fake_installed_version, unused, unused) |
| 41 | + unused.assert_not_called() |
0 commit comments