-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
test_toml_writer.py
188 lines (168 loc) · 4.15 KB
/
test_toml_writer.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# encoding: utf-8
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
from __future__ import absolute_import, print_function, unicode_literals
import collections
import itertools
from decimal import Decimal
import pytablewriter as ptw
import pytest
import toml
from dateutil.parser import parse
from ._common import print_test_result
from .data import float_header_list, float_value_matrix, headers, value_matrix
Data = collections.namedtuple("Data", "table_name header value expected")
normal_test_data_list = [
Data(
table_name="normal",
header=headers,
value=value_matrix,
expected="""[[normal]]
a = 1
c = "a"
b = 123.1
e = 1
dd = 1
[[normal]]
a = 2
c = "bb"
b = 2.2
e = 2.2
dd = 2.2
[[normal]]
a = 3
c = "ccc"
b = 3.3
e = "cccc"
dd = 3
""",
),
Data(
table_name="sparse",
header=headers,
value=[
["1", "", "a", "1", None],
[None, 2.2, None, "2.2", 2.2],
[None, None, None, None, None],
[3, 3.3, "ccc", None, "cccc"],
[None, None, None, None, None],
],
expected="""[[sparse]]
a = 1
c = "a"
b = ""
dd = 1
[[sparse]]
dd = 2.2
b = 2.2
e = 2.2
[[sparse]]
a = 3
c = "ccc"
b = 3.3
e = "cccc"
""",
),
Data(
table_name="symbols",
header=["a!0", "a#1", "a.2$", "a_%3", "a-&4"],
value=[["a?b", "c d", "e+f", "g=h", "i*j"], [1, 2.0, 3.3, Decimal("4.4"), ""]],
expected="""[[symbols]]
"a-&4" = "i*j"
"a#1" = "c d"
"a_%3" = "g=h"
"a!0" = "a?b"
"a.2$" = "e+f"
[[symbols]]
"a-&4" = ""
"a#1" = 2
"a_%3" = 4.4
"a!0" = 1
"a.2$" = 3.3
""",
),
Data(
table_name="mixtype",
header=["int", "float", "bool", "datetime"],
value=[
[0, 2.2, True, parse("2017-01-02T03:04:05")],
[-1, Decimal("4.4"), False, parse("2022-01-01T00:00:00")],
],
expected="""[[mixtype]]
float = 2.2
datetime = 2017-01-02T03:04:05
int = 0
bool = true
[[mixtype]]
float = 4.4
datetime = 2022-01-01T00:00:00
int = -1
bool = false
""",
),
Data(
table_name="float",
header=float_header_list,
value=float_value_matrix,
expected="""[[float]]
a = 0.01
b = 0.00125
c = 0
[[float]]
a = 1
b = 99.9
c = 0.01
[[float]]
a = 1.2
b = 999999.123
c = 0.001
""",
),
]
exception_test_data_list = [
Data(table_name="dummy", header=header, value=value, expected=ptw.EmptyTableDataError)
for header, value in itertools.product([None, [], ""], [None, [], ""])
] + [
Data(table_name="empty_header", header=None, value=value_matrix, expected=ptw.EmptyHeaderError),
Data(table_name=None, header=headers, value=value_matrix, expected=ptw.EmptyTableNameError),
]
table_writer_class = ptw.TomlTableWriter
class Test_TomlTableWriter_write_new_line(object):
def test_normal(self, capsys):
writer = table_writer_class()
writer.write_null_line()
out, _err = capsys.readouterr()
assert out == "\n"
class Test_TomlTableWriter_write_table(object):
@pytest.mark.xfail(run=False)
@pytest.mark.parametrize(
["table_name", "header", "value", "expected"],
[
[data.table_name, data.header, data.value, data.expected]
for data in normal_test_data_list
],
)
def test_normal(self, capsys, table_name, header, value, expected):
writer = table_writer_class()
writer.table_name = table_name
writer.headers = header
writer.value_matrix = value
writer.write_table()
out, err = capsys.readouterr()
print_test_result(expected=expected, actual=out, error=err)
assert toml.loads(out) == toml.loads(expected)
@pytest.mark.parametrize(
["table_name", "header", "value", "expected"],
[
[data.table_name, data.header, data.value, data.expected]
for data in exception_test_data_list
],
)
def test_exception(self, capsys, table_name, header, value, expected):
writer = table_writer_class()
writer.table_name = table_name
writer.headers = header
writer.value_matrix = value
with pytest.raises(expected):
writer.write_table()