Skip to content

Commit 691fc82

Browse files
ddeleodanieldeleo
authored andcommitted
Add missing phone_assertions.js file
Javscript and Markdown formatting Upgrade dataform cli version in package.json to 1.20.0
1 parent 2e30dc0 commit 691fc82

13 files changed

+299
-210
lines changed

dataform/examples/dataform_assertion_unit_test/README.md

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
# Dataform Custom Assertions
2-
Dataform is a platform to manage data in Big Query and other data warehouses and dataform perform the **T (Transformation)** in the **ELT** Pipeline. But before any transformaiton can be done, we need to make sure our input data is valid. Dataform has many built in data assertions such as uniqueKey, nonNull, and etc. But you can also customize your dataform assertions to meet individual's needs. This directory gives you a set of custom assertions that you can use in testing your data quailty in your project.
3-
## Requirement
4-
In order to test and run the custom dataform assertions, the person has to have have:
2+
3+
Dataform is a platform to manage data in Big Query and other data warehouses and
4+
dataform perform the **T (Transformation)** in the **ELT** Pipeline. But before
5+
any transformaiton can be done, we need to make sure our input data is valid.
6+
Dataform has many built in data assertions such as uniqueKey, nonNull, and etc.
7+
But you can also customize your dataform assertions to meet individual's needs.
8+
This directory gives you a set of custom assertions that you can use in testing
9+
your data quailty in your project.
10+
11+
## Requirement
12+
13+
In order to test and run the custom dataform assertions, the person has to have
14+
have:
15+
516
- A dataform project
617
- Credentials granting access to bigquery warehouse .df-credentials
718

819
## Usage
9-
The custom assertions are in javascript files in the includes folder. To test them, simply refer to the function in your transformation query. For example, if a person wants to use the ```test_telephone_number_digits(colName)``` assertions in the ```phone_assertions.js``` file, the person only needs to refer in the config section of the transformation query like:
20+
21+
The custom assertions are in javascript files in the includes folder. To test
22+
them, simply refer to the function in your transformation query. For example, if
23+
a person wants to use the ```test_telephone_number_digits(colName)``` assertions
24+
in the ```phone_assertions.js``` file, the person only needs to refer in the
25+
config section of the transformation query like:
1026

1127
```
1228
type: ....,
@@ -20,13 +36,23 @@ The custom assertions are in javascript files in the includes folder. To test th
2036
]
2137
}
2238
```
39+
2340
## Unit Testing Custom Assertions
24-
Unit testing your custom assertions is important because it helps you safeguard your ELT pipeline. In this project, we want to demonstrate an easy way for you to unit test your custom assertions. The workflow is simple and as listed below:
2541

26-
* Create a ```test_[NAME_YOUR_TEST]_assertions.js``` file in the ```definitions/tests/``` folder if your custom row assertions are not included in the existing template.
27-
* In ```test_[NAME_YOUR_TEST]_assertions.js``` change the code snippets ```const {[YOUR_CUSTOM_ASSERTION]} = [CUSTOMER_ASSERTION_FILE_NAME];``` and change the test name ```const test_name = "[YOUR_TEST_NAME]";```.
28-
* Add the testing data in the ```test_cases``` block with the following format ```"[INPUT]" : "[EXPECTED_OUTPUT]"```
29-
* Finally supply your custom function name in the ```generatetest(...)``` function.
42+
Unit testing your custom assertions is important because it helps you safeguard
43+
your ELT pipeline. In this project, we want to demonstrate an easy way for you
44+
to unit test your custom assertions. The workflow is simple and as listed below:
45+
46+
* Create a ```test_[NAME_YOUR_TEST]_assertions.js``` file in
47+
the ```definitions/tests/``` folder if your custom row assertions are not
48+
included in the existing template.
49+
* In ```test_[NAME_YOUR_TEST]_assertions.js``` change the code
50+
snippets ```const {[YOUR_CUSTOM_ASSERTION]} = [CUSTOMER_ASSERTION_FILE_NAME];```
51+
and change the test name ```const test_name = "[YOUR_TEST_NAME]";```.
52+
* Add the testing data in the ```test_cases``` block with the following
53+
format ```"[INPUT]" : "[EXPECTED_OUTPUT]"```
54+
* Finally supply your custom function name in the ```generatetest(...)```
55+
function.
3056

3157
Below is an example of the ```test_[NAME_YOUR_TEST]_assertions.js``` file:
3258

@@ -54,7 +80,12 @@ generate_test(test_name,
5480
[YOUT_CUSTOM_ASSERTIONS]);
5581
```
5682

57-
* Afterwards you can perform the unit test by running ```dataform test``` command.
58-
83+
* Afterwards you can perform the unit test by running ```dataform test```
84+
command.
85+
5986
## Liscense
60-
All solutions within this repository are provided under the [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) license. Please see the [LICENSE](https://www.apache.org/licenses/LICENSE-2.0) file for more detailed terms and conditions.
87+
88+
All solutions within this repository are provided under
89+
the [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) license. Please
90+
see the [LICENSE](https://www.apache.org/licenses/LICENSE-2.0) file for more
91+
detailed terms and conditions.

dataform/examples/dataform_assertion_unit_test/dataform.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
"defaultSchema": "dataform",
44
"assertionSchema": "dataform_assertions",
55
"defaultDatabase": "YOUR_PROJECT_ID",
6-
"useRunCache": false
6+
"concurrentQueryLimit": 100
77
}

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_date_assertions.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@ const {generate_test} = unit_test_utils;
1616
const {test_date} = date_assertions;
1717
const test_name = "test_date_assertions";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"1997/11/03" : "TRUE",
28-
"2008/08/08" : "TRUE",
29-
"1996/11/03" : "TRUE",
30-
"2005/04/13" : "TRUE",
31-
"1998/11/03" : "TRUE",
32-
"2006/07/29" : "TRUE",
33-
"2025/03/24" : "FALSE",
34-
"1769/03/24" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"1997/11/03": "TRUE",
28+
"2008/08/08": "TRUE",
29+
"1996/11/03": "TRUE",
30+
"2005/04/13": "TRUE",
31+
"1998/11/03": "TRUE",
32+
"2006/07/29": "TRUE",
33+
"2025/03/24": "FALSE",
34+
"1769/03/24": "FALSE"
3535
};
3636
// The function below will generate the necessary SQL to run unit tests.
37-
generate_test(test_name,
38-
test_cases,
39-
test_date);
37+
generate_test(test_name, test_cases, test_date);
4038

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_email_assertions.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,18 @@ const {generate_test} = unit_test_utils;
1616
const {test_email_validity} = personal_info_assertions;
1717
const test_name = "test_email_assertion_test";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"ruinanliu@google.com" : "TRUE",
28-
"[email protected]" : "TRUE",
29-
"1736#$%.com" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"someone@google.com": "TRUE",
28+
"[email protected]": "TRUE",
29+
"1736#$%.com": "FALSE"
3030
};
3131
// The function below will generate the necessary SQL to run unit tests.
32-
generate_test(test_name,
33-
test_cases,
34-
test_email_validity);
32+
generate_test(test_name, test_cases, test_email_validity);
3533

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_gender_assertions.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ const {generate_test} = unit_test_utils;
1616
const {test_gender_status} = personal_info_assertions;
1717
const test_name = "test_gemder_assertions";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"Female" : "TRUE",
28-
"Male" : "TRUE",
29-
"one" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"Female": "TRUE",
28+
"Male": "TRUE",
29+
"one": "FALSE"
3030
};
3131
// The function below will generate the necessary SQL to run unit tests.
3232
generate_test(test_name,

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_marital_status_assertions.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ const {generate_test} = unit_test_utils;
1616
const {test_marital_status} = personal_info_assertions;
1717
const test_name = "test_marital_status_assertions";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"Married" : "TRUE",
28-
"Divorced" : "TRUE",
29-
"Happy" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"Married": "TRUE",
28+
"Divorced": "TRUE",
29+
"Happy": "FALSE"
3030
};
3131
// The function below will generate the necessary SQL to run unit tests.
3232
generate_test(test_name,

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_personal_info_assertions.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ const {generate_test} = unit_test_utils;
1616
const {test_name} = personal_info_assertions;
1717
const test_file_name = "test_personal_info_assertions";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"Alan" : "TRUE",
28-
"Bob" : "TRUE",
29-
"Jack" : "TRUE",
30-
"John" : "TRUE",
31-
"y*(*&^^%$" : "FALSE",
32-
"Alannnn" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"Alan": "TRUE",
28+
"Bob": "TRUE",
29+
"Jack": "TRUE",
30+
"John": "TRUE",
31+
"y*(*&^^%$": "FALSE",
32+
"Alannnn": "FALSE"
3333
};
3434
// The function below will generate the necessary SQL to run unit tests.
3535
generate_test(test_file_name,

dataform/examples/dataform_assertion_unit_test/definitions/tests/test_telephone_number_assertions.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,26 @@ const {generate_test} = unit_test_utils;
1616
const {test_phone_number} = phone_assertions;
1717
const test_name = "test_telephone_number_assertions";
1818
const test_cases = {
19-
/*
20-
Provide your own testing data following the structure
21-
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22-
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23-
then the program will expect the custom data quality rules to also produce TRUE.
24-
Otherwise it will show that the custom data quality rules failed.
25-
*/
26-
27-
"8123456789" : "TRUE",
28-
"1234567899" : "TRUE",
29-
"5123456789" : "TRUE",
30-
"4576839485" : "TRUE",
31-
"2938475638" : "TRUE",
32-
"7928374657" : "TRUE",
33-
"7847563738" : "TRUE",
34-
"6768907654" : "TRUE",
35-
"1234567" : "FALSE",
36-
"0123456789" : "FALSE",
37-
"1111111111" : "FALSE",
38-
"374657389a" : "FALSE"
19+
/*
20+
Provide your own testing data following the structure
21+
<INPUT_TESTING_DATA> : "<EXPECTED OUTCOME>"
22+
For example, if a testing data has the <EXPECTED OUTCOME> to be TRUE,
23+
then the program will expect the custom data quality rules to also produce TRUE.
24+
Otherwise it will show that the custom data quality rules failed.
25+
*/
26+
27+
"8123456789": "TRUE",
28+
"1234567899": "TRUE",
29+
"5123456789": "TRUE",
30+
"4576839485": "TRUE",
31+
"2938475638": "TRUE",
32+
"7928374657": "TRUE",
33+
"7847563738": "TRUE",
34+
"6768907654": "TRUE",
35+
"1234567": "FALSE",
36+
"0123456789": "FALSE",
37+
"1111111111": "FALSE",
38+
"374657389a": "FALSE"
3939
};
4040
// The function below will generate the necessary SQL to run unit tests.
4141
generate_test(test_name,

dataform/examples/dataform_assertion_unit_test/includes/date_assertions.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,53 @@
1515
/*
1616
This assertion checks whether input date is future
1717
*/
18-
function test_future_date(colName){
19-
var result_query = `PARSE_DATE('%Y/%m/%d', ${colName}) < CURRENT_DATE()`
20-
return result_query
18+
function test_future_date(colName) {
19+
var result_query = `PARSE_DATE('%Y/%m/%d', ${colName}) < CURRENT_DATE()`
20+
return result_query
2121
}
2222

2323
/*
2424
This assertion checks whether the input birthdate is less than 100 yrs old
2525
*/
26-
function test_valid_years(colName){
27-
var result_query = `DATE_DIFF(CURRENT_DATE(), PARSE_DATE('%Y/%m/%d', ${colName}), YEAR) < 100`
28-
return result_query
26+
function test_valid_years(colName) {
27+
var result_query = `DATE_DIFF(CURRENT_DATE(), PARSE_DATE('%Y/%m/%d', ${colName}), YEAR) < 100`
28+
return result_query
2929
}
3030

3131
/*
3232
This function checks whether the format of the date is correct
3333
*/
34-
function test_date_format(colName, date_format){
35-
if(date_format == "yyyy/mm/dd"){
36-
var result_query = `REGEXP_CONTAINS(${colName}, r'^[0-9]{4}[/][0-9]{2}[/][0-9]{2}$')`
37-
return result_query
38-
} else if (date_format == "yyyymmdd"){
39-
var result_query = `REGEXP_CONTAINS(${colName}, r'^[0-9]{4}[0-9]{2}[0-9]{2}$')`
40-
return result_query
41-
}else{
42-
return `FALSE`
43-
}
34+
function test_date_format(colName, date_format) {
35+
if (date_format == "yyyy/mm/dd") {
36+
var result_query = `REGEXP_CONTAINS(${colName}, r'^[0-9]{4}[/][0-9]{2}[/][0-9]{2}$')`
37+
return result_query
38+
} else if (date_format == "yyyymmdd") {
39+
var result_query = `REGEXP_CONTAINS(${colName}, r'^[0-9]{4}[0-9]{2}[0-9]{2}$')`
40+
return result_query
41+
} else {
42+
return `FALSE`
43+
}
4444
}
4545

4646
/*
4747
This assertions combines custom assertions for testing future date and valid years
4848
*/
4949

50-
function test_date(colName){
51-
var result_query =
52-
`IF(${colName} IS NOT NULL AND ${colName} <> "",` +
53-
`IF(${test_date_format(colName, "yyyy/mm/dd")}, ` +
54-
`IF(${test_future_date(colName)}, ` +
55-
`${test_valid_years(colName, 100)}` +
56-
`, FALSE),` +
57-
`IF(${test_date_format(colName, "yyyymmdd")}, ` +
58-
`TRUE, FALSE)), FALSE)`
59-
return result_query
50+
function test_date(colName) {
51+
var result_query =
52+
`IF(${colName} IS NOT NULL AND ${colName} <> "",` +
53+
`IF(${test_date_format(colName, "yyyy/mm/dd")}, ` +
54+
`IF(${test_future_date(colName)}, ` +
55+
`${test_valid_years(colName, 100)}` +
56+
`, FALSE),` +
57+
`IF(${test_date_format(colName, "yyyymmdd")}, ` +
58+
`TRUE, FALSE)), FALSE)`
59+
return result_query
6060
}
6161

6262
module.exports = {
63-
test_future_date,
64-
test_valid_years,
65-
test_date_format,
66-
test_date
63+
test_future_date,
64+
test_valid_years,
65+
test_date_format,
66+
test_date
6767
}

0 commit comments

Comments
 (0)