-
Notifications
You must be signed in to change notification settings - Fork 97
/
tests.py
193 lines (159 loc) · 7.07 KB
/
tests.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
import unittest
import json
from json2csv import Json2Csv, MultiLineJson2Csv
from gen_outline import make_outline
class TestJson2Csv(unittest.TestCase):
def test_init(self):
outline = {'map': [['some_header', 'some_key']]}
loader = Json2Csv(outline)
self.assertIn('some_header', loader.key_map)
self.assertRaises(ValueError, Json2Csv, None)
self.assertRaises(ValueError, Json2Csv, {})
def test_process_row(self):
"""Given a valid key-map and data, it should return a valid row"""
outline = {'map': [['id', '_id'], ['count', 'count']]}
loader = Json2Csv(outline)
test_data = json.loads('{"_id" : "Someone","count" : 1}')
row = loader.process_row(test_data)
self.assertIs(type(row), dict)
self.assertIn('id', row.keys())
self.assertIn('count', row.keys())
self.assertEquals(row['id'], 'Someone')
self.assertEquals(row['count'], 1)
def test_process_row_nested_data(self):
"""Ensure that nested keys (with . notation) are processed"""
key_map = {"map": [['author', 'source.author'], ['message', 'message.original']]}
loader = Json2Csv(key_map)
test_data = json.loads(
'{"source": {"author": "Someone"}, "message": {"original": "Hey!", "Revised": "Hey yo!"}}'
)
row = loader.process_row(test_data)
self.assertIs(type(row), dict)
self.assertIn('author', row.keys())
self.assertIn('message', row.keys())
self.assertEquals(row['author'], 'Someone')
self.assertEquals(row['message'], 'Hey!')
def test_process_row_array_index(self):
"""Ensure that array indices are properly handled as part of the dot notation"""
pass
def test_process_each(self):
outline = {'map': [['id', '_id'], ['count', 'count']], 'collection': 'result'}
loader = Json2Csv(outline)
test_data = json.loads('{"result":[{"_id" : "Someone","count" : 1}]}')
loader.process_each(test_data)
self.assertEquals(len(loader.rows), 1)
row = loader.rows[0]
self.assertIs(type(row), dict)
self.assertIn('id', row.keys())
self.assertIn('count', row.keys())
self.assertEquals(row['id'], 'Someone')
self.assertEquals(row['count'], 1)
def test_process_each_optional_key(self):
"""Ensure a key that is not always present won't prevent data extraction
Where the data is missing, None is returned
"""
outline = {'map': [['id', '_id'], ['count', 'count'], ['tags_0', 'tags.0']]}
loader = Json2Csv(outline)
test_data = json.loads('''[
{"_id": "Someone","count": 1, "tags": ["super"]},
{"_id": "Another", "tags": []}]''')
self.assertEquals(len(test_data), 2)
loader.process_each(test_data)
self.assertEquals(len(loader.rows), 2)
second_row = loader.rows[1]
self.assertEquals(second_row['id'], 'Another')
# works for missing dict keys
self.assertIsNone(second_row['count'])
# and missing list indices
self.assertIsNone(second_row['tags_0'])
def test_load_json(self):
outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
loader = Json2Csv(outline)
with open('fixtures/data.json') as f:
loader.load(f)
first_row = loader.rows[0]
self.assertEqual(first_row['author'], 'Someone')
second_row = loader.rows[1]
self.assertEqual(second_row['author'], 'Another')
third_row = loader.rows[2]
self.assertEqual(third_row['author'], 'Me too')
def test_load_bare_json(self):
outline = {"map": [['author', 'source.author'], ['message', 'message.original']]}
loader = Json2Csv(outline)
with open('fixtures/bare_data.json') as f:
loader.load(f)
first_row = loader.rows[0]
self.assertEqual(first_row['author'], 'Someone')
second_row = loader.rows[1]
self.assertEqual(second_row['author'], 'Another')
third_row = loader.rows[2]
self.assertEqual(third_row['author'], 'Me too')
def test_write_csv(self):
pass
class TestMultiLineJson2Csv(unittest.TestCase):
def test_line_delimited(self):
outline = {"map": [['author', 'source.author'], ['message', 'message.original']]}
loader = MultiLineJson2Csv(outline)
with open('fixtures/line_delimited.json') as f:
loader.load(f)
first_row = loader.rows[0]
self.assertEqual(first_row['author'], 'Someone')
second_row = loader.rows[1]
self.assertEqual(second_row['author'], 'Another')
third_row = loader.rows[2]
self.assertEqual(third_row['author'], 'Me too')
class TestGenOutline(unittest.TestCase):
def test_basic(self):
with open('fixtures/data.json') as json_file:
outline = make_outline(json_file, False, 'nodes')
expected = {
'collection': 'nodes',
'map': [
('message_Revised', 'message.Revised'),
('message_original', 'message.original'),
('source_author', 'source.author'),
]
}
self.assertEqual(outline, expected)
def test_deeply_nested(self):
with open('fixtures/deeply_nested.json') as json_file:
outline = make_outline(json_file, False, 'nodes')
expected = {
'collection': 'nodes',
'map': [
('one_0_two_0_three_0', 'one.0.two.0.three.0'),
('one_0_two_0_three_1', 'one.0.two.0.three.1'),
('one_0_two_0_three_2', 'one.0.two.0.three.2'),
('one_0_two_1_three_0', 'one.0.two.1.three.0'),
('one_0_two_1_three_1', 'one.0.two.1.three.1'),
('one_0_two_1_three_2', 'one.0.two.1.three.2'),
]
}
self.assertEqual(outline, expected)
def test_different_keys_per_row(self):
"Outline should contain the union of the keys."
with open('fixtures/different_keys_per_row.json') as json_file:
outline = make_outline(json_file, False, 'nodes')
expected = {
'collection': 'nodes',
'map': [
('tags_0', 'tags.0'),
('tags_1', 'tags.1'),
('tags_2', 'tags.2'),
('that', 'that'),
('theother', 'theother'),
('this', 'this'),
]
}
self.assertEqual(outline, expected)
def test_line_delimited(self):
with open('fixtures/line_delimited.json') as json_file:
outline = make_outline(json_file, True, None)
expected = {
'map': [
('message_Revised', 'message.Revised'),
('message_original', 'message.original'),
('source_author', 'source.author'),
]
}
self.assertEqual(outline, expected)