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
|
from __future__ import annotations
from prettytable import PrettyTable, from_json
class TestJSONOutput:
def test_json_output(self, helper_table: PrettyTable) -> None:
result = helper_table.get_json_string()
assert (
result.strip()
== """
[
[
"",
"Field 1",
"Field 2",
"Field 3"
],
{
"": 1,
"Field 1": "value 1",
"Field 2": "value2",
"Field 3": "value3"
},
{
"": 4,
"Field 1": "value 4",
"Field 2": "value5",
"Field 3": "value6"
},
{
"": 7,
"Field 1": "value 7",
"Field 2": "value8",
"Field 3": "value9"
}
]""".strip()
)
options = {"fields": ["Field 1", "Field 3"]}
result = helper_table.get_json_string(**options)
assert (
result.strip()
== """
[
[
"Field 1",
"Field 3"
],
{
"Field 1": "value 1",
"Field 3": "value3"
},
{
"Field 1": "value 4",
"Field 3": "value6"
},
{
"Field 1": "value 7",
"Field 3": "value9"
}
]""".strip()
)
def test_json_output_options(self, helper_table: PrettyTable) -> None:
result = helper_table.get_json_string(
header=False, indent=None, separators=(",", ":")
)
assert (
result
== """[{"":1,"Field 1":"value 1","Field 2":"value2","Field 3":"value3"},"""
"""{"":4,"Field 1":"value 4","Field 2":"value5","Field 3":"value6"},"""
"""{"":7,"Field 1":"value 7","Field 2":"value8","Field 3":"value9"}]"""
)
class TestJSONConstructor:
def test_json_and_back(self, city_data: PrettyTable) -> None:
json_string = city_data.get_json_string()
new_table = from_json(json_string)
assert new_table.get_string() == city_data.get_string()
|