File: test_utils.py

package info (click to toggle)
python-transmission-rpc 7.0.11-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 440 kB
  • sloc: python: 2,605; sh: 9; makefile: 4
file content (118 lines) | stat: -rw-r--r-- 3,344 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 2008-12, Erik Svensson <erik.public@gmail.com>
# Copyright (c) 2018-2020 Trim21 <i@trim21.me>
# Licensed under the MIT license.
from __future__ import annotations

import datetime
from typing import Any
from unittest import mock

import pytest

from transmission_rpc import from_url, utils
from transmission_rpc.constants import DEFAULT_TIMEOUT, LOGGER


def assert_almost_eq(value: float, expected: float):
    assert abs(value - expected) < 1


@pytest.mark.parametrize(
    ("size", "expected"),
    {
        512: (512, "B"),
        1024: (1.0, "KiB"),
        1048575: (1023.999, "KiB"),
        1048576: (1.0, "MiB"),
        1073741824: (1.0, "GiB"),
        1099511627776: (1.0, "TiB"),
        1125899906842624: (1.0, "PiB"),
        1152921504606846976: (1.0, "EiB"),
    }.items(),
)
def test_format_size(size, expected: tuple[float, str]):
    result = utils.format_size(size)
    assert_almost_eq(result[0], expected[0])
    assert result[1] == expected[1]


@pytest.mark.parametrize(
    ("size", "expected"),
    [
        (512, (512, "B/s")),
        (1024, (1.0, "KiB/s")),
        (1048575, (1023.999, "KiB/s")),
        (1048576, (1.0, "MiB/s")),
        (1073741824, (1.0, "GiB/s")),
        (1099511627776, (1.0, "TiB/s")),
        (1125899906842624, (1.0, "PiB/s")),
        (1152921504606846976, (1.0, "EiB/s")),
    ],
)
def test_format_speed(size, expected):
    result = utils.format_speed(size)
    assert_almost_eq(result[0], expected[0])
    assert result[1] == expected[1]


@pytest.mark.parametrize(
    ("delta", "expected"),
    {
        datetime.timedelta(0, 0): "0 00:00:00",
        datetime.timedelta(0, 10): "0 00:00:10",
        datetime.timedelta(0, 60): "0 00:01:00",
        datetime.timedelta(0, 61): "0 00:01:01",
        datetime.timedelta(0, 3661): "0 01:01:01",
        datetime.timedelta(1, 3661): "1 01:01:01",
        datetime.timedelta(13, 65660): "13 18:14:20",
    }.items(),
)
def test_format_timedelta(delta, expected):
    assert utils.format_timedelta(delta), expected


@pytest.mark.parametrize(
    ("url", "kwargs"),
    {
        "http://a:b@127.0.0.1:9092/transmission/rpc": {
            "protocol": "http",
            "username": "a",
            "password": "b",
            "host": "127.0.0.1",
            "port": 9092,
            "path": "/transmission/rpc",
        },
        "http://127.0.0.1/transmission/rpc": {
            "protocol": "http",
            "username": None,
            "password": None,
            "host": "127.0.0.1",
            "port": 80,
            "path": "/transmission/rpc",
        },
        "https://127.0.0.1/tr/transmission/rpc": {
            "protocol": "https",
            "username": None,
            "password": None,
            "host": "127.0.0.1",
            "port": 443,
            "path": "/tr/transmission/rpc",
        },
        "https://127.0.0.1/": {
            "protocol": "https",
            "username": None,
            "password": None,
            "host": "127.0.0.1",
            "port": 443,
            "path": "/",
        },
    }.items(),
)
def test_from_url(url: str, kwargs: dict[str, Any]):
    with mock.patch("transmission_rpc.Client") as m:
        from_url(url)
        m.assert_called_once_with(
            **kwargs,
            timeout=DEFAULT_TIMEOUT,
            logger=LOGGER,
        )