Skip to content

Commit 4e5aea6

Browse files
Incorporating comments, adding outputs, editing. (#438)
1 parent b9b6ad8 commit 4e5aea6

4 files changed

Lines changed: 123 additions & 109 deletions

File tree

docs/concepts/models/overview.md

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,7 @@ MODEL (
9595
```
9696

9797
## Macros
98-
Macros can be used for passing in paramaterized arguments such as dates, as well as for making SQL less repetitive. By default, SQLMesh provides several predefined macro variables that can be used. Macros are used by prefixing with the `@` symbol.
99-
100-
- @start_date
101-
```sql
102-
-- The inclusive start interval of an execution casted to a DATETIME SQL object.
103-
@start_date = CAST("2020-01-01 00:00:00.0" AS DATETIME)
104-
```
105-
- @end_date
106-
```sql
107-
-- The inclusive end interval of an execution casted to a DATETIME SQL object.
108-
@end_date = CAST("2020-01-01 23:59:59.999000" AS DATETIME)
109-
```
110-
- @latest_date
111-
```sql
112-
-- The latest datetime or current run date of an execution. Used when you only care about the latest data.
113-
@latest_date = CAST("2020-01-01 00:00:00.0" AS DATETIME)
114-
```
115-
- @start_ds
116-
```sql
117-
-- The inclusive start date string.
118-
@start_ds = '2020-01-01'
119-
```
120-
- @end_ds
121-
```sql
122-
-- The inclusive end date string.
123-
@end_ds = '2020-01-01'
124-
```
125-
- @latest_ds
126-
```sql
127-
-- The date string of the run date.
128-
@end_ds = '2020-01-01'
129-
```
98+
Macros can be used for passing in paramaterized arguments such as dates, as well as for making SQL less repetitive. By default, SQLMesh provides several predefined macro variables that can be used. Macros are used by prefixing with the `@` symbol. For more information, refer to [macros](../../concepts/macros.md).
13099

131100
## Statements
132101
Models can have additional statements that run before the main query. This can be useful for loading things such as [UDFs](https://en.wikipedia.org/wiki/User-defined_function). In general, statements should only be used for preparing the main query. They should not be used for creating or altering tables, as this could lead to unpredictable behavior.
@@ -144,30 +113,7 @@ FROM y
144113
```
145114

146115
## Time column
147-
Models that are loaded incrementally require a time column to partition data. A time column is a column in a model with an optional format string in the dialect of the model; for example, `'%Y-%m-%d'` for DuckDB or `'yyyy-mm-dd'` for Snowflake.
148-
149-
```sql linenums="1"
150-
-- Orders are partitioned by the ds column
151-
MODEL (
152-
name sushi.orders,
153-
dialect duckdb,
154-
kind INCREMENTAL_BY_TIME_RANGE (
155-
time_column (ds, '%Y-%m-%d')
156-
)
157-
);
158-
159-
SELECT
160-
id::INT AS id, -- Primary key
161-
customer_id::INT AS customer_id, -- Id of customer who made the order
162-
waiter_id::INT AS waiter_id, -- Id of waiter who took the order
163-
start_ts::TEXT AS start_ts, -- Start timestamp
164-
end_ts::TEXT AS end_ts, -- End timestamp
165-
ds::TEXT AS ds -- Date of order
166-
FROM raw.orders
167-
WHERE
168-
ds BETWEEN @start_ds AND @end_ds
169-
```
170-
When SQLMesh incrementally inserts data for a partition, it will overwrite any existing data in that partition. For engines that support partitions, it will use an `INSERT OVERWRITE` query. For engines that do not, it will first delete the data in the partition before inserting.
116+
Models that are loaded incrementally require a time column to partition data. A time column is a column in a model with an optional format string in the dialect of the model; for example, `'%Y-%m-%d'` for DuckDB or `'yyyy-mm-dd'` for Snowflake. For more information, refer to [time column](../../concepts/models/model_kinds.md#time-column).
171117

172118
### Format string configuration
173119
The format string tells SQLMesh how your dates are formatted in order to compare start and end dates correctly. You can configure a project-wide default format in your project configuration. A time column format string declared in a model will override the project-wide default. If the model uses a different dialect than the rest of your project, the format string will be automatically transpiled to the model dialect with SQLGlot. SQLMesh will use

docs/concepts/tests.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
# Testing
2-
Testing is how you can protect your project from regression by continuously verifying that the output of each model matches the expectations. Unlike [audits](audits.md), tests are executed either on demand (eg. as part of a CI / CD job) or every time a new [plan](plans.md) is created.
32

4-
Similarly to unit testing in software development, SQLMesh evaluates the model's logic against predefined inputs and compares the output to expected outcomes provided as part of each test.
3+
Testing allows you to protect your project from regression by continuously verifying the output of each model matches your expectations. Unlike [audits](audits.md), tests are executed either on demand (for example, as part of a CI/CD job) or every time a new [plan](plans.md) is created.
54

6-
A comprehensive suite of tests can empower data practitioners to work with confidence, since it allows them to make sure that models behave as expected after changes have been applied to them.
5+
Similar to unit testing in software development, SQLMesh evaluates the model's logic against predefined inputs and then compares the output to expected outcomes provided as part of each test.
6+
7+
A comprehensive suite of tests can empower data practitioners to work with confidence, as it allows them to ensure models behave as expected after changes have been applied to them.
78

89
## Creating tests
9-
Test suites are defined using YAML format in files with the `.yaml` extension as part of the `tests/` folder of your SQLMesh project. Each test within a suite file consists of the following attributes:
10+
11+
Test suites are defined using YAML format within `.yaml` files in the `tests/` folder of your SQLMesh project. Each test within a suite file contains the following attributes:
1012

1113
* The unique name of a test
12-
* The name of the model that is targeted by this test
13-
* Test inputs. Inputs are defined per external table or upstream model referenced by the target model. Each test input consists of the following:
14+
* The name of the model targeted by this test
15+
* Test inputs, which are defined per external table or upstream model referenced by the target model. Each test input consists of the following:
1416
* The name of an upstream model or external table
1517
* The list of rows defined as a mapping from a column name to a value associated with it
16-
* Expected outputs which are defined as follows:
18+
* Expected outputs, which are defined as follows:
1719
* The list of rows that are expected to be returned by the model's query defined as a mapping from a column name to a value associated with it
18-
* [Optional] The list of expected rows per each individual [Common Table Expression](glossary.md#cte) (CTE) defined in the model's query.
19-
* [Optional] The dictionary of values for macro variables that will be set during model testing.
20+
* [Optional] The list of expected rows per each individual [Common Table Expression](glossary.md#cte) (CTE) defined in the model's query
21+
* [Optional] The dictionary of values for macro variables that will be set during model testing
2022

2123
The YAML format is defined as follows:
24+
2225
```yaml linenums="1"
2326
<unique_test_name>:
2427
model: <target_model_name>
@@ -38,9 +41,10 @@ The YAML format is defined as follows:
3841
<macro_variable_name>: <macro_variable_value>
3942
```
4043
41-
## Example
44+
### Example
45+
46+
In this example, we'll use the `sqlmesh_example.example_full_model` model, which is provided as part of the `sqlmesh init` command and defined as follows:
4247

43-
In this example we'll use the `sqlmesh_example.example_full_model` model which is provided as part of the `sqlmesh init` command and is defined as follows:
4448
```sql linenums="1"
4549
MODEL (
4650
name sqlmesh_example.example_full_model,
@@ -56,9 +60,10 @@ FROM
5660
GROUP BY item_id
5761
```
5862

59-
Notice how the query of the model definition above references one upstream model - `sqlmesh_example.example_incremental_model`.
63+
Notice how the query of the model definition above references one upstream model: `sqlmesh_example.example_incremental_model`.
6064

6165
The test definition for this model may look like following:
66+
6267
```yaml linenums="1"
6368
test_example_full_model:
6469
model: sqlmesh_example.example_full_model
@@ -85,7 +90,8 @@ test_example_full_model:
8590

8691
### Testing CTEs
8792

88-
As mentioned previously, individual CTEs within the model's query can also be tested. In this example let's slightly modify the query of the model used in the previous example:
93+
Individual CTEs within the model's query can also be tested. Let's slightly modify the query of the model used in the previous example:
94+
8995
```sql linenums="1"
9096
WITH filtered_orders_cte AS (
9197
SELECT
@@ -104,7 +110,8 @@ FROM
104110
GROUP BY item_id
105111
```
106112

107-
Below is the example of a test which verifies individual rows returned by the `filtered_orders_cte` CTE before aggregation takes place:
113+
Below is the example of a test that verifies individual rows returned by the `filtered_orders_cte` CTE before aggregation takes place:
114+
108115
```yaml linenums="1" hl_lines="16-22"
109116
test_example_full_model:
110117
model: sqlmesh_example.example_full_model
@@ -135,10 +142,14 @@ test_example_full_model:
135142
```
136143

137144
## Running tests
138-
Tests run automatically every time a new [plan](plans.md) is created. Additionally tests can be run on demand using the `sqlmesh test` command.
139145

140-
### The CLI test command
141-
You can execute tests using the `sqlmesh test` command as follows:
146+
### Automatic testing with plan
147+
148+
Tests run automatically every time a new [plan](plans.md) is created.
149+
150+
### Manual testing with the CLI
151+
152+
You can execute tests on demand using the `sqlmesh test` command as follows:
142153
```bash
143154
$ sqlmesh test
144155
.
@@ -148,7 +159,8 @@ Ran 1 test in 0.005s
148159
OK
149160
```
150161

151-
The command returns a non-zero exit code if on any failures and reports them in the standard error stream:
162+
The command returns a non-zero exit code if there are any failures, and reports them in the standard error stream:
163+
152164
```bash
153165
$ sqlmesh test
154166
F
@@ -169,12 +181,15 @@ Ran 1 test in 0.008s
169181
FAILED (failures=1)
170182
```
171183

184+
### Testing for specific models
172185
To run a specific model test, pass in the suite file name followed by `::` and the name of the test:
186+
173187
```
174188
sqlmesh test tests/test_suite.yaml::test_example_full_model
175189
```
176190

177191
You can also run tests that match a pattern or substring using a glob pathname expansion syntax:
192+
178193
```
179194
sqlmesh test tests/test_*
180195
```

docs/guides/testing.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,46 @@
11
# Testing guide
2-
32
## Testing changes to models
4-
53
To run unit tests for your models, run the `sqlmesh test` command as follows:
64

75
```bash
86
$ sqlmesh test
97
.
108
----------------------------------------------------------------------
11-
Ran 1 test in 0.052s
9+
Ran 1 test in 0.042s
1210

1311
OK
1412
```
15-
1613
As the unit tests run, SQLMesh will identify any that fail.
1714

1815
For more information about tests, refer to [testing](../concepts/tests.md).
1916

2017
### Test changes to a specific model
2118

22-
To run a specific model test, pass in the module followed by `::` and the name of the test.
23-
24-
For example: `example_full_model::test_example_full_model.`
19+
To run a specific model test, pass in the suite file name followed by `::` and the name of the test; for example: `sqlmesh test tests/test_suite.yaml::test_example_full_model`.
2520

2621
### Run a subset of tests
2722

28-
To run a test that matches a pattern or substring, use the following syntax: `project.tests.test_order*`.
23+
To run a test that matches a pattern or substring, use the following syntax: `sqlmesh test tests/test_example*`.
2924

30-
For example, this will match the following tests: `project.tests.test_orders`, `project.tests.test_order_items`.
25+
Running the above command will run our `test_example_full_model` test that we ran earlier using `sqlmesh test`:
3126

32-
## Auditing changes to models
27+
```
28+
$ sqlmesh test tests/test_example*
29+
.
30+
----------------------------------------------------------------------
31+
Ran 1 test in 0.042s
3332
33+
OK
34+
```
35+
36+
As another example, running the `sqlmesh test tests/test_order*` command would run the following tests:
37+
38+
* `test_orders`
39+
* `test_orders_takeout`
40+
* `test_order_items`
41+
* `test_order_type`
42+
43+
## Auditing changes to models
3444
To audit your models, run the `sqlmesh audit` command as follows:
3545

3646
```bash
@@ -41,7 +51,6 @@ assert_positive_order_ids PASS.
4151
Finished with 0 audit error(s).
4252
Done.
4353
```
44-
4554
**Note:** Ensure that you have already planned and applied your changes before running an audit.
4655

4756
By default, SQLMesh will halt the pipeline when an audit fails in order to prevent potentially invalid data from propagating further downstream. This behavior can be changed for individual audits by defining them as [non-blocking](../concepts/audits.md#non-blocking-audits).

0 commit comments

Comments
 (0)