def test_equality(self): func = utils.compose(int, str) other = utils.compose(int, str) assert func == other assert not func != other assert hash(func) == hash(other) assert not func == utils.compose(int) assert func != utils.compose(int) assert func != object() assert not func == object()
def test_ok(self): Tag = t.NamedTuple('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>')]]
def test_ok(self): Tag = t.NamedTuple('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>')
def test_unsupported_type(self): Tag = t.NamedTuple('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)
def test_multiple_funcs(self): func = utils.compose(str, lambda x: x + 1, int) assert isinstance(func.funcs, tuple) assert func('30', base=5) == '16'
def test_one_func_with_multiple_args(self): func = utils.compose(int) assert func('10', base=5) == 5 assert isinstance(func.funcs, tuple) assert func.funcs == (int, )
def test_empty(self): obj = object() func = utils.compose() assert func(obj) is obj assert isinstance(func.funcs, tuple) assert func.funcs == ()