Skip to content

Commit aff5d5b

Browse files
committed
docs: show a real model macro and inline mocks in the README example
1 parent bcacca8 commit aff5d5b

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

‎README.md‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,21 @@ MODEL (
156156
SELECT
157157
o.order_id,
158158
o.customer_id,
159-
p.amount_cents AS total_cents
159+
p.amount_cents AS total_cents,
160+
@cents_to_dollars("p.amount_cents") AS total_dollars
160161
FROM __ref("stg_orders") o
161162
JOIN __ref("stg_payments") p USING (order_id)
162163
```
163164

165+
Macros are plain Python functions that return SQL, kept next to the models that use them:
166+
167+
```python
168+
# models/marts/_sqlbuild/_macros/currency.py
169+
def cents_to_dollars(column: str) -> str:
170+
"""Convert a cents integer column to a dollars decimal with two decimal places."""
171+
return f"ROUND(({column}) / 100.0, 2)"
172+
```
173+
164174
A test mocks the sources and asserts on the model, resolving every model in between from its real
165175
SQL:
166176

@@ -169,17 +179,25 @@ TEST();
169179

170180
WITH
171181
__source__raw__orders AS (
172-
@mock_orders()
182+
SELECT
183+
1 AS id,
184+
100 AS customer_id,
185+
2 AS waffle_type_id,
186+
3 AS quantity,
187+
CAST('2026-04-01 10:00:00' AS TIMESTAMP) AS ordered_at,
188+
'completed' AS status
173189
),
174190
__source__raw__payments AS (
175191
SELECT
176-
1 AS payment_id,
192+
10 AS id,
177193
1 AS order_id,
178194
1500 AS amount_cents,
179-
'credit_card' AS method
195+
'credit_card' AS payment_method,
196+
CAST('2026-04-01 10:05:00' AS TIMESTAMP) AS paid_at,
197+
'success' AS status
180198
),
181199
__expected__fact_orders AS (
182-
SELECT 1 AS order_id, 100 AS customer_id, 1500 AS total_cents
200+
SELECT 1 AS order_id, 100 AS customer_id, 1500 AS total_cents, 15.00 AS total_dollars
183201
)
184202
SELECT 1
185203
```

0 commit comments

Comments
 (0)