-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_io.py
205 lines (156 loc) · 5.32 KB
/
test_io.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import os
import json
import unittest
from textwrap import dedent
import pyexcel as pe
from .fixtures import EXPECTED_RESULTS
class TestIO(unittest.TestCase):
TABLEFMT = 'simple'
expected_results = EXPECTED_RESULTS['simple']
def setUp(self):
self.testfile = 'testfile.%s' % self.TABLEFMT
self.testfile2 = None
def _check_test_file(self, name):
with open(self.testfile, "r") as f:
written_content = f.read()
written_content = written_content.strip('\n')
self.assertTrue(name in self.expected_results,
'expected result missing: %s' % written_content)
expected = self.expected_results[name]
self.assertEqual(written_content, expected)
def test_no_title_multiple_sheets(self):
adict = {
'sheet 1': [[1, 2], [3, 4]],
'sheet 2': [[5, 6], [7, 8]]
}
pe.save_book_as(bookdict=adict, dest_file_name=self.testfile,
dest_write_title=False)
self._check_test_file('no_title_multiple_sheets')
def test_dict(self):
adict = {
'sheet 1': [[1, 2], [3, 4]],
'sheet 2': [[5, 6], [7, 8]]
}
pe.save_book_as(bookdict=adict, dest_file_name=self.testfile)
self._check_test_file('dict')
def test_normal_usage(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8, 999]
]
s = pe.Sheet(content)
s.save_as(self.testfile)
self._check_test_file('normal_usage')
def test_new_normal_usage(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8, 999]
]
pe.save_as(array=content, dest_file_name=self.testfile)
self._check_test_file('new_normal_usage')
def test_no_title_single_sheet(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8, 999]
]
pe.save_as(array=content, dest_file_name=self.testfile,
dest_write_title=False)
self._check_test_file('no_title_single_sheet')
def test_new_normal_usage_irregular_columns(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8]
]
pe.save_as(array=content, dest_file_name=self.testfile)
self._check_test_file('new_normal_usage_irregular_columns')
def test_csvbook_irregular_columns(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8]
]
self.testfile2 = "testfile.csv"
pe.save_as(array=content, dest_file_name=self.testfile2)
pe.save_as(file_name=self.testfile2, dest_file_name=self.testfile)
self._check_test_file('csvbook_irregular_columns')
def test_column_series(self):
content = [
["Column 1", "Column 2", "Column 3"],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
pe.save_as(array=content, name_columns_by_row=0,
dest_file_name=self.testfile)
self._check_test_file('column_series')
def test_column_series_irregular_columns(self):
content = [
["Column 1", "Column 2", "Column 3"],
[1, 2, 3],
[4, 5, 6],
[7, 8, '']
]
pe.save_as(array=content, name_columns_by_row=0,
dest_file_name=self.testfile)
self._check_test_file('column_series_irregular_columns')
def test_data_frame(self):
content = [
["", "Column 1", "Column 2", "Column 3"],
["Row 1", 1, 2, 3],
["Row 2", 4, 5, 6],
["Row 3", 7, 8, 9]
]
pe.save_as(array=content, name_rows_by_column=0, name_columns_by_row=0,
dest_file_name=self.testfile)
self._check_test_file('data_frame')
def test_row_series(self):
content = [
["Row 1", 1, 2, 3],
["Row 2", 4, 5, 6],
["Row 3", 7, 8, 9]
]
pe.save_as(array=content, name_rows_by_column=0,
dest_file_name=self.testfile)
self._check_test_file('row_series')
def tearDown(self):
if os.path.exists(self.testfile):
os.unlink(self.testfile)
if self.testfile2 and os.path.exists(self.testfile2):
os.unlink(self.testfile2)
class TestRst(TestIO):
TABLEFMT = 'rst'
expected_results = EXPECTED_RESULTS['rst']
class TestHTML(TestIO):
TABLEFMT = 'html'
expected_results = EXPECTED_RESULTS['html']
class TestJSON(TestIO):
TABLEFMT = 'json'
expected_results = EXPECTED_RESULTS['json']
def _check_test_file(self, name):
with open(self.testfile, "r") as f:
json.load(f)
super(TestJSON, self)._check_test_file(name)
class TestStream(unittest.TestCase):
def test_normal_usage(self):
content = [
[1, 2, 3],
[4, 588, 6],
[7, 8, 999]
]
s = pe.Sheet(content)
# note, plus the trailing '\n'
# due to tabluate produces the extra new line
content = dedent("""
pyexcel sheet:
- --- ---
1 2 3
4 588 6
7 8 999
- --- ---""").strip('\n')
self.assertEqual(s.simple, content)
if __name__ == "__main__":
unittest.main()