Skip to content

Commit

Permalink
Fail freshness if variable is missing. (sodadata#1304)
Browse files Browse the repository at this point in the history
* Fail freshness if variable is missing.

Fix sodadata#1200

Co-authored-by: Vijay Kiran <mail@vijaykiran.com>
  • Loading branch information
m1n0 and vijaykiran authored May 2, 2022
1 parent 3a60572 commit 1f68476
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
28 changes: 15 additions & 13 deletions soda/core/soda/execution/freshness_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def evaluate(self, metrics: Dict[str, Metric], historic_values: Dict[str, object
check_cfg: FreshnessCheckCfg = self.check_cfg

now_variable_name = check_cfg.variable_name
now_variable_timestamp_text = self.data_source_scan.scan.get_variable(now_variable_name)
if not now_variable_timestamp_text:
self.outcome = CheckOutcome.FAIL
self.logs.error(
f"Could not parse variable {now_variable_name} as a timestamp: variable not found",
)
return

max_column_timestamp: Optional[datetime] = metrics.get(MAX_COLUMN_TIMESTAMP).value
now_variable_timestamp: Optional[datetime] = None

Expand All @@ -57,19 +65,13 @@ def evaluate(self, metrics: Dict[str, Metric], historic_values: Dict[str, object
location=self.check_cfg.location,
)

now_variable_timestamp_text = self.data_source_scan.scan.get_variable(now_variable_name)
if now_variable_timestamp_text is not None:
try:
now_variable_timestamp = datetime.fromisoformat(now_variable_timestamp_text)
except:
self.logs.error(
f"Could not parse variable {now_variable_name} as a timestamp: {now_variable_timestamp_text}",
location=check_cfg.location,
)
else:
now_variable_timestamp = self.data_source_scan.scan._data_timestamp
now_variable_name = "scan._scan_time"
now_variable_timestamp_text = str(now_variable_timestamp)
try:
now_variable_timestamp = datetime.fromisoformat(now_variable_timestamp_text)
except:
self.logs.error(
f"Could not parse variable {now_variable_name} as a timestamp: {now_variable_timestamp_text}",
location=check_cfg.location,
)

is_now_variable_timestamp_valid = isinstance(now_variable_timestamp, datetime)

Expand Down
22 changes: 19 additions & 3 deletions soda/core/tests/data_source/test_freshness.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_freshness_timezones_input_no_tz(scanner: Scanner):
table_name = scanner.ensure_test_table(customers_test_table)

scan = scanner.create_test_scan()
scan.add_variables({"NOW": "2020-06-25 00:00:00"})
scan.add_variables({"NOW": "2020-06-25 00:00:00"}) # NOW overrides default "now" variable in scan.
scan.add_sodacl_yaml_str(
f"""
checks for {table_name}:
Expand All @@ -39,7 +39,7 @@ def test_freshness_timezones_input_with_tz(scanner: Scanner):
table_name = scanner.ensure_test_table(customers_test_table)

scan = scanner.create_test_scan()
scan.add_variables({"NOW": "2020-06-25 01:00:00+01:00"})
scan.add_variables({"NOW": "2020-06-25 01:00:00+01:00"}) # NOW overrides default "now" variable in scan.
scan.add_sodacl_yaml_str(
f"""
checks for {table_name}:
Expand Down Expand Up @@ -90,7 +90,7 @@ def test_freshness_warning(scanner: Scanner):
table_name = scanner.ensure_test_table(customers_test_table)

scan = scanner.create_test_scan()
scan.add_variables({"NOW": "2020-06-25 00:00:00"})
scan.add_variables({"NOW": "2020-06-25 00:00:00"}) # NOW overrides default "now" variable in scan.
scan.add_sodacl_yaml_str(
f"""
checks for {table_name}:
Expand Down Expand Up @@ -150,3 +150,19 @@ def test_freshness_no_rows(scanner: Scanner):
scan.execute()

scan.assert_all_checks_fail()


def test_fail_freshness_var_missing(scanner: Scanner):
table_name = scanner.ensure_test_table(customers_test_table)

scan = scanner.create_test_scan()
scan.add_sodacl_yaml_str(
f"""
checks for {table_name}:
- freshness using ts with CUSTOM_USER_VAR < 1d
"""
)
scan.execute(allow_error_warning=True)

scan.assert_all_checks_fail()
scan.assert_log_error("variable not found")
6 changes: 4 additions & 2 deletions soda/core/tests/helpers/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ def enable_mock_soda_cloud(self) -> MockSodaCloud:
self._configuration.soda_cloud = MockSodaCloud()
return self._configuration.soda_cloud

def execute(self):
def execute(self, allow_error_warning: bool = False):
super().execute()
self.assert_no_error_nor_warning_logs()

if not allow_error_warning:
self.assert_no_error_nor_warning_logs()

def execute_unchecked(self):
super().execute()
Expand Down

0 comments on commit 1f68476

Please sign in to comment.