def test_type_system_default_type(default_inference_functions, default_relationships): type_sys = TypeSystem(inference_functions=default_inference_functions, relationships=default_relationships, default_type=SubRegionCode) assert type_sys.default_type == SubRegionCode type_sys.update_inference_function(Categorical, None) test_series = pd.Series(['a', 'b', 'c']) assert type_sys.infer_logical_type(test_series) == SubRegionCode assert SubRegionCode in type_sys.registered_types
def test_nl_inference_not_called_with_other_matches(nl_mock, pandas_integers): assert isinstance(ww.type_system.infer_logical_type(pandas_integers[0]), Integer) new_type_sys = TypeSystem( inference_functions=DEFAULT_INFERENCE_FUNCTIONS, relationships=DEFAULT_RELATIONSHIPS, default_type=DEFAULT_TYPE, ) new_type_sys.inference_functions[NaturalLanguage] = nl_mock _ = new_type_sys.infer_logical_type(pandas_integers[0]) assert not nl_mock.called
def test_nl_inference_called_with_unknown_type(nl_mock, pandas_strings): assert isinstance(ww.type_system.infer_logical_type(pandas_strings[0]), Unknown) new_type_sys = TypeSystem( inference_functions=DEFAULT_INFERENCE_FUNCTIONS, relationships=DEFAULT_RELATIONSHIPS, default_type=DEFAULT_TYPE, ) new_type_sys.inference_functions[NaturalLanguage] = nl_mock _ = new_type_sys.infer_logical_type(pandas_strings[0]) assert nl_mock.called
def test_type_system_default_type_remove_error(default_inference_functions, default_relationships): type_sys = TypeSystem(inference_functions=default_inference_functions, relationships=default_relationships, default_type=SubRegionCode) error_msg = "Default LogicalType cannot be removed" with pytest.raises(ValueError, match=error_msg): type_sys.remove_type(SubRegionCode) with pytest.raises(ValueError, match=error_msg): type_sys.remove_type('SubRegionCode') with pytest.raises(ValueError, match=error_msg): type_sys.remove_type('sub_region_code')
def test_type_system_init(default_inference_functions, default_relationships): type_sys = TypeSystem(inference_functions=default_inference_functions, relationships=default_relationships) assert len(type_sys.inference_functions) == 5 assert type_sys.inference_functions[Double] is double_func assert type_sys.inference_functions[Integer] is integer_func assert type_sys.inference_functions[Categorical] is categorical_func assert len(type_sys.relationships) == 2 assert type_sys.relationships[0] == (Double, Integer) assert type_sys.relationships[1] == (Categorical, CountryCode)
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
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_same_depth(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) inferred_type = type_sys.infer_logical_type(pd.Series([1, 2, 3])) # Should match CountryCode - same depth as Integer, but CountryCode parent # (Categorical) is tried and found first assert inferred_type == CountryCode
def type_sys(default_inference_functions, default_relationships): return TypeSystem(inference_functions=default_inference_functions, relationships=default_relationships, default_type=NaturalLanguage)
def type_sys(default_inference_functions, default_relationships): return TypeSystem( inference_functions=default_inference_functions, relationships=default_relationships, default_type=Unknown, )