| 1 |
# -*- coding: utf-8 -*- |
| 2 |
from __future__ import absolute_import, print_function, division |
| 3 |
|
| 4 |
import math |
| 5 |
|
| 6 |
from datetime import datetime, date |
| 7 |
from decimal import Decimal |
| 8 |
from tempfile import NamedTemporaryFile |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from petl.compat import PY3 |
| 13 |
from petl.transform.basics import cat |
| 14 |
from petl.util.base import dicts |
| 15 |
from petl.util.vis import look |
| 16 |
|
| 17 |
from petl.test.helpers import ieq |
| 18 |
|
| 19 |
from petl.io.avro import fromavro, toavro, appendavro |
| 20 |
|
| 21 |
from petl.test.io.test_avro_schemas import schema0, schema1, schema2, \ |
| 22 |
schema3, schema4, schema5, schema6 |
| 23 |
|
| 24 |
if PY3: |
| 25 |
from datetime import timezone |
| 26 |
|
| 27 |
try: |
| 28 |
import fastavro |
| 29 |
# import fastavro dependencies |
| 30 |
import pytz |
| 31 |
except ImportError as e: |
| 32 |
pytest.skip('SKIP avro tests: %s' % e, allow_module_level=True) |
| 33 |
else: |
| 34 |
# region Test Cases |
| 35 |
|
| 36 |
def test_fromavro11(): |
| 37 |
_read_from_mavro_file(table1, schema1) |
| 38 |
|
| 39 |
def test_fromavro22(): |
| 40 |
_read_from_mavro_file(table2, schema2) |
| 41 |
|
| 42 |
def test_fromavro33(): |
| 43 |
_read_from_mavro_file(table3, schema3) |
| 44 |
|
| 45 |
def test_toavro11(): |
| 46 |
_write_to_avro_file(table1, schema1) |
| 47 |
|
| 48 |
def test_toavro22(): |
| 49 |
_write_to_avro_file(table2, schema2) |
| 50 |
|
| 51 |
def test_toavro33(): |
| 52 |
_write_to_avro_file(table3, schema3) |
| 53 |
|
| 54 |
def test_toavro10(): |
| 55 |
_write_to_avro_file(table1, None) |
| 56 |
|
| 57 |
def test_toavro13(): |
| 58 |
_write_to_avro_file(table01, schema0, table10) |
| 59 |
|
| 60 |
def test_toavro20(): |
| 61 |
_write_to_avro_file(table2, None) |
| 62 |
|
| 63 |
def test_toavro30(): |
| 64 |
_write_to_avro_file(table3, None) |
| 65 |
|
| 66 |
def test_toavro44(): |
| 67 |
_write_to_avro_file(table4, schema4) |
| 68 |
|
| 69 |
def test_toavro55(): |
| 70 |
_write_to_avro_file(table5, schema5) |
| 71 |
|
| 72 |
def test_toavro50(): |
| 73 |
_write_to_avro_file(table5, None) |
| 74 |
|
| 75 |
def test_toavro70(): |
| 76 |
_write_to_avro_file(table71, None) |
| 77 |
|
| 78 |
def test_toavro80(): |
| 79 |
_write_to_avro_file(table8, None) |
| 80 |
|
| 81 |
def test_toavro90(): |
| 82 |
_write_to_avro_file(table9, None) |
| 83 |
|
| 84 |
def test_toavro61(): |
| 85 |
_write_to_avro_file(table61, schema6, print_tables=False) |
| 86 |
|
| 87 |
def test_toavro62(): |
| 88 |
_write_to_avro_file(table62, schema6, print_tables=False) |
| 89 |
|
| 90 |
def test_toavro63(): |
| 91 |
_write_to_avro_file(table63, schema6, print_tables=False) |
| 92 |
|
| 93 |
def test_toavro60(): |
| 94 |
_write_to_avro_file(table60, schema6, print_tables=False) |
| 95 |
|
| 96 |
def test_appendavro11(): |
| 97 |
_append_to_avro_file(table11, table12, schema1, table1) |
| 98 |
|
| 99 |
def test_appendavro22(): |
| 100 |
_append_to_avro_file(table21, table22, schema2, table2) |
| 101 |
|
| 102 |
def test_appendavro10(): |
| 103 |
_append_to_avro_file(table11, table12, schema1) |
| 104 |
|
| 105 |
def test_toavro_troubleshooting10(): |
| 106 |
nullable_schema = dict(schema0) |
| 107 |
schema_fields = nullable_schema['fields'] |
| 108 |
for field in schema_fields: |
| 109 |
field['type'] = ['null', 'string'] |
| 110 |
try: |
| 111 |
_write_temp_avro_file(table1, nullable_schema) |
| 112 |
except ValueError as vex: |
| 113 |
bob = "%s" % vex |
| 114 |
assert 'Bob' in bob |
| 115 |
return |
| 116 |
assert False, 'Failed schema conversion' |
| 117 |
|
| 118 |
def test_toavro_troubleshooting11(): |
| 119 |
table0 = list(table1) |
| 120 |
table0[3][1] = None |
| 121 |
try: |
| 122 |
_write_temp_avro_file(table0, schema1) |
| 123 |
except TypeError as tex: |
| 124 |
joe = "%s" % tex |
| 125 |
assert 'Joe' in joe |
| 126 |
return |
| 127 |
assert False, 'Failed schema conversion' |
| 128 |
|
| 129 |
# endregion |
| 130 |
|
| 131 |
# region Execution |
| 132 |
|
| 133 |
def _read_from_mavro_file(test_rows, test_schema, test_expect=None, print_tables=True): |
| 134 |
_show__expect_rows(test_rows, print_tables) |
| 135 |
test_filename = _create_avro_example(test_schema, test_rows) |
| 136 |
test_actual = fromavro(test_filename) |
| 137 |
test_expect2 = test_rows if test_expect is None else test_expect |
| 138 |
_assert_rows_are_equals(test_expect2, test_actual, print_tables) |
| 139 |
return test_filename |
| 140 |
|
| 141 |
def _write_temp_avro_file(test_rows, test_schema): |
| 142 |
test_filename = _get_tempfile_path() |
| 143 |
print("Writing avro file:", test_filename) |
| 144 |
toavro(test_rows, test_filename, schema=test_schema) |
| 145 |
return test_filename |
| 146 |
|
| 147 |
def _write_to_avro_file(test_rows, test_schema, test_expect=None, print_tables=True): |
| 148 |
_show__expect_rows(test_rows, print_tables) |
| 149 |
test_filename = _write_temp_avro_file(test_rows, test_schema) |
| 150 |
test_actual = fromavro(test_filename) |
| 151 |
test_expect2 = test_rows if test_expect is None else test_expect |
| 152 |
_assert_rows_are_equals(test_expect2, test_actual, print_tables) |
| 153 |
|
| 154 |
def _append_to_avro_file(test_rows1, test_rows2, test_schema, test_expect=None, print_tables=True): |
| 155 |
_show__expect_rows(test_rows1, print_tables) |
| 156 |
_show__expect_rows(test_rows2, print_tables) |
| 157 |
test_filename = _get_tempfile_path() |
| 158 |
toavro(test_rows1, test_filename, schema=test_schema) |
| 159 |
appendavro(test_rows2, test_filename, schema=test_schema) |
| 160 |
|
| 161 |
test_actual = fromavro(test_filename) |
| 162 |
if test_expect is not None: |
| 163 |
test_expect2 = test_expect |
| 164 |
else: |
| 165 |
test_expect2 = cat(test_rows1, test_rows2) |
| 166 |
_assert_rows_are_equals(test_expect2, test_actual, print_tables) |
| 167 |
|
| 168 |
# endregion |
| 169 |
|
| 170 |
# region Helpers |
| 171 |
|
| 172 |
def _assert_rows_are_equals(test_expect, test_actual, print_tables=True): |
| 173 |
if print_tables: |
| 174 |
_show__rows_from('Actual:', test_actual) |
| 175 |
avro_schema = test_actual.get_avro_schema() |
| 176 |
print('\nSchema:\n', avro_schema) |
| 177 |
ieq(test_expect, test_actual) |
| 178 |
ieq(test_expect, test_actual) # verify can iterate twice |
| 179 |
|
| 180 |
def _show__expect_rows(test_rows, print_tables=True, limit=0): |
| 181 |
if print_tables: |
| 182 |
_show__rows_from('\nExpected:', test_rows, limit) |
| 183 |
|
| 184 |
def _show__rows_from(label, test_rows, limit=0): |
| 185 |
print(label) |
| 186 |
print(look(test_rows, limit=limit)) |
| 187 |
|
| 188 |
def _decs(float_value, rounding=12): |
| 189 |
return Decimal(str(round(float_value, rounding))) |
| 190 |
|
| 191 |
def _utc(year, month, day, hour=0, minute=0, second=0, microsecond=0): |
| 192 |
u = datetime(year, month, day, hour, minute, second, microsecond) |
| 193 |
if PY3: |
| 194 |
return u.replace(tzinfo=timezone.utc) |
| 195 |
return u.replace(tzinfo=pytz.utc) |
| 196 |
|
| 197 |
def _get_tempfile_path(delete_on_close=False): |
| 198 |
f = NamedTemporaryFile(delete=delete_on_close, mode='r') |
| 199 |
test_filename = f.name |
| 200 |
f.close() |
| 201 |
return test_filename |
| 202 |
|
| 203 |
def _create_avro_example(test_schema, test_table): |
| 204 |
parsed_schema = fastavro.parse_schema(test_schema) |
| 205 |
rows = dicts(test_table) |
| 206 |
with NamedTemporaryFile(delete=False, mode='wb') as fo: |
| 207 |
fastavro.writer(fo, parsed_schema, rows) |
| 208 |
return fo.name |
| 209 |
|
| 210 |
# endregion |
| 211 |
|
| 212 |
# region Mockup data |
| 213 |
|
| 214 |
header1 = [u'name', u'friends', u'age'] |
| 215 |
|
| 216 |
rows1 = [[u'Bob', 42, 33], |
| 217 |
[u'Jim', 13, 69], |
| 218 |
[u'Joe', 86, 17], |
| 219 |
[u'Ted', 23, 51]] |
| 220 |
|
| 221 |
table1 = [header1] + rows1 |
| 222 |
|
| 223 |
table11 = [header1] + rows1[0:2] |
| 224 |
table12 = [header1] + rows1[2:] |
| 225 |
|
| 226 |
table01 = [header1[0:2]] + [item[0:2] for item in rows1] |
| 227 |
table10 = [header1] + [item[0:2] + [None] for item in rows1] |
| 228 |
|
| 229 |
table2 = [[u'name', u'age', u'birthday', u'death', u'insurance', u'deny'], |
| 230 |
[u'pete', 17, date(2012, 10, 11), |
| 231 |
_utc(2018, 10, 14, 15, 16, 17, 18000), Decimal('1.100'), False], |
| 232 |
[u'mike', 27, date(2002, 11, 12), |
| 233 |
_utc(2015, 12, 13, 14, 15, 16, 17000), Decimal('1.010'), False], |
| 234 |
[u'zack', 37, date(1992, 12, 13), |
| 235 |
_utc(2010, 11, 12, 13, 14, 15, 16000), Decimal('123.456'), True], |
| 236 |
[u'gene', 47, date(1982, 12, 25), |
| 237 |
_utc(2009, 10, 11, 12, 13, 14, 15000), Decimal('-1.010'), False]] |
| 238 |
|
| 239 |
table21 = table2[0:3] |
| 240 |
table22 = [table2[0]] + table2[3:] |
| 241 |
|
| 242 |
table3 = [[u'name', u'age', u'birthday', u'death'], |
| 243 |
[u'pete', 17, date(2012, 10, 11), |
| 244 |
_utc(2018, 10, 14, 15, 16, 17, 18000)], |
| 245 |
[u'mike', 27, date(2002, 11, 12), |
| 246 |
_utc(2015, 12, 13, 14, 15, 16, 17000)], |
| 247 |
[u'zack', 37, date(1992, 12, 13), |
| 248 |
_utc(2010, 11, 12, 13, 14, 15, 16000)], |
| 249 |
[u'gene', 47, date(1982, 12, 25), |
| 250 |
_utc(2009, 10, 11, 12, 13, 14, 15000)]] |
| 251 |
|
| 252 |
table4 = [[u'name', u'friends', u'age', u'birthday'], |
| 253 |
[u'Bob', 42, 33, date(2012, 10, 11)], |
| 254 |
[u'Jim', 13, 69, None], |
| 255 |
[None, 86, 17, date(1992, 12, 13)], |
| 256 |
[u'Ted', 23, None, date(1982, 12, 25)]] |
| 257 |
|
| 258 |
table5 = [[u'palette', u'colors'], |
| 259 |
[u'red', [u'red', u'salmon', u'crimson', u'firebrick', u'coral']], |
| 260 |
[u'pink', [u'pink', u'rose']], |
| 261 |
[u'purple', [u'purple', u'violet', u'fuchsia', |
| 262 |
u'magenta', u'indigo', u'orchid', u'lavender']], |
| 263 |
[u'green', [u'green', u'lime', u'seagreen', |
| 264 |
u'grass', u'olive', u'forest', u'teal']], |
| 265 |
[u'blue', [u'blue', u'cyan', u'aqua', u'aquamarine', |
| 266 |
u'turquoise', u'royal', u'sky', u'navy']], |
| 267 |
[u'gold', [u'gold', u'yellow', u'khaki', |
| 268 |
u'mocassin', u'papayawhip', u'lemonchiffon']], |
| 269 |
[u'black', None]] |
| 270 |
|
| 271 |
header6 = [u'array_string', u'array_record', u'nulable_date', |
| 272 |
u'multi_union_time', u'array_bytes_decimal', u'array_fixed_decimal'] |
| 273 |
|
| 274 |
rows61 = [[u'a', u'b', u'c'], |
| 275 |
[{u'f1': u'1', u'f2': Decimal('654.321')}], |
| 276 |
date(2020, 1, 10), |
| 277 |
_utc(2020, 12, 19, 18, 17, 16, 15000), |
| 278 |
[Decimal('123.456')], |
| 279 |
[Decimal('987.654')], ] |
| 280 |
|
| 281 |
rows62 = [[u'a', u'b', u'c'], |
| 282 |
[{u'f1': u'1', u'f2': Decimal('654.321')}], |
| 283 |
date(2020, 1, 10), |
| 284 |
_utc(2020, 12, 19, 18, 17, 16, 15000), |
| 285 |
[Decimal('123.456'), Decimal('456.789')], |
| 286 |
[Decimal('987.654'), Decimal('321.123'), Decimal('456.654')]] |
| 287 |
|
| 288 |
table61 = [header6, rows61] |
| 289 |
|
| 290 |
table62 = [header6, rows62] |
| 291 |
|
| 292 |
table63 = [header6, rows61, rows62] |
| 293 |
|
| 294 |
table60 = [header6, [rows61[0], rows61[1], ]] |
| 295 |
|
| 296 |
header7 = [u'col', u'sqrt_pow_ij'] |
| 297 |
|
| 298 |
rows70 = [[j, [round(math.sqrt(math.pow(i*j, i+j)), 9) |
| 299 |
for i in range(1, j+1)]] for j in range(1, 7)] |
| 300 |
|
| 301 |
rows71 = [[j, [Decimal(str(round(math.sqrt(math.pow(i*j, i+j)), 9))) |
| 302 |
for i in range(1, j+1)]] for j in range(1, 7)] |
| 303 |
|
| 304 |
table70 = [header7] + rows70 |
| 305 |
table71 = [header7] + rows71 |
| 306 |
|
| 307 |
header8 = [u'number', u'properties'] |
| 308 |
|
| 309 |
rows8 = [[_decs(x), { |
| 310 |
u'atan': _decs(math.atan(x)), |
| 311 |
u'sin': math.sin(x), |
| 312 |
u'cos': math.cos(x), |
| 313 |
u'tan': math.tan(x), |
| 314 |
u'square': x*x, |
| 315 |
u'sqrt': math.sqrt(x), |
| 316 |
u'log': math.log(x), |
| 317 |
u'log10': math.log10(x), |
| 318 |
u'exp': math.log10(x), |
| 319 |
u'power_x': x**x, |
| 320 |
u'power_minus_x': x**-x, |
| 321 |
}] for x in range(1, 12)] |
| 322 |
|
| 323 |
table8 = [header8] + rows8 |
| 324 |
|
| 325 |
rows9 = [[1, { u'name': u'Bob', u'age': 20 }], |
| 326 |
[2, { u'name': u'Ted', u'budget': _decs(54321.25) }], |
| 327 |
[2, { u'name': u'Jim', u'color': u'blue' }], |
| 328 |
[2, { u'name': u'Joe', u'alias': u'terminator' }]] |
| 329 |
|
| 330 |
table9 = [header8] + rows9 |
| 331 |
|
| 332 |
# endregion |
| 333 |
|
| 334 |
# region testing |
| 335 |
|
| 336 |
# endregion |
| 337 |
|
| 338 |
# end of tests # |