File: test_output.py

package info (click to toggle)
lexicon 3.21.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 72,688 kB
  • sloc: python: 20,075; sh: 94; makefile: 7
file content (78 lines) | stat: -rw-r--r-- 2,156 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
73
74
75
76
77
78
"""Ensure that stdout corresponds to the given reference output"""

import json

from lexicon._private import cli

DATA = [
    {
        "id": "fake-id",
        "type": "TXT",
        "name": "fake.example.com",
        "content": "fake",
        "ttl": 3600,
    },
    {
        "id": "fake2-id",
        "type": "TXT",
        "name": "fake2.example.com",
        "content": "fake2",
        "ttl": 3600,
    },
]


def assert_correct_output(capsys, expected_output_lines):
    out, _ = capsys.readouterr()
    assert out.splitlines() == expected_output_lines


def test_output_function_outputs_json_as_table(capsys):
    expected_output_lines = [
        "ID       TYPE NAME              CONTENT TTL ",
        "-------- ---- ----------------- ------- ----",
        "fake-id  TXT  fake.example.com  fake    3600",
        "fake2-id TXT  fake2.example.com fake2   3600",
    ]

    cli.handle_output(DATA, "TABLE", "list")
    assert_correct_output(capsys, expected_output_lines)


def test_output_function_outputs_json_as_table_with_no_header(capsys):
    expected_output_lines = [
        "fake-id  TXT fake.example.com  fake  3600",
        "fake2-id TXT fake2.example.com fake2 3600",
    ]

    cli.handle_output(DATA, "TABLE-NO-HEADER", "list")
    assert_correct_output(capsys, expected_output_lines)


def test_output_function_outputs_json_as_json_string(capsys):
    cli.handle_output(DATA, "JSON", "list")

    out, _ = capsys.readouterr()
    json_data = json.loads(out)

    assert json_data == DATA


def test_output_function_output_nothing_when_quiet(capsys):
    expected_output_lines = []

    cli.handle_output(DATA, "QUIET", "list")
    assert_correct_output(capsys, expected_output_lines)


def test_output_function_outputs_nothing_with_not_a_json_serializable(capsys):
    expected_output_lines = []

    cli.handle_output(object(), "TABLE", "list")
    assert_correct_output(capsys, expected_output_lines)

    cli.handle_output(object(), "TABLE-NO-HEADER", "list")
    assert_correct_output(capsys, expected_output_lines)

    cli.handle_output(object(), "JSON", "list")
    assert_correct_output(capsys, expected_output_lines)