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
|
from __future__ import annotations
import logging
import pytest
from inline_snapshot import snapshot
from pydantic import BaseModel
from pydantic import Field
from pytest import LogCaptureFixture
from zabbix_cli.models import MetaKey
from zabbix_cli.models import TableRenderable
@pytest.mark.parametrize(
"header, expect",
[
pytest.param(None, "Foo", id="None is default"),
pytest.param("", "Foo", id="Empty string is default"),
("Foo Header", "Foo Header"),
],
)
def test_table_renderable_metakey_header(header: str, expect: str) -> None:
class TestTableRenderable(TableRenderable):
foo: str = Field(..., json_schema_extra={MetaKey.HEADER: header})
t = TestTableRenderable(foo="bar")
assert t.__cols__() == [expect]
assert t.__rows__() == [["bar"]]
assert t.__cols_rows__() == ([expect], [["bar"]])
@pytest.mark.parametrize(
"content, join_char, expect",
[
(["a", "b", "c"], ",", ["a,b,c"]),
(["a", "b", "c"], "|", ["a|b|c"]),
(["a", "b", "c"], " ", ["a b c"]),
(["a", "b", "c"], "", ["abc"]),
# Test empty list
([], ",", [""]),
([], "|", [""]),
([], " ", [""]),
([], "", [""]),
],
)
def test_table_renderable_metakey_join_char(
content: list[str], join_char: str, expect: str
) -> None:
class TestTableRenderable(TableRenderable):
foo: list[str] = Field(..., json_schema_extra={MetaKey.JOIN_CHAR: join_char})
t = TestTableRenderable(foo=content)
assert t.__rows__() == [expect]
def test_all_metakeys() -> None:
class TestTableRenderable(TableRenderable):
foo: list[str] = Field(
...,
json_schema_extra={MetaKey.JOIN_CHAR: "|", MetaKey.HEADER: "Foo Header"},
)
t = TestTableRenderable(foo=["foo", "bar"])
assert t.__cols__() == ["Foo Header"]
assert t.__rows__() == [["foo|bar"]]
assert t.__cols_rows__() == (["Foo Header"], [["foo|bar"]])
def test_rows_with_unknown_base_model(caplog: LogCaptureFixture) -> None:
"""Test that we log when we try to render a BaseModel
instance that does not inherit from TableRenderable.
"""
class FooModel(BaseModel):
foo: str
bar: int
baz: float
qux: list[str]
class TestTableRenderable(TableRenderable):
foo: FooModel
t = TestTableRenderable(foo=FooModel(foo="foo", bar=1, baz=1.0, qux=["a", "b"]))
caplog.set_level(logging.WARNING)
# Non-TableRenderable models are rendered as JSON
assert t.__rows__() == snapshot(
[
[
"""\
{
"foo": "foo",
"bar": 1,
"baz": 1.0,
"qux": [
"a",
"b"
]
}\
"""
]
]
)
# Check that we logged info on what happened and how we got there
assert caplog.record_tuples == snapshot(
[("zabbix_cli", 30, "Cannot render FooModel as a table.")]
)
record = caplog.records[0]
assert record.funcName == "__rows__"
assert record.stack_info is not None
assert "test_rows_with_unknown_base_model" in record.stack_info
|