|
| 1 | +const commonAssertions = require("../index"); |
| 2 | + |
| 3 | +const commonAssertionsResult = commonAssertions({ |
| 4 | + globalAssertionsParams: { |
| 5 | + "database": "sandbox-hrialan", |
| 6 | + "schema": "assertions_" + dataform.projectConfig.vars.env, |
| 7 | + "location": "EU", |
| 8 | + "tags": ["assertions"], |
| 9 | + // Sometimes data quality is not good in some environments, |
| 10 | + // so we can disable the assertions in those environments. |
| 11 | + // "disabledInEnvs": ["dv", "qa"] |
| 12 | + }, |
| 13 | + rowConditions: { |
| 14 | + "first_table": { |
| 15 | + "id_not_null": "id IS NOT NULL", |
| 16 | + "id_strict_positive": "id > 0" |
| 17 | + }, |
| 18 | + "second_table": { |
| 19 | + "id_not_null": "id IS NOT NULL" |
| 20 | + } |
| 21 | + }, |
| 22 | + uniqueKeyConditions: { |
| 23 | + "first_table": ["id"], |
| 24 | + "second_table": ["id"] |
| 25 | + }, |
| 26 | + dataFreshnessConditions: { |
| 27 | + "first_table": { |
| 28 | + "dateColumn": "updated_date", |
| 29 | + "timeUnit": "DAY", |
| 30 | + "delayCondition": 1, |
| 31 | + }, |
| 32 | + "second_table": { |
| 33 | + "dateColumn": "updated_date", |
| 34 | + "timeUnit": "MONTH", |
| 35 | + "delayCondition": 3, |
| 36 | + } |
| 37 | + }, |
| 38 | + dataCompletenessConditions: { |
| 39 | + "first_table": { |
| 40 | + // Format: "column": allowedPercentageNull |
| 41 | + "updated_date": 1, // 1% of null values allowed in the updated_date column |
| 42 | + "id": 20 |
| 43 | + }, |
| 44 | + "second_table": { |
| 45 | + "id": 30 |
| 46 | + } |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +/* |
| 51 | + * ASSERTIONS AUDIT TABLE EXAMPLE |
| 52 | + * The following code snippet is used to publish the results of the created assertions in a table for audit purposes. |
| 53 | + * The result is a table with the following columns: |
| 54 | + * | assertion_name | assertion_type | |
| 55 | + * |----------------|----------------| |
| 56 | + * | id_not_null | row_condition | |
| 57 | + * | ... | ... | |
| 58 | + */ |
| 59 | + |
| 60 | +let selectClauses = []; |
| 61 | + |
| 62 | +for (const key in commonAssertionsResult) { |
| 63 | + if (commonAssertionsResult.hasOwnProperty(key)) { |
| 64 | + const commonAssertionsResultForKey = commonAssertionsResult[key]; |
| 65 | + if (commonAssertionsResultForKey.length > 0) { |
| 66 | + const selectClause = commonAssertionsResultForKey.map(assertion => { |
| 67 | + return `SELECT "${assertion.proto.target.name}" AS assertion_name, '${key}' AS assertion_type`; |
| 68 | + }).join("\n UNION ALL \n"); |
| 69 | + |
| 70 | + selectClauses.push(selectClause); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +const sqlQuery = selectClauses.join("\n UNION ALL \n"); |
| 76 | + |
| 77 | +publish("assertions_audit", { |
| 78 | + type: "table" |
| 79 | +}).query( |
| 80 | + (ctx) => sqlQuery |
| 81 | +); |
0 commit comments