test_json_match.py
1 import pandas as pd 2 3 from evidently.legacy.features.json_match_feature import JSONMatch 4 from evidently.legacy.pipeline.column_mapping import ColumnMapping 5 from evidently.legacy.utils.data_preprocessing import create_data_definition 6 7 8 def test_is_valid_sql_feature(): 9 feature_generator = JSONMatch( 10 first_column="col_1", second_column="col_2", display_name="Json Match", feature_type="num", name="is_json_match" 11 ) 12 13 # Define JSON strings for each scenario 14 scenarios = [ 15 # Scenario 1 - Matching JSONs 16 ('{"name": "Alice", "age": 25, "city": "London"}', '{"city": "London", "age": 25, "name": "Alice"}'), 17 # Scenario 2 - Different whitespace (still matching) 18 ('{ "name" : "Bob" , "age" : 22 , "city" : "Paris" }', '{"city": "Paris", "name": "Bob", "age": 22}'), 19 # Scenario 3 - Invalid JSON in one column 20 ( 21 '{"name": "Eve", "age": 28, "city": "Berlin"}', 22 '{"city": "Berlin", "age": 28, "name": Eve}', 23 ), # Missing quotes around "Eve" 24 # Scenario 4 - Keys mismatch 25 ( 26 '{"name": "Charlie", "age": 30, "country": "USA"}', 27 '{"name": "Charlie", "age": 30, "city": "USA"}', 28 ), # 'country' vs 'city' 29 # Scenario 5 - Values mismatch 30 ( 31 '{"name": "David", "age": 35, "city": "Tokyo"}', 32 '{"city": "Tokyo", "age": 35, "name": "Daniel"}', 33 ), # 'David' vs 'Daniel' 34 ] 35 36 # Create DataFrame 37 data = pd.DataFrame(scenarios, columns=["col_1", "col_2"]) 38 39 result = feature_generator.generate_feature( 40 data=data, 41 data_definition=create_data_definition(None, data, ColumnMapping()), 42 ) 43 44 expected_result = pd.DataFrame(dict(is_json_match=[True, True, False, False, False])) 45 46 print(result) 47 48 print(expected_result) 49 50 try: 51 assert result.equals(expected_result) 52 return True 53 except AssertionError: 54 return False 55 56 57 print(test_is_valid_sql_feature())