test_net_render.py 1.51 KB
Newer Older
one's avatar
one committed
1
"""Tests for hytop.net formatting helpers."""
one's avatar
one committed
2
3
4

from __future__ import annotations

one's avatar
one committed
5
from hytop.net.app import format_iface_name, format_rate, split_iface_key
one's avatar
one committed
6
7
8
9
10
11
12
13
14
15
16


class TestFormatIfaceName:
    def test_keep_name_when_up(self):
        assert format_iface_name("eth0", "up") == "eth0"

    def test_mark_down_state(self):
        assert format_iface_name("eth0", "down") == "eth0 (down)"

    def test_mark_init_state(self):
        assert format_iface_name("mlx5_0/p1", "init") == "mlx5_0/p1 (down)"
17
18
19
20


class TestFormatRate:
    def test_zero(self):
one's avatar
one committed
21
        assert format_rate(0.0) == "0.00 B/s"
22
23

    def test_bytes(self):
one's avatar
one committed
24
        assert format_rate(512.0) == "512.00 B/s"
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

    def test_kilobytes(self):
        result = format_rate(1500.0)
        assert "kB/s" in result

    def test_megabytes(self):
        result = format_rate(2_000_000.0)
        assert "MB/s" in result

    def test_gigabytes(self):
        result = format_rate(3_000_000_000.0)
        assert "GB/s" in result

    def test_iec_kibibytes(self):
        result = format_rate(2048.0, iec=True)
        assert "KiB/s" in result

    def test_iec_mebibytes(self):
        result = format_rate(2 * 1024 * 1024.0, iec=True)
        assert "MiB/s" in result

    def test_iec_gibibytes(self):
        result = format_rate(2 * 1024**3, iec=True)
        assert "GiB/s" in result


class TestSplitIfaceKey:
    def test_eth(self):
        assert split_iface_key("eth:eth0") == ("eth", "eth0")

    def test_ib(self):
        assert split_iface_key("ib:mlx5_0/p1") == ("ib", "mlx5_0/p1")