@@ -72,31 +72,28 @@ def test_data_completeness(snowflake_conn):
7272 """Test for data completeness - no nulls and all machines have records"""
7373 cursor = snowflake_conn .cursor ()
7474 try :
75- # Check for NULL values in critical columns using CASE statements
75+ # Check for NULL values in critical columns
7676 cursor .execute ("""
7777 SELECT COUNT(*)
7878 FROM FACTORY_PIPELINE_DEMO.PUBLIC_marts.machine_health_metrics
79- WHERE machine_id = ''
80- OR health_status = ''
81- OR failure_risk_score = 0
82- OR maintenance_recommendation = ''
79+ WHERE machine_id IS NULL
80+ OR health_status IS NULL
81+ OR failure_risk_score IS NULL
82+ OR maintenance_recommendation IS NULL
8383 """ )
8484 null_count = cursor .fetchone ()[0 ]
85- assert null_count == 0 , "Critical columns should not be empty "
85+ assert null_count == 0 , "Critical columns should not contain NULL values "
8686
87- # Check if each machine has at least one record using EXISTS
87+ # Check if each machine has at least one record
8888 cursor .execute ("""
89- SELECT m1.machine_id
90- FROM FACTORY_PIPELINE_DEMO.PUBLIC.RAW_SENSOR_DATA m1
91- WHERE NOT EXISTS (
92- SELECT 1
93- FROM FACTORY_PIPELINE_DEMO.PUBLIC_marts.machine_health_metrics m2
94- WHERE m1.machine_id = m2.machine_id
95- )
96- LIMIT 1
89+ SELECT COUNT(*)
90+ FROM FACTORY_PIPELINE_DEMO.PUBLIC.RAW_SENSOR_DATA s
91+ LEFT JOIN FACTORY_PIPELINE_DEMO.PUBLIC_marts.machine_health_metrics m
92+ ON s.machine_id = m.machine_id
93+ WHERE m.machine_id IS NULL
9794 """ )
98- missing_machines = cursor .fetchone ()
99- assert missing_machines is None , "All machines should have health metrics"
95+ missing_machines = cursor .fetchone ()[ 0 ]
96+ assert missing_machines == 0 , "All machines should have health metrics"
10097
10198 finally :
10299 cursor .close ()
@@ -222,19 +219,16 @@ def test_data_relationships(snowflake_conn):
222219 """Test relationships between metrics and source data"""
223220 cursor = snowflake_conn .cursor ()
224221 try :
225- # Check if all machines in metrics exist in sensor data using EXISTS
222+ # Check if all machines in metrics exist in sensor data
226223 cursor .execute ("""
227- SELECT m.machine_id
228- FROM FACTORY_PIPELINE_DEMO.PUBLIC_marts.machine_health_metrics m
229- WHERE NOT EXISTS (
230- SELECT 1
231- FROM FACTORY_PIPELINE_DEMO.PUBLIC.RAW_SENSOR_DATA s
232- WHERE m.machine_id = s.machine_id
233- )
234- LIMIT 1
224+ SELECT COUNT(*)
225+ FROM FACTORY_PIPELINE_DEMO.PUBLIC_marts.machine_health_metrics m
226+ LEFT JOIN FACTORY_PIPELINE_DEMO.PUBLIC.RAW_SENSOR_DATA s
227+ ON m.machine_id = s.machine_id
228+ WHERE s.machine_id IS NULL
235229 """ )
236- orphaned_metric = cursor .fetchone ()
237- assert orphaned_metric is None , "All machines in metrics should exist in sensor data"
230+ orphaned_metrics = cursor .fetchone ()[ 0 ]
231+ assert orphaned_metrics == 0 , "All machines in metrics should exist in sensor data"
238232
239233 # Check if metrics align with recent sensor data
240234 cursor .execute ("""
0 commit comments