Coverage for tests/output/test_render.py: 100%
62 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
« prev ^ index » next coverage.py v6.5.0, created at 2023-02-09 12:09 +0100
1from __future__ import annotations
3from typing import Any
5import pytest
6from pydantic import BaseModel
7from pytest import CaptureFixture
8from pytest_mock import MockerFixture
10from harbor_cli.format import OutputFormat
11from harbor_cli.output import render
12from harbor_cli.output.render import render_json
13from harbor_cli.output.render import render_result
14from harbor_cli.output.render import render_table
15from harbor_cli.state import state
18# The actual testing of the render functions is done in test_render_<format>()
19def test_render_result_json(mocker: MockerFixture) -> None:
20 """Test that we can render a result."""
21 spy = mocker.spy(render, "render_json")
22 state.config.output.format = OutputFormat.JSON
23 result = {"a": 1}
24 render_result(result)
25 spy.assert_called_once()
28def test_render_result_table(mocker: MockerFixture) -> None:
29 """Test that we can render a result."""
30 spy = mocker.spy(render, "render_table")
31 state.config.output.format = OutputFormat.TABLE
32 result = {"a": 1}
33 render_result(result)
34 spy.assert_called_once()
37class SomeModel(BaseModel):
38 a: int
39 b: str
42# assume JSON indent is 2
43@pytest.mark.parametrize(
44 "inp,expected",
45 [
46 ({}, "{}"),
47 ([], "[]"),
48 ({"a": 1}, '{\n "a": 1\n}'),
49 ({"a": 1}, '{\n "a": 1\n}'),
50 (SomeModel(a=1, b="2"), '{\n "a": 1,\n "b": "2"\n}'),
51 ],
52)
53def test_render_json(capsys: CaptureFixture, inp: Any, expected: str) -> None:
54 state.config.output.JSON.indent = 2
55 render_json(inp)
56 out, _ = capsys.readouterr()
57 assert out == expected + "\n"
60def test_render_table(capsys: CaptureFixture) -> None:
61 # FIXME: not sure how to test this
62 render_table({"a": 1})
63 out, _ = capsys.readouterr()
64 assert out not in ["", "\n"]
67def test_render_table_compact_mock(
68 mocker: MockerFixture, compact_table_renderable: BaseModel
69) -> None:
70 """Test that we can render a result as a compact table."""
71 # FIXME: getting the following error when attempting to create mock
72 # for render_table and render_table_compact when using hypothesis strategy:
73 # TypeError: __name__ must be set to a string object
75 full_table_spy = mocker.spy(render, "render_table_full")
76 compact_table_spy = mocker.spy(render, "render_table_compact")
78 state.config.output.format = OutputFormat.TABLE
79 state.config.output.table.compact = True
80 render_table(compact_table_renderable)
82 # Check our spies
83 compact_table_spy.assert_called_once()
84 full_table_spy.assert_not_called()
87def test_render_table_compact_fallback(mocker: MockerFixture) -> None:
88 """Tests that a model with no compact table implementation is rendered
89 via the fallback full table implementation."""
90 full_table_spy = mocker.spy(render, "render_table_full")
91 compact_table_spy = mocker.spy(render, "render_table_compact")
93 # Activate compact table mode
94 state.config.output.format = OutputFormat.TABLE
95 state.config.output.table.compact = True
97 model = SomeModel(a=1, b="2")
98 render_table(model)
100 # Check our spies
101 compact_table_spy.assert_called_once() # we try to call it before falling back
102 full_table_spy.assert_called()
105# TODO: fix not being able to combine mocker and hypothesis
106def test_render_table_full_mock(
107 mocker: MockerFixture, compact_table_renderable: BaseModel
108) -> None:
109 """Test that we can render a result as a compact table."""
110 # FIXME: getting the following error when attempting to create mock
111 # for render_table and render_table_compact:
112 # TypeError: __name__ must be set to a string object
114 full_table_spy = mocker.spy(render, "render_table_full")
115 compact_table_spy = mocker.spy(render, "render_table_compact")
117 # Deactivate compact tables
118 state.config.output.format = OutputFormat.TABLE
119 state.config.output.table.compact = False
120 render_table(compact_table_renderable)
122 # Check our spies
123 compact_table_spy.assert_not_called()
124 full_table_spy.assert_called()