예제 #1
0
def test_decode_optional():
    t = Transcoder()

    r = t.decode('text', typing.Optional[str])
    assert r == 'text'

    r = t.decode(None, typing.Optional[str])
    assert r == None
예제 #2
0
def test_add_new_codec():
    NewType = type('NewType', (), {})

    class NewTypeCodec(ICodec):
        def can_handle(self, field_type: typing.Type) -> bool:
            return field_type is NewType

        def encode(self, obj: typing.Any,
                   field_type: typing.Type) -> typing.Any:
            return True

        def decode(self, data: dict, field_type: typing.Type) -> typing.Any:
            return True

    t = Transcoder()
    t.add_codec(NewTypeCodec())

    assert t.encode(NewType(), NewType)
    assert t.decode({}, NewType)
예제 #3
0
def test_encode_primitive():
    t = Transcoder()

    m = t.decode('x', str)
    assert m == 'x'

    m = t.decode(1, int)
    assert m == 1

    m = t.decode(1.0, int)
    assert m == 1

    m = t.decode(1.1, float)
    assert m == 1.1

    m = t.decode(1, float)
    assert m == 1.0

    m = t.decode(True, bool)
    assert m == True
예제 #4
0
def test_decode_single_type_sequence():
    t = Transcoder()
    m = t.decode(tuple(['x']), tuple[str, ...])
    assert m == ('x', )
예제 #5
0
def test_decode_dict():
    t = Transcoder()

    r = t.decode({'0': 'True'}, typing.Dict[int, bool])
    assert r == {0: True}
예제 #6
0
def test_decode_none():
    t = Transcoder()
    assert t.decode(None, type(None)) is None