def test_passed_non_namedtuple() -> None: with warnings.catch_warnings(record=True) as record: autotui.namedtuple_prompt_funcs(Broken) assert len(record) == 2 assert "No parameters extracted from object, may not be NamedTuple?" == str( record[1].message)
def test_passed_namedtuple_with_no_attrs() -> None: with pytest.warns( UserWarning, match= r"No parameters extracted from object, may not be NamedTuple?", ): autotui.namedtuple_prompt_funcs(EmptyNamedTuple)
def test_custom_handles_serializers() -> None: t = Temperature("20C") assert t.celsius == 20.0 # just test creating the namedtuple prompt handler = autotui.AutoHandler(func=Temperature, catch_errors=[TypeError, ValueError]) funcs = autotui.namedtuple_prompt_funcs( Reading, type_validators={Temperature: handler}) assert len(funcs.keys()) == 2 f = tempfile.NamedTemporaryFile(delete=False) type_serializers = {Temperature: serialize_temp} attr_deserializers = {"temp": deserialize_temp} d1 = datetime.now() d2 = datetime.now() readings: List[Reading] = [ Reading(when=d1, temp=Temperature("20C")), Reading(when=d2, temp=Temperature("19C")), ] with open(f.name, "w") as fp: autotui.namedtuple_sequence_dump(readings, fp, type_serializers=type_serializers) # loads first, check warning when no deserializer provided with open(f.name) as fp: str_contents = fp.read() with warnings.catch_warnings(record=True) as record: readings_back = autotui.namedtuple_sequence_loads(str_contents, to=Reading) assert len(record) >= 1 assert "No known way to deserialize" in str(record[0].message) assert len(readings_back) == 2 assert readings_back[0].temp == 20.0 # then load properly with open(f.name) as fp: rbr = autotui.namedtuple_sequence_load( fp, to=Reading, attr_deserializers=attr_deserializers) assert len(rbr) == 2 assert int(rbr[0].when.timestamp()) == int(d1.timestamp()) assert rbr[0].temp == Temperature("20C") # delete file os.unlink(f.name)
def test_prompt_funcs() -> None: autotui.namedtuple_prompt_funcs(P) autotui.namedtuple_prompt_funcs(L) autotui.namedtuple_prompt_funcs(O)
def test_cant_handle() -> None: with pytest.raises(AutoTUIException): autotui.namedtuple_prompt_funcs(WeightData)
def test_attr_auto_handler() -> None: handler = autotui.AutoHandler(func=Weight, catch_errors=[ValueError]) funcs = autotui.namedtuple_prompt_funcs(WeightData, attr_validators={"data": handler}) assert len(funcs.keys()) == 2
def test_type_auto_handler() -> None: handler = autotui.AutoHandler(func=Weight, catch_errors=[ValueError]) # use handler to specify how to create/catch funcs = autotui.namedtuple_prompt_funcs(WeightData, type_validators={Weight: handler}) assert len(funcs.keys()) == 2