File: test_version_helpers.py

package info (click to toggle)
knockpy 9.0.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 560 kB
  • sloc: python: 6,435; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations

import knockpy.version as version


class _DummyResponse:
    def __init__(self, status_code: int, payload):
        self.status_code = status_code
        self._payload = payload

    def json(self):
        return self._payload


def test_version_key_and_compare():
    assert version._version_key("9.0.0") == (9, 0, 0)
    assert version._version_key("9.0.0rc1") == (9, 0, 0, 1)
    assert version.is_newer_version("9.1.0", "9.0.9") is True
    assert version.is_newer_version("9.0.0", "9.0.0") is False
    assert version.is_newer_version("9.0", "9.0.0") is False


def test_check_latest_version_update_available(monkeypatch):
    monkeypatch.setattr(
        version.httpx,
        "get",
        lambda *args, **kwargs: _DummyResponse(200, {"info": {"version": "9.1.0"}}),
    )
    info = version.check_latest_version(current="9.0.0")
    assert info["ok"] is True
    assert info["latest"] == "9.1.0"
    assert info["update_available"] is True


def test_check_latest_version_no_update(monkeypatch):
    monkeypatch.setattr(
        version.httpx,
        "get",
        lambda *args, **kwargs: _DummyResponse(200, {"info": {"version": "9.0.0"}}),
    )
    info = version.check_latest_version(current="9.0.0")
    assert info["ok"] is True
    assert info["update_available"] is False


def test_check_latest_version_handles_http_error(monkeypatch):
    monkeypatch.setattr(
        version.httpx,
        "get",
        lambda *args, **kwargs: _DummyResponse(503, {"info": {"version": "9.1.0"}}),
    )
    info = version.check_latest_version(current="9.0.0")
    assert info["ok"] is False
    assert "HTTP 503" in str(info["error"])


def test_check_latest_version_handles_exception(monkeypatch):
    def _boom(*args, **kwargs):
        raise RuntimeError("network down")

    monkeypatch.setattr(version.httpx, "get", _boom)
    info = version.check_latest_version(current="9.0.0")
    assert info["ok"] is False
    assert "RuntimeError" in str(info["error"])