|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +****************************************** |
| 5 | +tests.test_interval_deserialization |
| 6 | +****************************************** |
| 7 | +
|
| 8 | +Tests for how SQLAthanor deserializes Interval columns. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | +import datetime |
| 14 | + |
| 15 | +from tests.fixtures import db_engine, tables, base_model, db_session, \ |
| 16 | + model_complex_postgresql, instance_postgresql |
| 17 | + |
| 18 | +from sqlathanor.errors import InvalidFormatError, ValueDeserializationError, \ |
| 19 | + UnsupportedDeserializationError |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.parametrize('attribute, format, input_value, expected_result, error', [ |
| 24 | + ('name', 'csv', 'serialized', 'deserialized', None), |
| 25 | + ('id', 'csv', '1', 1, None), |
| 26 | + ('hybrid', 'csv', 1, 1, None), |
| 27 | + ('smallint_column', 'csv', '2', 2, None), |
| 28 | + ('addresses', 'json', [], [], None), |
| 29 | + ('time_delta', 'csv', 86400.0, datetime.timedelta(1), None), |
| 30 | + ('time_delta', 'csv', '00:35:00', datetime.timedelta(minutes = 35), None), |
| 31 | + ('time_delta', 'csv', '00:35:00.456', datetime.timedelta(minutes = 35, milliseconds = 456), None), |
| 32 | + ('name', 'invalid', None, None, InvalidFormatError), |
| 33 | + ('missing', 'csv', None, None, UnsupportedDeserializationError), |
| 34 | + ('hidden', 'csv', None, None, UnsupportedDeserializationError), |
| 35 | + ('id', 'csv', 'invalid', None, ValueDeserializationError), |
| 36 | + ('time_delta', 'csv', 'not-numeric', None, ValueDeserializationError), |
| 37 | + ('time_delta', 'json', 86400.0, None, UnsupportedDeserializationError), |
| 38 | + ('time_delta', 'json', '00:35:00', None, UnsupportedDeserializationError), |
| 39 | +
|
| 40 | +]) |
| 41 | +def test__get_deserialized_value(request, |
| 42 | + instance_postgresql, |
| 43 | + attribute, |
| 44 | + format, |
| 45 | + input_value, |
| 46 | + expected_result, |
| 47 | + error): |
| 48 | + target = instance_postgresql[0][0] |
| 49 | + |
| 50 | + if not error: |
| 51 | + result = target._get_deserialized_value(input_value, format, attribute) |
| 52 | + assert result == expected_result |
| 53 | + else: |
| 54 | + with pytest.raises(error): |
| 55 | + result = target._get_deserialized_value(input_value, format, attribute) |
0 commit comments