def test_add_type_with_parent(): type_sys = TypeSystem() type_sys.add_type(Double, inference_function=double_func) type_sys.add_type(Integer, inference_function=integer_func, parent=Double) assert Integer in type_sys.inference_functions.keys() assert type_sys.inference_functions[Integer] is integer_func assert len(type_sys.relationships) == 1 assert type_sys.relationships[0] == (Double, Integer)
def test_add_type_without_parent(): type_sys = TypeSystem() type_sys.add_type(Double, inference_function=double_func) assert Double in type_sys.inference_functions.keys() assert type_sys.inference_functions[Double] is double_func type_sys.add_type(Integer) assert Integer in type_sys.inference_functions.keys() assert type_sys.inference_functions[Integer] is None
def test_str_to_logical_type(): all_types = ww.type_system.registered_types with pytest.raises(ValueError, match='String test is not a valid logical type'): ww.type_system.str_to_logical_type('test') assert ww.type_system.str_to_logical_type('test', raise_error=False) is None for logical_type in all_types: assert ww.type_system.str_to_logical_type( logical_type.__name__) == logical_type assert ww.type_system.str_to_logical_type( logical_type.type_string) == logical_type assert ww.type_system.str_to_logical_type('bOoLeAn') == Boolean assert ww.type_system.str_to_logical_type( 'person_full_NAME') == PersonFullName assert ww.type_system.str_to_logical_type( 'PersonFullnamE') == PersonFullName ymd = '%Y-%m-%d' datetime_with_format = ww.type_system.str_to_logical_type( 'datetime', params={'datetime_format': ymd}) assert datetime_with_format.__class__ == Datetime assert datetime_with_format.datetime_format == ymd assert datetime_with_format == Datetime(datetime_format=ymd) datetime_no_format = ww.type_system.str_to_logical_type( 'datetime', params={'datetime_format': None}) assert datetime_no_format.__class__ == Datetime assert datetime_no_format.datetime_format is None assert datetime_no_format == Datetime() # When parameters are supplied in a non-empty dictionary, the logical type gets instantiated assert ww.type_system.str_to_logical_type('person_full_NAME', params={}) == PersonFullName assert datetime_no_format != Datetime # Input a different type system new_type_sys = TypeSystem() with pytest.raises(ValueError, match='String Integer is not a valid logical type'): new_type_sys.str_to_logical_type('Integer') new_type_sys.add_type(Boolean) assert Boolean == new_type_sys.str_to_logical_type('Boolean')
def test_str_to_logical_type(): all_types = ww.type_system.registered_types with pytest.raises(ValueError, match="String test is not a valid logical type"): ww.type_system.str_to_logical_type("test") assert ww.type_system.str_to_logical_type("test", raise_error=False) is None for logical_type in all_types: assert ww.type_system.str_to_logical_type( logical_type.__name__) == logical_type assert (ww.type_system.str_to_logical_type( logical_type.type_string) == logical_type) assert ww.type_system.str_to_logical_type("bOoLeAn") == Boolean assert ww.type_system.str_to_logical_type( "person_full_NAME") == PersonFullName assert ww.type_system.str_to_logical_type( "PersonFullnamE") == PersonFullName ymd = "%Y-%m-%d" datetime_with_format = ww.type_system.str_to_logical_type( "datetime", params={"datetime_format": ymd}) assert datetime_with_format.__class__ == Datetime assert datetime_with_format.datetime_format == ymd assert datetime_with_format == Datetime(datetime_format=ymd) datetime_no_format = ww.type_system.str_to_logical_type( "datetime", params={"datetime_format": None}) assert datetime_no_format.__class__ == Datetime assert datetime_no_format.datetime_format is None assert datetime_no_format == Datetime() # When parameters are supplied in a non-empty dictionary, the logical type gets instantiated assert (ww.type_system.str_to_logical_type("person_full_NAME", params={}) == PersonFullName) assert datetime_no_format != Datetime # Input a different type system new_type_sys = TypeSystem() with pytest.raises(ValueError, match="String Integer is not a valid logical type"): new_type_sys.str_to_logical_type("Integer") new_type_sys.add_type(Boolean) assert Boolean == new_type_sys.str_to_logical_type("Boolean")
def test_inference_multiple_matches_different_depths(default_relationships): def always_true(series): return True inference_functions = { Categorical: always_true, Double: always_true, Integer: always_true, CountryCode: always_true, } type_sys = TypeSystem(inference_functions=inference_functions, relationships=default_relationships, default_type=NaturalLanguage) type_sys.update_inference_function(Integer, always_true) type_sys.update_inference_function(CountryCode, always_true) type_sys.add_type(SubRegionCode, inference_function=always_true, parent=CountryCode) inferred_type = type_sys.infer_logical_type(pd.Series([1, 2, 3])) # Should match SubRegionCode as it is the deepest match assert inferred_type == SubRegionCode