Exemplo n.º 1
0
def test_argument_and_explicit_converters_explicit():
    reg = ConverterRegistry()

    assert reg.argument_and_explicit_converters({'a': None},
                                                {'a': Converter(int)}) == {
                                                    'a': Converter(int)
                                                }
Exemplo n.º 2
0
def test_argument_and_explicit_converters_from_type():

    reg = ConverterRegistry()
    reg.register_converter(int, Converter(int))

    assert reg.argument_and_explicit_converters({'a': None}, {'a': int}) == {
        'a': Converter(int)}
Exemplo n.º 3
0
def test_argument_and_explicit_converters_from_type():

    reg = ConverterRegistry()
    reg.register_converter(int, Converter(int))

    assert reg.argument_and_explicit_converters({'a': None}, {'a': int}) == {
        'a': Converter(int)
    }
Exemplo n.º 4
0
def test_converter_registry():
    r = ConverterRegistry()

    c = Converter(int, type(u""))
    r.register_converter(int, c)
    assert r.converter_for_type(int) is c
    assert r.converter_for_value(1) is c
    assert r.converter_for_value(None) is IDENTITY_CONVERTER
    with pytest.raises(DirectiveError):
        r.converter_for_value('s')
Exemplo n.º 5
0
def test_converter_registry():
    r = ConverterRegistry()

    c = Converter(int, unicode)
    r.register_converter(int, c)
    assert r.converter_for_type(int) is c
    assert r.converter_for_value(1) is c
    assert r.converter_for_value(None) is IDENTITY_CONVERTER
    with pytest.raises(DirectiveError):
        r.converter_for_value('s')
Exemplo n.º 6
0
def test_converter_registry():
    r = ConverterRegistry()

    c = Converter(int, unicode)
    r.register_converter(int, c)
    assert r.converter_for_type(int) is c
    assert r.converter_for_value(1) is c
    assert r.converter_for_value(None) is IDENTITY_CONVERTER
    assert r.converter_for_value('s') is None
Exemplo n.º 7
0
def test_argument_and_explicit_converters_none_defaults():
    reg = ConverterRegistry()

    assert reg.argument_and_explicit_converters({'a': None}, {}) == {
        'a': IDENTITY_CONVERTER
    }
Exemplo n.º 8
0
def test_argument_and_explicit_converters_explicit():
    reg = ConverterRegistry()

    assert reg.argument_and_explicit_converters(
        {'a': None}, {'a': Converter(int)}) == {'a': Converter(int)}
Exemplo n.º 9
0
def test_argument_and_explicit_converters_none_defaults():
    reg = ConverterRegistry()

    assert reg.argument_and_explicit_converters({'a': None}, {}) == {
        'a': IDENTITY_CONVERTER}
Exemplo n.º 10
0
def test_converter_registry_inheritance():
    r = ConverterRegistry()

    class Lifeform(object):
        def __init__(self, name):
            self.name = name

    class Animal(Lifeform):
        pass

    seaweed = Lifeform('seaweed')
    elephant = Animal('elephant')

    lifeforms = {
        'seaweed': seaweed,
        'elephant': elephant,
        }

    def lifeform_decode(s):
        try:
            return lifeforms[s]
        except KeyError:
            raise ValueError

    def lifeform_encode(l):
        return l.name

    c = Converter(lifeform_decode, lifeform_encode)
    r.register_converter(Lifeform, c)
    assert r.converter_for_type(Lifeform) is c
    assert r.converter_for_type(Animal) is c
    assert r.converter_for_value(Lifeform('seaweed')) is c
    assert r.converter_for_value(Animal('elephant')) is c
    assert r.converter_for_value(None) is IDENTITY_CONVERTER
    assert r.converter_for_value('s') is None
    assert r.converter_for_type(Lifeform).decode('elephant') is elephant
    assert r.converter_for_type(Lifeform).encode(seaweed) == 'seaweed'
Exemplo n.º 11
0
def test_converter_registry_inheritance():
    r = ConverterRegistry()

    class Lifeform(object):
        def __init__(self, name):
            self.name = name

    class Animal(Lifeform):
        pass

    seaweed = Lifeform('seaweed')
    elephant = Animal('elephant')

    lifeforms = {
        'seaweed': seaweed,
        'elephant': elephant,
    }

    def lifeform_decode(s):
        try:
            return lifeforms[s]
        except KeyError:
            raise ValueError

    def lifeform_encode(l):
        return l.name

    c = Converter(lifeform_decode, lifeform_encode)
    r.register_converter(Lifeform, c)
    assert r.converter_for_type(Lifeform) is c
    assert r.converter_for_type(Animal) is c
    assert r.converter_for_value(Lifeform('seaweed')) is c
    assert r.converter_for_value(Animal('elephant')) is c
    assert r.converter_for_value(None) is IDENTITY_CONVERTER
    with pytest.raises(DirectiveError):
        assert r.converter_for_value('s') is None
    assert r.converter_for_type(Lifeform).decode(['elephant']) is elephant
    assert r.converter_for_type(Lifeform).encode(seaweed) == ['seaweed']