def test_wrap_function_structured_to_structured(FooIn, FooOut, Image): '''Tests function wrapper utility both args and return need to be wrapped''' def foo(x: int, y: int) -> int: return x + y f, in_, out = _wrap_function(foo) assert _types_equal(in_, FooIn) assert _types_equal(out, FooOut) assert f(FooIn(1, 2)) == FooOut(3)
def test_wrap_function_structured_to_structured_already_wrapped(FooIn, FooOut, Image): '''Tests function wrapper utility both args and return are already wrapped''' def bar(msg: FooIn) -> FooOut: return FooOut(msg.x + msg.y) f, in_, out = _wrap_function(bar) assert f is bar assert _types_equal(in_, FooIn) assert _types_equal(out, FooOut) assert f(FooIn(1, 2)) == FooOut(3)
def test_wrap_function_structured_to_structured_return_already_wrapped(FooIn, FooOut, Image): '''Tests function wrapper utility return is already wrapped''' QuxOut = NamedTuple('QuxOut', [('value', int)]) def qux(msg: FooIn) -> int: return msg.x + msg.y f, in_, out = _wrap_function(qux) assert _types_equal(in_, FooIn) assert _types_equal(out, QuxOut) assert f(FooIn(1, 2)) == QuxOut(3)
def test_wrap_function_structured_to_structured_args_already_wrapped(FooIn, FooOut, Image): '''Tests function wrapper utility args are already wrapped''' BazIn = NamedTuple('BazIn', [('x', int), ('y', int)]) def baz(x: int, y: int) -> FooOut: return FooOut(x + y) f, in_, out = _wrap_function(baz) assert _types_equal(in_, BazIn) assert _types_equal(out, FooOut) assert f(BazIn(1, 2)) == FooOut(3)
def test_type_equality(): '''Tests that type equality function works as expected''' t1 = NamedTuple('T1', [('x', int), ('y', int)]) t2 = NamedTuple('T1', [('x', int), ('y', float)]) t3 = NamedTuple('T2', [('x', int), ('y', t1)]) t4 = NamedTuple('T2', [('x', int), ('y', t2)]) t5 = NamedTuple('T3', [('x', int), ('y', t1)]) t6 = NamedTuple('T2', [('x', int), ('y', t1)]) t7 = NamedTuple('T2', [('x', int), ('z', t1)]) assert not _types_equal(t1, t2) # type differs assert not _types_equal(t3, t4) # type differs assert not _types_equal(t3, t5) # name differs assert not _types_equal(t3, t7) # field differs assert _types_equal(t3, t6)
def test_wrap_function(): '''Tests function wrapper utility''' FooIn = NamedTuple('FooIn', [('x', int), ('y', int)]) FooOut = NamedTuple('FooOut', [('value', int)]) # ============================================================================= # both args and return need to be wrapped # ============================================================================= def foo(x: int, y: int) -> int: return x + y f, in_, out = _wrap_function(foo) assert _types_equal(in_, FooIn) assert _types_equal(out, FooOut) assert f(FooIn(1, 2)) == FooOut(3) # ============================================================================= # function is already considered wrapped # ============================================================================= def bar(msg: FooIn) -> FooOut: return FooOut(msg.x + msg.y) f, in_, out = _wrap_function(bar) assert f is bar assert _types_equal(in_, FooIn) assert _types_equal(out, FooOut) assert f(FooIn(1, 2)) == FooOut(3) # ============================================================================= # function args need to be wrapped but return is fine # ============================================================================= BazIn = NamedTuple('BazIn', [('x', int), ('y', int)]) def baz(x: int, y: int) -> FooOut: return FooOut(x + y) f, in_, out = _wrap_function(baz) assert _types_equal(in_, BazIn) assert _types_equal(out, FooOut) assert f(BazIn(1, 2)) == FooOut(3) # ============================================================================= # function return needs to be wrapped but args are fine # ============================================================================= QuxOut = NamedTuple('QuxOut', [('value', int)]) def qux(msg: FooIn) -> int: return msg.x + msg.y f, in_, out = _wrap_function(qux) assert _types_equal(in_, FooIn) assert _types_equal(out, QuxOut) assert f(BazIn(1, 2)) == QuxOut(3)
def test_wrap_function_structured_to_raw(FooIn, FooOut, Image): '''Tests function wrapper utility check for structured to user-defined data''' def test_create_image_func(x: int, y: int) -> Image: return Image("b\00" * x * y) f, raw_in, raw_out = _wrap_function(test_create_image_func) assert _types_equal(raw_in, FooIn, ignore_type_name=True) assert is_raw_type(raw_out) assert f(raw_in(2, 2)) == Image("b\00" * 4)
def test_wrap_function_raw_to_structured(FooIn, FooOut, Image): '''Tests function wrapper utility check for user-defined to structured data''' def test_get_image_size_func(image: Image) -> int: return len(image) f, raw_in, raw_out = _wrap_function(test_get_image_size_func) assert is_raw_type(raw_in) assert _types_equal(raw_out, FooOut, ignore_type_name=True) assert f(Image(Image(b"1234"))) == raw_out(4)