Created
September 2, 2022 06:36
-
-
Save DaveFlynn/37b9f92e2784187566a6b4f8d3101250 to your computer and use it in GitHub Desktop.
PIpeRider custom assertion to allow a specific number of null values in a column
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AssertAllowedNulls(BaseAssertionType): | |
def name(self): | |
return "assert_allowed_nulls" | |
def execute(self, context: AssertionContext, table: str, column: str, metrics: dict) -> AssertionResult: | |
column_metrics = metrics.get('tables', {}).get(table, {}).get('columns', {}).get(column) | |
if column_metrics is None: | |
# column could not be found | |
return context.result.fail('column does not exist') | |
# 1. Get the metric for the desired column, e.g. num of nulls in column | |
nulls = column_metrics.get('nulls') | |
# 2. Get expectation from assertion file | |
allowed_nulls = context.asserts.get('allowed_nulls', []) | |
# 3. Implement logic to compare expectated and actual metic value | |
if (nulls <= allowed_nulls): | |
# 4. return pass (acceptable number of nulls) | |
return context.result.success("{}".format(nulls)) | |
else: | |
# 4. return fail (more nulls than we expected) | |
return context.result.fail("{}".format(nulls)) | |
def validate(self, context: AssertionContext) -> ValidationResult: | |
result = ValidationResult(context) | |
# result.errors.append('explain to users why this broken') | |
return result | |
# register new assertions | |
register_assertion_function(AssertAllowedNulls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment