You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/tests.md
+40-41Lines changed: 40 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ Tests within a suite file contain the following attributes:
14
14
15
15
* The unique name of a test
16
16
* The name of the model targeted by this test
17
+
*[Optional] The test's description
17
18
* Test inputs, which are defined per upstream model or external table referenced by the target model. Each test input consists of the following:
18
19
* The name of an upstream model or external table
19
20
* The list of rows defined as a mapping from a column name to a value associated with it
@@ -28,6 +29,7 @@ The YAML format is defined as follows:
28
29
```yaml linenums="1"
29
30
<unique_test_name>:
30
31
model: <target_model_name>
32
+
description: <description> # Optional
31
33
inputs:
32
34
<upstream_model_or_external_table_name>:
33
35
rows:
@@ -49,7 +51,7 @@ The YAML format is defined as follows:
49
51
50
52
The `rows` key is optional in the above format, so the following would also be valid:
51
53
52
-
```
54
+
```yaml linenums="1"
53
55
<unique_test_name>:
54
56
model: <target_model_name>
55
57
inputs:
@@ -97,7 +99,6 @@ SELECT
97
99
FROM
98
100
sqlmesh_example.incremental_model
99
101
GROUP BY item_id
100
-
ORDER BY item_id
101
102
```
102
103
103
104
Notice how the query of the model definition above references one upstream model: `sqlmesh_example.incremental_model`.
@@ -109,16 +110,16 @@ test_example_full_model:
109
110
model: sqlmesh_example.full_model
110
111
inputs:
111
112
sqlmesh_example.incremental_model:
112
-
rows:
113
-
- id: 1
114
-
item_id: 1
115
-
ds: '2020-01-01'
116
-
- id: 2
117
-
item_id: 1
118
-
ds: '2020-01-02'
119
-
- id: 3
120
-
item_id: 2
121
-
ds: '2020-01-03'
113
+
rows:
114
+
- id: 1
115
+
item_id: 1
116
+
event_date: '2020-01-01'
117
+
- id: 2
118
+
item_id: 1
119
+
event_date: '2020-01-02'
120
+
- id: 3
121
+
item_id: 2
122
+
event_date: '2020-01-03'
122
123
outputs:
123
124
query:
124
125
rows:
@@ -128,7 +129,7 @@ test_example_full_model:
128
129
num_orders: 1
129
130
```
130
131
131
-
The `ds` column is not needed in the above test, since it is not referenced in `full_model`, so it may be omitted.
132
+
The `event_date` column is not needed in the above test, since it is not referenced in `full_model`, so it may be omitted.
132
133
133
134
If we were only interested in testing the `num_orders` column, we could only specify input values for the `id` column of `sqlmesh_example.incremental_model`, thus rewriting the above test more compactly as follows:
134
135
@@ -165,11 +166,10 @@ WITH filtered_orders_cte AS (
165
166
)
166
167
SELECT
167
168
item_id,
168
-
COUNT(distinct id) AS num_orders,
169
+
COUNT(DISTINCT id) AS num_orders,
169
170
FROM
170
171
filtered_orders_cte
171
172
GROUP BY item_id
172
-
ORDER BY item_id
173
173
```
174
174
175
175
Below is the example of a test that verifies individual rows returned by the `filtered_orders_cte` CTE before aggregation takes place:
@@ -182,13 +182,13 @@ test_example_full_model:
182
182
rows:
183
183
- id: 1
184
184
item_id: 1
185
-
ds: '2020-01-01'
185
+
event_date: '2020-01-01'
186
186
- id: 2
187
187
item_id: 1
188
-
ds: '2020-01-02'
188
+
event_date: '2020-01-02'
189
189
- id: 3
190
190
item_id: 2
191
-
ds: '2020-01-03'
191
+
event_date: '2020-01-03'
192
192
outputs:
193
193
ctes:
194
194
filtered_orders_cte:
@@ -217,22 +217,21 @@ In this example, we'll show how to generate a test for `sqlmesh_example.incremen
217
217
MODEL (
218
218
name sqlmesh_example.incremental_model,
219
219
kind INCREMENTAL_BY_TIME_RANGE (
220
-
time_column ds
220
+
time_column event_date
221
221
),
222
222
start '2020-01-01',
223
223
cron '@daily',
224
-
grain (id, ds)
224
+
grain (id, event_date)
225
225
);
226
226
227
227
SELECT
228
228
id,
229
229
item_id,
230
-
ds,
230
+
event_date,
231
231
FROM
232
232
sqlmesh_example.seed_model
233
233
WHERE
234
-
ds between @start_ds and @end_ds
235
-
234
+
event_date BETWEEN @start_date AND @end_date
236
235
```
237
236
238
237
Firstly, we need to specify the input data for the upstream model `sqlmesh_example.seed_model`. The `create_test` command starts by executing a user-supplied query against the project's data warehouse and uses the returned data to produce the test's input rows.
@@ -243,22 +242,22 @@ For instance, the following query will return three rows from the table correspo
243
242
SELECT * FROM sqlmesh_example.seed_model LIMIT 3
244
243
```
245
244
246
-
Next, notice that `sqlmesh_example.incremental_model` contains a filter which references the `@start_ds` and `@end_ds` [macro variables](macros/macro_variables.md).
245
+
Next, notice that `sqlmesh_example.incremental_model` contains a filter which references the `@start_date` and `@end_date` [macro variables](macros/macro_variables.md).
247
246
248
-
To make the generated test deterministic and thus ensure that it will always succeed, we need to define these variables and modify the above query to constrain `ds` accordingly.
247
+
To make the generated test deterministic and thus ensure that it will always succeed, we need to define these variables and modify the above query to constrain `event_date` accordingly.
249
248
250
-
If we set `@start_ds` to `'2020-01-01'` and `@end_ds` to `'2020-01-04'`, the above query needs to be changed to:
249
+
If we set `@start_date` to `'2020-01-01'` and `@end_date` to `'2020-01-04'`, the above query needs to be changed to:
251
250
252
251
```sql linenums="1"
253
-
SELECT * FROM sqlmesh_example.seed_model WHERE ds BETWEEN '2020-01-01' AND '2020-01-04' LIMIT 3
252
+
SELECT * FROM sqlmesh_example.seed_model WHERE event_date BETWEEN '2020-01-01' AND '2020-01-04' LIMIT 3
254
253
```
255
254
256
255
Finally, combining this query with the proper macro variable definitions, we can compute the expected output for the model's query in order to generate the complete test.
257
256
258
257
This can be achieved using the following command:
259
258
260
-
```bash
261
-
$ sqlmesh create_test sqlmesh_example.incremental_model --query sqlmesh_example.seed_model "select * from sqlmesh_example.seed_model where ds between '2020-01-01' and '2020-01-04' limit 3" --var start '2020-01-01' --var end '2020-01-04'
259
+
```
260
+
$ sqlmesh create_test sqlmesh_example.incremental_model --query sqlmesh_example.seed_model "SELECT * FROM sqlmesh_example.seed_model WHERE event_date BETWEEN '2020-01-01' AND '2020-01-04' LIMIT 3" --var start '2020-01-01' --var end '2020-01-04'
262
261
```
263
262
264
263
Running this creates the following new test, located at `tests/test_incremental_model.yaml`:
@@ -270,32 +269,32 @@ test_incremental_model:
270
269
sqlmesh_example.seed_model:
271
270
- id: 1
272
271
item_id: 2
273
-
ds: '2020-01-01'
272
+
event_date: 2020-01-01
274
273
- id: 2
275
274
item_id: 1
276
-
ds: '2020-01-01'
275
+
event_date: 2020-01-01
277
276
- id: 3
278
277
item_id: 3
279
-
ds: '2020-01-03'
278
+
event_date: 2020-01-03
280
279
outputs:
281
280
query:
282
281
- id: 1
283
282
item_id: 2
284
-
ds: '2020-01-01'
283
+
event_date: 2020-01-01
285
284
- id: 2
286
285
item_id: 1
287
-
ds: '2020-01-01'
286
+
event_date: 2020-01-01
288
287
- id: 3
289
288
item_id: 3
290
-
ds: '2020-01-03'
289
+
event_date: 2020-01-03
291
290
vars:
292
291
start: '2020-01-01'
293
292
end: '2020-01-04'
294
293
```
295
294
296
295
As shown below, we now have two passing tests. Hooray!
0 commit comments