Fix type of expected values (as read from JSON) according to actual ORC datatype.
(actual_cols, expected_cols)
| 47 | |
| 48 | |
| 49 | def fix_example_values(actual_cols, expected_cols): |
| 50 | """ |
| 51 | Fix type of expected values (as read from JSON) according to |
| 52 | actual ORC datatype. |
| 53 | """ |
| 54 | for name in expected_cols: |
| 55 | expected = expected_cols[name] |
| 56 | actual = actual_cols[name] |
| 57 | if (name == "map" and |
| 58 | [d.keys() == {'key', 'value'} for m in expected for d in m]): |
| 59 | # convert [{'key': k, 'value': v}, ...] to [(k, v), ...] |
| 60 | col = expected_cols[name].copy() |
| 61 | for i, m in enumerate(expected): |
| 62 | col[i] = [(d['key'], d['value']) for d in m] |
| 63 | expected_cols[name] = col |
| 64 | continue |
| 65 | |
| 66 | typ = actual[0].__class__ |
| 67 | if issubclass(typ, datetime.datetime): |
| 68 | # timestamp fields are represented as strings in JSON files |
| 69 | expected = pd.to_datetime(expected) |
| 70 | elif issubclass(typ, datetime.date): |
| 71 | # date fields are represented as strings in JSON files |
| 72 | expected = expected.dt.date |
| 73 | elif typ is decimal.Decimal: |
| 74 | converted_decimals = [None] * len(expected) |
| 75 | # decimal fields are represented as reals in JSON files |
| 76 | for i, (d, v) in enumerate(zip(actual, expected)): |
| 77 | if not pd.isnull(v): |
| 78 | exp = d.as_tuple().exponent |
| 79 | factor = 10 ** -exp |
| 80 | converted_decimals[i] = ( |
| 81 | decimal.Decimal(round(v * factor)).scaleb(exp)) |
| 82 | expected = pd.Series(converted_decimals) |
| 83 | |
| 84 | expected_cols[name] = expected |
| 85 | |
| 86 | |
| 87 | def check_example_values(orc_df, expected_df, start=None, stop=None): |
no test coverage detected