File: test_memory.py

package info (click to toggle)
qtile 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,004 kB
  • sloc: python: 49,959; ansic: 4,371; xml: 324; sh: 260; makefile: 218
file content (72 lines) | stat: -rw-r--r-- 2,157 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
import sys
from importlib import reload
from types import ModuleType

import pytest

import libqtile.bar
import libqtile.config


def no_op(*args, **kwargs):
    pass


class FakePsutil(ModuleType):
    class virtual_memory:  # noqa: N801
        def __init__(self):
            self.used = 2534260736  # 2,474,864K, 2,417M, 2.36G
            self.total = 8180686848  # 7,988,952K, 7,802M, 7.72G
            self.free = 2354114560
            self.available = 5646426112
            self.percent = 39.4
            self.buffers = 346394624
            self.active = 1132359680
            self.inactive = 3862183936
            self.shared = 516395008

    class swap_memory:  # noqa: N801
        def __init__(self):
            self.total = 8429498368
            self.used = 0
            self.free = 8429498368
            self.percent = 0.0


@pytest.fixture()
def patched_memory(
    monkeypatch,
):
    monkeypatch.setitem(sys.modules, "psutil", FakePsutil("psutil"))
    from libqtile.widget import memory

    reload(memory)
    return memory


def test_memory_defaults(manager_nospawn, minimal_conf_noscreen, patched_memory):
    """Test no text when free space over threshold"""
    widget = patched_memory.Memory()
    config = minimal_conf_noscreen
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([widget], 10))]
    manager_nospawn.start(config)
    assert manager_nospawn.c.widget["memory"].info()["text"] == " 2417M/ 7802M"


@pytest.mark.parametrize(
    "unit,expects",
    [
        ("G", " 2G/ 8G"),
        ("M", " 2417M/ 7802M"),
        ("K", " 2474864K/ 7988952K"),
        ("B", " 2534260736B/ 8180686848B"),
    ],
)
def test_memory_units(manager_nospawn, minimal_conf_noscreen, patched_memory, unit, expects):
    """Test no text when free space over threshold"""
    widget = patched_memory.Memory(measure_mem=unit)
    config = minimal_conf_noscreen
    config.screens = [libqtile.config.Screen(top=libqtile.bar.Bar([widget], 10))]
    manager_nospawn.start(config)
    manager_nospawn.c.widget["memory"].eval("self.update(self.poll())")
    assert manager_nospawn.c.widget["memory"].info()["text"] == expects