Skip to content

Commit c05da39

Browse files
committed
add unit tests
1 parent 92dfc8d commit c05da39

1 file changed

Lines changed: 114 additions & 11 deletions

File tree

docs/examples/sqlmesh_cli_crash_course.md

Lines changed: 114 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ You’ll use these commands 80% of the time because this is how you apply code c
1717
5. Run data diff against prod
1818
6. Apply the changes to prod
1919

20-
---
20+
### Preview, Apply, and Audit Changes in dev
2121

2222
All these steps are bundled into a single command below:
2323

@@ -146,6 +146,7 @@ All these steps are bundled into a single command below:
146146
✔ Virtual layer updated
147147
```
148148

149+
### Run Data Diff Against Prod
149150

150151
Run data diff against prod. This is a good way to verify the changes are behaving as expected **after** applying them to `dev`.
151152

@@ -237,6 +238,8 @@ Run data diff against prod. This is a good way to verify the changes are behavin
237238
└─────────┴──────┴─────┘
238239
```
239240

241+
### Apply Changes to Prod
242+
240243
!!! warning "Apply the changes to prod"
241244
This step is recommended **only in CI/CD** as best practice. # TODO: link to github cicd bot setup
242245
For learning purposes and hot fixes, you can apply the changes to prod by entering `y` at the prompt.
@@ -332,6 +335,10 @@ You'll use these commands to validate your changes are behaving as expected. Aud
332335
3. Ad hoc query the data directly in the CLI
333336
4. Add linting
334337

338+
---
339+
340+
### Create and Audit External Models
341+
335342
=== "SQLMesh"
336343

337344
```bash
@@ -465,6 +472,112 @@ You'll use these commands to validate your changes are behaving as expected. Aud
465472
Apply - Backfill Tables [y/n]:
466473
```
467474

475+
### Automatically Generate Unit Tests
476+
477+
=== "SQLMesh"
478+
479+
```bash
480+
sqlmesh create_test sqlmesh_example.full_model \
481+
--query sqlmesh_example.incremental_model \
482+
"select * from sqlmesh_example.incremental_model limit 5"
483+
```
484+
485+
```bash
486+
sqlmesh create_test <model_name> \
487+
--query <model_name upstream> \
488+
"select * from <model_name upstream> limit 5"
489+
```
490+
491+
492+
=== "Tobiko Cloud"
493+
494+
```bash
495+
tcloud sqlmesh create_test demo.stg_payments \
496+
--query demo.seed_raw_payments \
497+
"select * from demo.seed_raw_payments limit 5"
498+
```
499+
500+
```bash
501+
tcloud sqlmesh create_test <model_name> \
502+
--query <model_name upstream> \
503+
"select * from <model_name upstream> limit 5"
504+
```
505+
506+
??? "Example Output"
507+
- Generated unit tests for the `sqlmesh_example.full_model` model live querying the data.
508+
- I ran the tests and they passed locally.
509+
- If you're using a cloud data warehouse, this will transpile your SQL syntax to its equivalent in duckdb.
510+
- This runs fast and free on your local machine.
511+
512+
```yaml
513+
# tests/test_full_model.yaml
514+
test_full_model:
515+
model: '"db"."sqlmesh_example"."full_model"'
516+
inputs:
517+
'"db"."sqlmesh_example"."incremental_model"':
518+
- id: -11
519+
item_id: -11
520+
event_date: 2020-01-01
521+
new_column: 7
522+
- id: 1
523+
item_id: 1
524+
event_date: 2020-01-01
525+
new_column: 7
526+
- id: 3
527+
item_id: 3
528+
event_date: 2020-01-03
529+
new_column: 7
530+
- id: 4
531+
item_id: 1
532+
event_date: 2020-01-04
533+
new_column: 7
534+
- id: 5
535+
item_id: 1
536+
event_date: 2020-01-05
537+
new_column: 7
538+
outputs:
539+
query:
540+
- item_id: 3
541+
num_orders: 1
542+
new_column: 7
543+
- item_id: 1
544+
num_orders: 3
545+
new_column: 7
546+
- item_id: -11
547+
num_orders: 1
548+
new_column: 7
549+
```
550+
551+
```bash
552+
(demo) ➜ demo git:(main) ✗ sqlmesh test
553+
.
554+
----------------------------------------------------------------------
555+
Ran 1 test in 0.053s
556+
557+
OK
558+
```
559+
560+
```bash
561+
# what if the test fails?
562+
(demo) ➜ demo git:(main) ✗ sqlmesh test
563+
F
564+
======================================================================
565+
FAIL: test_full_model (/Users/sung/Desktop/git_repos/sqlmesh-cli-revamp/tests/test_full_model.yaml)
566+
None
567+
----------------------------------------------------------------------
568+
AssertionError: Data mismatch (exp: expected, act: actual)
569+
570+
new_column
571+
exp act
572+
0 0.0 7.0
573+
574+
----------------------------------------------------------------------
575+
Ran 1 test in 0.020s
576+
577+
FAILED (failures=1)
578+
```
579+
580+
468581
## **Debugging Workflow**
469582

470583
You'll use these commands ad hoc to validate your changes are behaving as expected. Audits (data tests) are a great first step, and you'll want to evolve into to feel confident about the changes. The workflow is as follows:
@@ -558,17 +671,7 @@ asdf
558671

559672
asdf
560673

561-
=== "SQLMesh"
562-
563-
```bash
564-
sqlmesh create_test demo.stg_payments --query demo.seed_raw_payments "select * from demo.seed_raw_payments limit 5"
565-
```
566674

567-
=== "Tobiko Cloud"
568-
569-
```bash
570-
tcloud sqlmesh create_test demo.stg_payments --query demo.seed_raw_payments "select * from demo.seed_raw_payments limit 5"
571-
```
572675

573676
asdf
574677

0 commit comments

Comments
 (0)