Debian Codegraph @

tests/test_json.py

raw
1 from __future__ import annotations
2
3 from prettytable import PrettyTable, from_json
4
5
6 class TestJSONOutput:
7 def test_json_output(self, helper_table: PrettyTable) -> None:
8 result = helper_table.get_json_string()
9 assert (
10 result.strip()
11 == """
12 [
13 [
14 "",
15 "Field 1",
16 "Field 2",
17 "Field 3"
18 ],
19 {
20 "": 1,
21 "Field 1": "value 1",
22 "Field 2": "value2",
23 "Field 3": "value3"
24 },
25 {
26 "": 4,
27 "Field 1": "value 4",
28 "Field 2": "value5",
29 "Field 3": "value6"
30 },
31 {
32 "": 7,
33 "Field 1": "value 7",
34 "Field 2": "value8",
35 "Field 3": "value9"
36 }
37 ]""".strip()
38 )
39 options = {"fields": ["Field 1", "Field 3"]}
40 result = helper_table.get_json_string(**options)
41 assert (
42 result.strip()
43 == """
44 [
45 [
46 "Field 1",
47 "Field 3"
48 ],
49 {
50 "Field 1": "value 1",
51 "Field 3": "value3"
52 },
53 {
54 "Field 1": "value 4",
55 "Field 3": "value6"
56 },
57 {
58 "Field 1": "value 7",
59 "Field 3": "value9"
60 }
61 ]""".strip()
62 )
63
64 def test_json_output_options(self, helper_table: PrettyTable) -> None:
65 result = helper_table.get_json_string(
66 header=False, indent=None, separators=(",", ":")
67 )
68 assert (
69 result
70 == """[{"":1,"Field 1":"value 1","Field 2":"value2","Field 3":"value3"},"""
71 """{"":4,"Field 1":"value 4","Field 2":"value5","Field 3":"value6"},"""
72 """{"":7,"Field 1":"value 7","Field 2":"value8","Field 3":"value9"}]"""
73 )
74
75
76 class TestJSONConstructor:
77 def test_json_and_back(self, city_data: PrettyTable) -> None:
78 json_string = city_data.get_json_string()
79 new_table = from_json(json_string)
80 assert new_table.get_string() == city_data.get_string()