Пример #1
0
def test_from_jsondata_unsupported_class():
    class UnsupportedClass(object):
        pass

    with raises(ValueError) as excinfo:
        from_jsondata(UnsupportedClass, {'some': 'test'})
    assert str(excinfo.value).startswith('Unable to generate instance of ')
Пример #2
0
def test_from_jsondata_additional_fields():
    NamedTupleA = NamedTuple('NamedTupleA', [
        ('a', str),
        ('b', datetime),
    ])
    with raises(ValueError) as excinfo:
        from_jsondata(NamedTupleA, {'a': 'test', 'b': None, 'c': None})
    assert str(excinfo.value).startswith('Unable to generate instance of ')
Пример #3
0
def test_from_jsondata_ambiguous_union_type():
    NamedTupleA = NamedTuple('NamedTupleA', [
        ('a', int),
    ])
    OtherNamedTupleA = NamedTuple('NamedTupleA', [
        ('b', int),
    ])
    NamedTupleC = NamedTuple('NamedTupleC', [
        ('c', Union[NamedTupleA, OtherNamedTupleA]),
    ])
    with raises(ValueError) as excinfo:
        from_jsondata(NamedTupleC, {'c': {'type': 'NamedTupleA'}})
    assert str(excinfo.value).startswith('Multiple matching types for ')
Пример #4
0
def test_from_jsondata_missing_union_type():
    NamedTupleA = NamedTuple('NamedTupleA', [
        ('a', int),
    ])
    NamedTupleB = NamedTuple('NamedTupleB', [
        ('b', int),
    ])
    NamedTupleC = NamedTuple('NamedTupleC', [
        ('c', Union[NamedTupleA, NamedTupleB]),
    ])
    with raises(ValueError) as excinfo:
        from_jsondata(NamedTupleC, {'c': {'type': 'NonExistingType'}})
    assert str(excinfo.value).startswith('Unable to find type ')
Пример #5
0
def test_jsondata_empty():
    NamedTupleEmpty = NamedTuple('NamedTupleEmpty', [])

    class NamedTupleEmptySubClass(NamedTupleEmpty):
        def submethod(self):
            return True

    value = NamedTupleEmptySubClass()
    jsondata = {}
    assert to_jsondata(value) == jsondata
    generated_value = from_jsondata(NamedTupleEmptySubClass, jsondata)
    assert isinstance(generated_value, NamedTupleEmptySubClass)
    assert generated_value == value
Пример #6
0
def test_nonobject_from_jsondata_int():
    assert from_jsondata(int, 2) == 2
Пример #7
0
def test_nonobject_from_jsondata_list():
    assert from_jsondata(List[int], [1, 2, 3]) == [1, 2, 3]
Пример #8
0
def test_jsondata_roundtrip():
    NamedTupleEmpty = NamedTuple('NamedTupleEmpty', [])
    NamedTupleA = NamedTuple('NamedTupleA', [
        ('a', str),
    ])
    NamedTupleB = NamedTuple('NamedTupleB', [
        ('a', int),
        ('b', bool),
    ])
    NamedTupleC = NamedTuple('NamedTupleC', [
        ('a', int),
        ('b', bool),
        ('c', int),
        ('d', str),
        ('e', float),
        ('f', datetime),
        ('g', datetime),
        ('h', NamedTupleA),
        ('i', Union[NamedTupleA, NamedTupleB]),
        ('j', List[Union[NamedTupleA, NamedTupleB]]),
        ('k', NamedTupleEmpty),
        ('l', Union[NamedTupleEmpty, NamedTupleA, NamedTupleB]),
        ('m', timedelta),
        ('n', date),
        ('o', Dict[str, date]),
        ('p', datetime),
    ])

    class NamedTupleCSubClass(NamedTupleC):
        def submethod(self):
            return True

    value = NamedTupleCSubClass(
        a=1,
        b=True,
        c=123456789012345678901234567890,
        d='test\u00a7',
        e=1.4,
        f=datetime(2016, 7, 1, 18, 0, 28, 123456),
        g=None,
        h=NamedTupleA(a='sub', ),
        i=NamedTupleA(a='sub in union', ),
        j=[
            NamedTupleA(a='sub in list of union', ),
            NamedTupleB(
                a=2,
                b=False,
            ),
        ],
        k=NamedTupleEmpty(),
        l=NamedTupleEmpty(),
        m=timedelta(minutes=10, microseconds=3),
        n=date(2017, 1, 2),
        o={
            '2': date(2016, 1, 2),
            '4': date(2016, 3, 4),
        },
        p=datetime(2017, 3, 6, 15, 1, 31, 0),
    )
    jsondata = {
        'a':
        1,
        'b':
        True,
        'c':
        123456789012345678901234567890,
        'd':
        'test\u00a7',
        'e':
        1.4,
        'f':
        '2016-07-01T18:00:28.123456Z',
        'g':
        None,
        'h': {
            'a': 'sub',
        },
        'i': {
            'type': 'NamedTupleA',
            'a': 'sub in union',
        },
        'j': [
            {
                'type': 'NamedTupleA',
                'a': 'sub in list of union',
            },
            {
                'type': 'NamedTupleB',
                'a': 2,
                'b': False,
            },
        ],
        'k': {},
        'l': {
            'type': 'NamedTupleEmpty',
        },
        'm':
        600.000003,
        'n':
        '2017-01-02',
        'o': {
            '2': '2016-01-02',
            '4': '2016-03-04',
        },
        'p':
        '2017-03-06T15:01:31Z',
    }
    assert to_jsondata(value) == jsondata
    generated_value = from_jsondata(NamedTupleCSubClass, jsondata)
    assert isinstance(generated_value, NamedTupleCSubClass)
    assert generated_value == value
Пример #9
0
def test_from_jsondata_unsupported_dict_key_type():
    with raises(ValueError) as excinfo:
        from_jsondata(Dict[int, str], {'some': 'test'})
    assert str(excinfo.value) == 'Invalid key type for JSON object: int'
Пример #10
0
def test_from_jsondata_unsupported_nontype():
    UnsupportedNonType = 42
    with raises(ValueError) as excinfo:
        from_jsondata(UnsupportedNonType, {'some': 'test'})
    assert str(excinfo.value).startswith('Unable to generate instance of ')
Пример #11
0
def test_from_jsondata_unsupported_type_list():
    with raises(ValueError) as excinfo:
        from_jsondata(list, {})
    assert str(excinfo.value).startswith('Unable to generate instance of ')