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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
from __future__ import annotations
from pathlib import Path
from typing import Any
import attr
import pytest
from pgactivity.config import Configuration, ConfigurationError, Flag, UISection
def test_flag() -> None:
f = Flag(3)
assert f == Flag.APPNAME | Flag.DATABASE
assert f | Flag.CLIENT == Flag.CLIENT | Flag.APPNAME | Flag.DATABASE
f ^= Flag.APPNAME
assert f == Flag.DATABASE
def test_flag_load() -> None:
options = {
"appname": True,
"client": True,
"cpu": True,
"database": True,
"mem": True,
"pid": None,
"read": True,
"relation": True,
"time": True,
"user": True,
"wait": None,
"write": True,
"xmin": True,
}
flag = Flag.load(None, is_local=True, **options)
assert (
flag
== Flag.PID
| Flag.IOWAIT
| Flag.MODE
| Flag.TYPE
| Flag.RELATION
| Flag.WAIT
| Flag.TIME
| Flag.WRITE
| Flag.READ
| Flag.MEM
| Flag.CPU
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
| Flag.DATABASE
| Flag.XMIN
)
cfg = Configuration(
name="test",
values=dict(
pid=UISection(hidden=True),
relation=UISection(hidden=False),
wait=UISection(hidden=False),
),
)
flag = Flag.load(cfg, is_local=False, **options)
assert (
flag
== Flag.MODE
| Flag.TYPE
| Flag.RELATION
| Flag.WAIT
| Flag.TIME
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
| Flag.DATABASE
| Flag.XMIN
)
options["database"] = False
options["time"] = False
options["pid"] = False
cfg = Configuration(
name="test",
values=dict(database=UISection(hidden=False), relation=UISection(hidden=True)),
)
flag = Flag.load(cfg, is_local=False, **options)
assert (
flag
== Flag.MODE
| Flag.TYPE
| Flag.WAIT
| Flag.USER
| Flag.CLIENT
| Flag.APPNAME
| Flag.XMIN
)
def asdict(cfg: Configuration) -> dict[str, Any]:
return {k: attr.asdict(v) for k, v in cfg.items()}
def test_error() -> None:
cfg = Configuration(name="test", values={})
with pytest.raises(ConfigurationError, match="test error") as cm:
raise cfg.error("test error")
assert cm.value.filename == "test"
def test_lookup(tmp_path: Path) -> None:
cfg = Configuration.lookup(None, user_config_home=tmp_path)
assert cfg is None
(tmp_path / "pg_activity.conf").write_text(
"\n".join(
[
"[client]",
"width=5",
"color=cyan",
"[header]",
"show_instance=no",
]
)
)
cfg = Configuration.lookup(None, user_config_home=tmp_path)
assert cfg is not None and asdict(cfg) == {
"client": {"hidden": False, "width": 5, "color": "cyan"},
"header": {"show_instance": False, "show_system": True, "show_workers": True},
}
(tmp_path / "pg_activity").mkdir()
(tmp_path / "pg_activity" / "x.conf").write_text(
"\n".join(
["[database]", "hidden= on", "width = 3 ", "[header]", "show_workers=no"]
)
)
cfg = Configuration.lookup("x", user_config_home=tmp_path)
assert cfg is not None and asdict(cfg) == {
"database": {"hidden": True, "width": 3, "color": None},
"header": {"show_instance": True, "show_system": True, "show_workers": False},
}
with pytest.raises(FileNotFoundError):
Configuration.lookup("y", user_config_home=tmp_path)
no_header = {
"header": {k: False for k in ("show_instance", "show_system", "show_workers")}
}
columns = (
"database",
"user",
"client",
"cpu",
"mem",
"read",
"write",
"appname",
"xmin",
)
narrow = {k: {"hidden": True, "width": None, "color": None} for k in columns}
wide = {k: {"hidden": False, "width": None, "color": None} for k in columns}
minimal = {**no_header, **narrow}
@pytest.mark.parametrize(
"profile, expected",
[
("minimal", minimal),
("narrow", narrow),
("wide", wide),
],
)
def test_lookup_builtin_profiles(
tmp_path: Path, profile: str, expected: dict[str, Any]
) -> None:
cfg = Configuration.lookup(profile, user_config_home=tmp_path)
assert cfg is not None and asdict(cfg) == expected
|