/
test_script.py
269 lines (219 loc) · 7.75 KB
/
test_script.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Modifications copyright (C) 2017 Pluralsight LLC
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import csv
# import six
from six import BytesIO, StringIO
import re
try:
import json
except ImportError:
import simplejson as json
from tempfile import NamedTemporaryFile
import spavro.schema
from spavro.io import DatumWriter
from spavro.datafile import DataFileWriter
from os.path import dirname, join, isfile
from os import remove
from operator import itemgetter
SPACE_CHARS = re.compile(r'\s+')
NUM_RECORDS = 7
try:
from subprocess import check_output
except ImportError:
from subprocess import Popen, PIPE
def check_output(args):
pipe = Popen(args, stdout=PIPE)
if pipe.wait() != 0:
raise ValueError
return pipe.stdout.read()
try:
from subprocess import check_call
except ImportError:
def check_call(args, **kw):
pipe = Popen(args, **kw)
assert pipe.wait() == 0
SCHEMA = '''
{
"namespace": "test.avro",
"name": "LooneyTunes",
"type": "record",
"fields": [
{"name": "first", "type": "string"},
{"name": "last", "type": "string"},
{"name": "type", "type": "string"}
]
}
'''
LOONIES = (
(u"daffy", u"duck", u"duck"),
(u"bugs", u"bunny", u"bunny"),
(u"tweety", u"", u"bird"),
(u"road", u"runner", u"bird"),
(u"wile", u"e", u"coyote"),
(u"pepe", u"le pew", u"skunk"),
(u"foghorn", u"leghorn", u"rooster"),
)
def looney_records():
for f, l, t in LOONIES:
yield {"first": f, "last": l, "type": t}
SCRIPT = join(dirname(__file__), "..", "scripts", "avro")
_JSON_PRETTY = '''{
"first": "daffy",
"last": "duck",
"type": "duck"
}'''
def gen_avro(filename):
schema = spavro.schema.parse(SCHEMA)
fo = open(filename, "wb")
writer = DataFileWriter(fo, DatumWriter(), schema)
for record in looney_records():
writer.append(record)
writer.close()
fo.close()
def tempfile():
return NamedTemporaryFile(delete=False).name
class TestCat(unittest.TestCase):
def setUp(self):
self.avro_file = tempfile()
gen_avro(self.avro_file)
def tearDown(self):
if isfile(self.avro_file):
remove(self.avro_file)
def _run(self, *args, **kw):
out = check_output([SCRIPT, "cat", self.avro_file] + list(args)).decode('utf-8')
# subprocess returns bytes, decode into utf8 strings
# Note: should this be ASCII instead?
if kw.get("raw"):
return out
else:
return out.splitlines()
def test_print(self):
return len(self._run()) == NUM_RECORDS
def test_filter(self):
return len(self._run("--filter", "r['type']=='bird'")) == 2
def test_skip(self):
skip = 3
return len(self._run("--skip", str(skip))) == NUM_RECORDS - skip
def test_csv(self):
raw = self._run("-f", "csv", raw=True)
reader = csv.reader(StringIO(raw))
assert len(list(reader)) == NUM_RECORDS
def test_csv_header(self):
'''Test the CSV header is processed correctly'''
io = StringIO(self._run("-f", "csv", "--header", raw=True))
reader = csv.DictReader(io)
self.assertEqual(reader.fieldnames, ['first', 'last', 'type'])
def test_print_schema(self):
out = self._run("--print-schema", raw=True)
assert json.loads(out)["namespace"] == "test.avro"
def test_help(self):
# Just see we have these
self._run("-h")
self._run("--help")
def test_json_pretty(self):
out = self._run("--format", "json-pretty", "-n", "1", raw=1)
clean_out = SPACE_CHARS.sub('', out)
example = SPACE_CHARS.sub('', _JSON_PRETTY)
self.assertEqual(example, clean_out)
def test_version(self):
check_output([SCRIPT, "cat", "--version"]).decode('utf-8')
def test_files(self):
out = self._run(self.avro_file)
assert len(out) == 2 * NUM_RECORDS
def test_fields(self):
# One field selection (no comma)
out = self._run('--fields', 'last')
assert json.loads(out[0]) == {'last': 'duck'}
# Field selection (with comma and space)
out = self._run('--fields', 'first, last')
assert json.loads(out[0]) == {'first': 'daffy', 'last': 'duck'}
# Empty fields should get all
out = self._run('--fields', '')
assert json.loads(out[0]) == \
{'first': 'daffy', 'last': 'duck', 'type': 'duck'}
# Non existing fields are ignored
out = self._run('--fields', 'first,last,age')
assert json.loads(out[0]) == {'first': 'daffy', 'last': 'duck'}
class TestWrite(unittest.TestCase):
def setUp(self):
self.json_file = tempfile() + ".json"
fo = open(self.json_file, "w")
for record in looney_records():
json.dump(record, fo)
fo.write("\n")
fo.close()
self.csv_file = tempfile() + ".csv"
fo = open(self.csv_file, "w")
write = csv.writer(fo).writerow
get = itemgetter("first", "last", "type")
for record in looney_records():
write(get(record))
fo.close()
self.schema_file = tempfile()
fo = open(self.schema_file, "w")
fo.write(SCHEMA)
fo.close()
def tearDown(self):
for filename in (self.csv_file, self.json_file, self.schema_file):
try:
remove(filename)
except OSError:
continue
def _run(self, *args, **kw):
args = [SCRIPT, "write", "--schema", self.schema_file] + list(args)
check_call(args, **kw)
def load_avro(self, filename):
out = check_output([SCRIPT, "cat", filename]).decode('utf-8')
return map(json.loads, out.splitlines())
def test_version(self):
check_call([SCRIPT, "write", "--version"])
def format_check(self, format, filename):
tmp = tempfile()
fo = open(tmp, "wb")
self._run(filename, "-f", format, stdout=fo)
fo.close()
records = list(self.load_avro(tmp))
assert len(records) == NUM_RECORDS
assert records[0]["first"] == "daffy"
remove(tmp)
def test_write_json(self):
self.format_check("json", self.json_file)
def test_write_csv(self):
self.format_check("csv", self.csv_file)
def test_outfile(self):
tmp = tempfile()
remove(tmp)
self._run(self.json_file, "-o", tmp)
assert len(list(self.load_avro(tmp))) == NUM_RECORDS
remove(tmp)
def test_multi_file(self):
tmp = tempfile()
fo = open(tmp, "wb")
self._run(self.json_file, self.json_file, stdout=fo)
fo.close()
assert len(list(self.load_avro(tmp))) == 2 * NUM_RECORDS
remove(tmp)
def test_stdin(self):
tmp = tempfile()
info = open(self.json_file, "rb")
fo = open(tmp, "wb")
self._run("--input-type", "json", stdin=info, stdout=fo)
fo.close()
assert len(list(self.load_avro(tmp))) == NUM_RECORDS
remove(tmp)