Skip to content

Commit 607a61c

Browse files
Add amount_major to views (#83)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e0c0c0c commit 607a61c

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The relations are defined in [create-relations.sql](sqlite_export_for_ynab/ddl/c
6161

6262
1. Some objects are pulled out into their own tables so they can be more cleanly modeled in SQLite (ex: subtransactions, loan account periodic values).
6363
1. Foreign keys are added as needed (ex: budget ID, transaction ID) so data across budgets remains separate.
64-
1. Two new views called `flat_transactions` and `scheduled_flat_transactions` allow you to query split and non-split transactions easily, without needing to also query `subtransactions` and `scheduled_subtransactions` respectively.
64+
1. Two new views called `flat_transactions` and `scheduled_flat_transactions` allow you to query split and non-split transactions easily, without needing to also query `subtransactions` and `scheduled_subtransactions` respectively. They also include fields to improve quality of life (ex: `amount_major` to convert from [YNAB's milliunits](https://api.ynab.com/#formats) to [major units](https://en.wikipedia.org/wiki/ISO_4217) i.e. dollars).
6565

6666
## Querying
6767

sqlite_export_for_ynab/ddl/create-relations.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ SELECT
160160
, t.matched_transaction_id
161161
, COALESCE(st.id, t.id) AS id
162162
, COALESCE(st.amount, t.amount) AS amount
163+
, COALESCE(st.amount, t.amount) / -1000.0 AS amount_major
163164
, CASE
164165
WHEN
165166
COALESCE(st.transfer_account_id, t.transfer_account_id) IS null
@@ -249,6 +250,7 @@ SELECT
249250
, COALESCE(st.payee_name, t.payee_name) AS payee_name
250251
, COALESCE(st.id, t.id) AS id
251252
, COALESCE(st.amount, t.amount) AS amount
253+
, COALESCE(st.amount, t.amount) / -1000.0 AS amount_major
252254
, CASE
253255
WHEN
254256
COALESCE(st.transfer_account_id, t.transfer_account_id) IS null

testing/fixtures.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,12 @@
136136
{
137137
"id": SUBTRANSACTION_ID_1,
138138
"transaction_id": TRANSACTION_ID_1,
139-
"amount": -7000,
139+
"amount": -7500,
140140
},
141141
{
142142
"id": SUBTRANSACTION_ID_2,
143143
"transaction_id": TRANSACTION_ID_1,
144-
"amount": -3000,
144+
"amount": -2500,
145145
},
146146
],
147147
},
@@ -167,12 +167,12 @@
167167
{
168168
"id": SCHEDULED_SUBTRANSACTION_ID_1,
169169
"scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1,
170-
"amount": -8000,
170+
"amount": -8040,
171171
},
172172
{
173173
"id": SCHEDULED_SUBTRANSACTION_ID_2,
174174
"scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1,
175-
"amount": -4000,
175+
"amount": -2960,
176176
},
177177
],
178178
},

tests/_main_test.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ def test_insert_transactions(cur):
257257
"id": SUBTRANSACTION_ID_1,
258258
"transaction_id": TRANSACTION_ID_1,
259259
"budget_id": BUDGET_ID_1,
260-
"amount": -7000,
260+
"amount": -7500,
261261
},
262262
{
263263
"id": SUBTRANSACTION_ID_2,
264264
"transaction_id": TRANSACTION_ID_1,
265265
"budget_id": BUDGET_ID_1,
266-
"amount": -3000,
266+
"amount": -2500,
267267
},
268268
]
269269

@@ -275,22 +275,25 @@ def test_insert_transactions(cur):
275275
"date": "2024-02-01",
276276
"id": TRANSACTION_ID_2,
277277
"amount": -15000,
278+
"amount_major": pytest.approx(15),
278279
},
279280
{
280281
"transaction_id": TRANSACTION_ID_1,
281282
"subtransaction_id": SUBTRANSACTION_ID_1,
282283
"budget_id": BUDGET_ID_1,
283284
"date": "2024-01-01",
284285
"id": SUBTRANSACTION_ID_1,
285-
"amount": -7000,
286+
"amount": -7500,
287+
"amount_major": pytest.approx(7.50),
286288
},
287289
{
288290
"transaction_id": TRANSACTION_ID_1,
289291
"subtransaction_id": SUBTRANSACTION_ID_2,
290292
"budget_id": BUDGET_ID_1,
291293
"date": "2024-01-01",
292294
"id": SUBTRANSACTION_ID_2,
293-
"amount": -3000,
295+
"amount": -2500,
296+
"amount_major": pytest.approx(2.50),
294297
},
295298
]
296299

@@ -322,13 +325,13 @@ def test_insert_scheduled_transactions(cur):
322325
"id": SCHEDULED_SUBTRANSACTION_ID_1,
323326
"scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1,
324327
"budget_id": BUDGET_ID_1,
325-
"amount": -8000,
328+
"amount": -8040,
326329
},
327330
{
328331
"id": SCHEDULED_SUBTRANSACTION_ID_2,
329332
"scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1,
330333
"budget_id": BUDGET_ID_1,
331-
"amount": -4000,
334+
"amount": -2960,
332335
},
333336
]
334337

@@ -339,20 +342,23 @@ def test_insert_scheduled_transactions(cur):
339342
"budget_id": BUDGET_ID_1,
340343
"id": SCHEDULED_TRANSACTION_ID_2,
341344
"amount": -11000,
345+
"amount_major": pytest.approx(11),
342346
},
343347
{
344348
"transaction_id": SCHEDULED_TRANSACTION_ID_1,
345349
"subtransaction_id": SCHEDULED_SUBTRANSACTION_ID_1,
346350
"budget_id": BUDGET_ID_1,
347351
"id": SCHEDULED_SUBTRANSACTION_ID_1,
348-
"amount": -8000,
352+
"amount": -8040,
353+
"amount_major": pytest.approx(8.04),
349354
},
350355
{
351356
"transaction_id": SCHEDULED_TRANSACTION_ID_1,
352357
"subtransaction_id": SCHEDULED_SUBTRANSACTION_ID_2,
353358
"budget_id": BUDGET_ID_1,
354359
"id": SCHEDULED_SUBTRANSACTION_ID_2,
355-
"amount": -4000,
360+
"amount": -2960,
361+
"amount_major": pytest.approx(2.96),
356362
},
357363
]
358364

0 commit comments

Comments
 (0)