Exemplo n.º 1
0
    def test_ok(self):
        @dataclass(frozen=True)
        class Tag:
            name: str

        registry = load.GenericRegistry({
            t.List: load.list_loader,
        }) | load.PrimitiveRegistry({
            Tag: compose(Tag, '<{}>'.format),
        })

        # simple case
        loader = registry(t.List[Tag])
        assert loader(['hello', 5, 'there'
                       ]) == [Tag('<hello>'),
                              Tag('<5>'),
                              Tag('<there>')]

        # recursive case
        loader = registry(t.List[t.List[Tag]])
        assert loader([['hello', 9, 'there'], [], ['another', 'list']
                       ]) == [[Tag('<hello>'),
                               Tag('<9>'),
                               Tag('<there>')], [],
                              [Tag('<another>'),
                               Tag('<list>')]]
Exemplo n.º 2
0
    def test_ok(self):
        @dataclass(frozen=True)
        class Tag:
            name: str

        registry = load.PrimitiveRegistry({Tag: compose(Tag, '<{}>'.format)
                                           }) | load.get_optional_loader

        loader = registry(t.Optional[Tag])
        assert loader(None) is None
        assert loader(5) == Tag('<5>')
Exemplo n.º 3
0
def registry():
    return load.PrimitiveRegistry({
        int: int,
        float: float,
        bool: {
            'true': True,
            'false': False
        }.__getitem__,
        str: str,
        type(None): identity,
        object: identity,
        datetime: utils.parse_iso8601,
    }) | load.GenericRegistry({
        t.List: load.list_loader,
        t.Set: load.set_loader,
    }) | load.get_optional_loader
Exemplo n.º 4
0
    def test_unsupported_type(self):
        @dataclass(frozen=True)
        class Tag:
            name: str

        registry = load.GenericRegistry({
            t.List: load.list_loader,
        }) | load.PrimitiveRegistry({
            Tag: compose(Tag, '<{}>'.format),
        })
        with pytest.raises(load.UnsupportedType):
            registry(t.Set[Tag])

        with pytest.raises(load.UnsupportedType):
            registry(t.List[str])

        with pytest.raises(load.UnsupportedType):
            registry(object)
Exemplo n.º 5
0
 def test_not_found(self):
     registry = load.PrimitiveRegistry({int: round})
     with pytest.raises(load.UnsupportedType):
         registry(str)
Exemplo n.º 6
0
 def test_found(self):
     registry = load.PrimitiveRegistry({int: round})
     loader = registry(int)
     assert loader(3.4) == 3