-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_csv_converter.py
108 lines (89 loc) · 6.22 KB
/
test_csv_converter.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
import json
import os
import unittest
from csv_converter import CSVConverter
fo = 'test_files'
class MyTestCase(unittest.TestCase):
def test_error_if_no_config(self):
with self.assertRaises(Exception):
CSVConverter(config_json=None, config_file_name=None, config_dict=None)
with self.assertRaises(Exception):
CSVConverter()
def test_error_if_bad_config(self):
with self.assertRaises(Exception):
CSVConverter(config_json='Not real valid JSON here...')
with self.assertRaises(Exception):
CSVConverter(config_file_name=os.path.join(fo, 'non-existant-file-hfhffhjksdfhjksfhjk'))
with self.assertRaises(Exception):
CSVConverter(config_file_name=os.path.join(fo, 'real-but-invalid.json'))
with self.assertRaises(Exception):
# noinspection PyTypeChecker
CSVConverter(config_dict=['This isn', 't a dict'])
def test_simple_case(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_case_twice_no_append(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_case_twice_with_append(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'), append_mode=True)
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple-two.csv'))
with open(os.path.join(fo, 'valid-simple-appended.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_out_of_order_case(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple-out-of-order.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_with_defaults(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-with-defaults.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple-with-missing.csv'))
with open(os.path.join(fo, 'valid-simple-with-defaults.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_with_lambda(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-with-lambda.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-to-lower.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_with_funlinks(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-with-funlink.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-double-digits.csv')) as correct_file:
self.assertEqual(correct_file.read(), output.replace('\r\n', '\n'))
def test_append_two_simple(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'))
output = converter.convert(input_file_name=[os.path.join(fo, 'valid-simple.csv'),
os.path.join(fo, 'valid-simple-two.csv')])
with open(os.path.join(fo, 'valid-simple-appended.csv')) as correct_file:
self.assertEqual(correct_file.read(), output.replace('\r\n', '\n'))
def test_xlsx_simple(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple.xlsx'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_case_with_junk_before_heading(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change-with-hints.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple-with-junk-before-heading.csv'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_xlsx_simple_case_with_junk_before_heading(self):
converter = CSVConverter(config_file_name=os.path.join(fo, 'valid-simple-header-change-with-hints.json'))
output = converter.convert(input_file_name=os.path.join(fo, 'valid-simple-with-junk-before-heading.xlsx'))
with open(os.path.join(fo, 'valid-simple-new.csv')) as output_file:
self.assertEqual(output_file.read(), output.replace('\r\n', '\n'))
def test_simple_autogenerated_from_csv(self):
output = CSVConverter(no_config=True).generate_json_headers(input_file_name=os.path.join(fo, 'valid-simple.csv'))
with open(os.path.join(fo, 'valid-simple-autogenerated.json')) as output_file:
self.assertEqual(json.load(output_file), output)
if __name__ == '__main__':
unittest.main()