sqlx-ledger 0.11.14

An embeddable double sided accounting ledger built on PG/SQLx
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
mod helpers;

use rust_decimal::Decimal;

use rand::distributions::{Alphanumeric, DistString};
use sqlx_ledger::{account::*, balance::AccountBalance, event::*, journal::*, tx_template::*, *};

/// Check if an event belongs to our test, identified by external_id and account IDs.
fn is_our_event(
    event: &SqlxLedgerEvent,
    external_id: &str,
    sender_account_id: AccountId,
    recipient_account_id: AccountId,
) -> bool {
    match &event.data {
        SqlxLedgerEventData::TransactionCreated(tx) => tx.external_id == external_id,
        SqlxLedgerEventData::BalanceUpdated(b) => {
            b.account_id == sender_account_id || b.account_id == recipient_account_id
        }
        _ => false,
    }
}

/// Collect events matching this test's transaction from a broadcast receiver.
/// Returns when `target` events are collected or the deadline expires.
async fn collect_our_events(
    rx: &mut tokio::sync::broadcast::Receiver<SqlxLedgerEvent>,
    external_id: &str,
    sender_account_id: AccountId,
    recipient_account_id: AccountId,
    target: usize,
    deadline: tokio::time::Instant,
) -> Vec<SqlxLedgerEvent> {
    let per_event_timeout = tokio::time::Duration::from_millis(500);
    let mut collected = Vec::new();
    while tokio::time::Instant::now() < deadline && collected.len() < target {
        match tokio::time::timeout(per_event_timeout, rx.recv()).await {
            Ok(Ok(event)) => {
                if is_our_event(&event, external_id, sender_account_id, recipient_account_id) {
                    collected.push(event);
                }
            }
            Ok(Err(tokio::sync::broadcast::error::RecvError::Lagged(_))) => continue,
            Ok(Err(e)) => panic!("Unexpected recv error: {e}"),
            Err(_) => {}
        }
    }
    collected
}

#[tokio::test]
async fn post_transaction() -> anyhow::Result<()> {
    let pool = helpers::init_pool().await?;

    let tx_code = Alphanumeric.sample_string(&mut rand::thread_rng(), 32);

    let name = Alphanumeric.sample_string(&mut rand::thread_rng(), 32);
    let new_journal = NewJournal::builder().name(name).build().unwrap();
    let ledger = SqlxLedger::new(&pool);

    let journal_id = ledger.journals().create(new_journal).await.unwrap();
    let code = Alphanumeric.sample_string(&mut rand::thread_rng(), 32);
    let new_account = NewAccount::builder()
        .id(uuid::Uuid::new_v4())
        .name(format!("Test Sender Account {code}"))
        .code(code)
        .build()
        .unwrap();
    let sender_account_id = ledger.accounts().create(new_account).await.unwrap();
    let code = Alphanumeric.sample_string(&mut rand::thread_rng(), 32);
    let new_account = NewAccount::builder()
        .id(uuid::Uuid::new_v4())
        .name(format!("Test Recipient Account {code}"))
        .code(code)
        .build()
        .unwrap();
    let recipient_account_id = ledger.accounts().create(new_account).await.unwrap();

    let params = vec![
        ParamDefinition::builder()
            .name("recipient")
            .r#type(ParamDataType::UUID)
            .build()
            .unwrap(),
        ParamDefinition::builder()
            .name("sender")
            .r#type(ParamDataType::UUID)
            .build()
            .unwrap(),
        ParamDefinition::builder()
            .name("journal_id")
            .r#type(ParamDataType::UUID)
            .build()
            .unwrap(),
        ParamDefinition::builder()
            .name("external_id")
            .r#type(ParamDataType::STRING)
            .build()
            .unwrap(),
        ParamDefinition::builder()
            .name("effective")
            .r#type(ParamDataType::DATE)
            .default_expr("date()")
            .build()
            .unwrap(),
    ];
    let entries = vec![
        EntryInput::builder()
            .entry_type("'TEST_BTC_DR'")
            .account_id("params.sender")
            .layer("SETTLED")
            .direction("DEBIT")
            .units("decimal('1290')")
            .currency("'BTC'")
            .build()
            .unwrap(),
        EntryInput::builder()
            .entry_type("'TEST_BTC_CR'")
            .account_id("params.recipient")
            .layer("SETTLED")
            .direction("CREDIT")
            .units("decimal('1290')")
            .currency("'BTC'")
            .build()
            .unwrap(),
        EntryInput::builder()
            .entry_type("'TEST_USD_DR'")
            .account_id("params.sender")
            .layer("SETTLED")
            .direction("DEBIT")
            .units("decimal('100')")
            .currency("'USD'")
            .build()
            .unwrap(),
        EntryInput::builder()
            .entry_type("'TEST_USD_CR'")
            .account_id("params.recipient")
            .layer("SETTLED")
            .direction("CREDIT")
            .units("decimal('100')")
            .currency("'USD'")
            .build()
            .unwrap(),
    ];
    let new_template = NewTxTemplate::builder()
        .id(uuid::Uuid::new_v4())
        .code(&tx_code)
        .params(params)
        .tx_input(
            TxInput::builder()
                .effective("params.effective")
                .journal_id("params.journal_id")
                .external_id("params.external_id")
                .metadata(r#"{"foo": "bar"}"#)
                .build()
                .unwrap(),
        )
        .entries(entries)
        .build()
        .unwrap();
    ledger.tx_templates().create(new_template).await.unwrap();

    let baseline_id: i64 =
        sqlx::query_scalar::<_, i64>("SELECT COALESCE(MAX(id), 0) FROM sqlx_ledger_events")
            .fetch_one(&pool)
            .await?;

    let external_id = uuid::Uuid::new_v4().to_string();
    let mut params = TxParams::new();
    params.insert("journal_id", journal_id);
    params.insert("sender", sender_account_id);
    params.insert("recipient", recipient_account_id);
    params.insert("external_id", external_id.clone());

    ledger
        .post_transaction(TransactionId::new(), &tx_code, Some(params))
        .await
        .unwrap();
    let transactions = ledger
        .transactions()
        .list_by_external_ids(vec![external_id.clone()])
        .await?;
    assert_eq!(transactions.len(), 1);

    let entries = ledger
        .entries()
        .list_by_transaction_ids(vec![transactions[0].id])
        .await?;
    assert!(entries.get(&transactions[0].id).is_some());
    assert_eq!(entries.get(&transactions[0].id).unwrap().len(), 4);

    // Subscribe after posting so the after_id catch-up reliably fetches our events
    // from the DB, avoiding broadcast lag from concurrent tests.
    let events = ledger
        .events(EventSubscriberOpts {
            after_id: Some(SqlxLedgerEventId::from(baseline_id)),
            buffer: 10000,
            ..Default::default()
        })
        .await?;
    let mut sender_account_balance_events = events
        .account_balance(journal_id, sender_account_id)
        .await
        .expect("event subscriber closed");
    let mut journal_events = events
        .journal(journal_id)
        .await
        .expect("event subscriber closed");

    let deadline = tokio::time::Instant::now() + tokio::time::Duration::from_secs(5);

    // Collect events from journal and account-balance channels concurrently
    let ext_id = external_id.clone();
    let journal_handle = tokio::spawn(async move {
        collect_our_events(
            &mut journal_events,
            &ext_id,
            sender_account_id,
            recipient_account_id,
            5,
            deadline,
        )
        .await
    });
    let ext_id = external_id.clone();
    let balance_handle = tokio::spawn(async move {
        collect_our_events(
            &mut sender_account_balance_events,
            &ext_id,
            sender_account_id,
            recipient_account_id,
            2,
            deadline,
        )
        .await
    });

    let journal_collected = journal_handle.await?;
    let sender_balance_collected = balance_handle.await?;

    // Verify sender account balance channel only receives BalanceUpdated events
    assert!(
        !sender_balance_collected.is_empty(),
        "Sender account balance channel should receive events"
    );
    assert!(
        sender_balance_collected
            .iter()
            .all(|e| e.r#type == SqlxLedgerEventType::BalanceUpdated),
        "Sender account balance channel should only contain BalanceUpdated events"
    );

    // Verify journal channel receives both TransactionCreated and BalanceUpdated
    let journal_tx_count = journal_collected
        .iter()
        .filter(|e| e.r#type == SqlxLedgerEventType::TransactionCreated)
        .count();
    let journal_balance_count = journal_collected
        .iter()
        .filter(|e| e.r#type == SqlxLedgerEventType::BalanceUpdated)
        .count();
    assert_eq!(
        journal_tx_count, 1,
        "Journal channel should have 1 TransactionCreated"
    );
    assert_eq!(
        journal_balance_count, 4,
        "Journal channel should have exactly 4 BalanceUpdated (2 accounts × 2 currencies), got {journal_balance_count}"
    );

    // Verify events are in strictly increasing ID order
    for window in journal_collected.windows(2) {
        assert!(window[0].id < window[1].id, "journal_events out of order");
    }

    // Find the TransactionCreated event for after_id verification
    let tx_event = journal_collected
        .iter()
        .find(|e| e.r#type == SqlxLedgerEventType::TransactionCreated)
        .expect("should have TransactionCreated event");

    // Verify after_id subscription receives events after the TransactionCreated
    let after_events = ledger
        .events(EventSubscriberOpts {
            after_id: Some(tx_event.id),
            buffer: 10000,
            ..Default::default()
        })
        .await?;
    let mut after_all = after_events.all().unwrap();
    let after_deadline = tokio::time::Instant::now() + tokio::time::Duration::from_secs(5);
    let after_collected = collect_our_events(
        &mut after_all,
        &external_id,
        sender_account_id,
        recipient_account_id,
        4,
        after_deadline,
    )
    .await;
    let after_balance_count = after_collected
        .iter()
        .filter(|e| e.r#type == SqlxLedgerEventType::BalanceUpdated)
        .count();
    assert_eq!(
        after_balance_count, 4,
        "after_id subscription should receive exactly 4 BalanceUpdated events \
         (2 accounts × 2 currencies), got {after_balance_count}"
    );
    assert!(
        after_collected
            .iter()
            .all(|e| e.r#type == SqlxLedgerEventType::BalanceUpdated),
        "All events after TransactionCreated should be BalanceUpdated"
    );

    let usd = rusty_money::iso::find("USD").unwrap();
    let btc = rusty_money::crypto::find("BTC").unwrap();

    let usd_credit_balance = get_balance(
        &ledger,
        journal_id,
        recipient_account_id,
        Currency::Iso(usd),
    )
    .await?;
    assert_eq!(usd_credit_balance.settled(), Decimal::from(100));

    let btc_credit_balance = get_balance(
        &ledger,
        journal_id,
        recipient_account_id,
        Currency::Crypto(btc),
    )
    .await?;
    assert_eq!(btc_credit_balance.settled(), Decimal::from(1290));

    let btc_debit_balance = get_balance(
        &ledger,
        journal_id,
        sender_account_id,
        Currency::Crypto(btc),
    )
    .await?;
    assert_eq!(btc_debit_balance.settled(), Decimal::from(-1290));

    let usd_credit_balance =
        get_balance(&ledger, journal_id, sender_account_id, Currency::Iso(usd)).await?;
    assert_eq!(usd_credit_balance.settled(), Decimal::from(-100));

    let external_id = uuid::Uuid::new_v4().to_string();
    params = TxParams::new();
    params.insert("journal_id", journal_id);
    params.insert("sender", sender_account_id);
    params.insert("recipient", recipient_account_id);
    params.insert("external_id", external_id.clone());

    ledger
        .post_transaction(TransactionId::new(), &tx_code, Some(params))
        .await
        .unwrap();

    let usd_credit_balance = get_balance(
        &ledger,
        journal_id,
        recipient_account_id,
        Currency::Iso(usd),
    )
    .await?;
    assert_eq!(usd_credit_balance.settled(), Decimal::from(200));

    let btc_credit_balance = get_balance(
        &ledger,
        journal_id,
        recipient_account_id,
        Currency::Crypto(btc),
    )
    .await?;
    assert_eq!(btc_credit_balance.settled(), Decimal::from(2580));

    let btc_debit_balance = get_balance(
        &ledger,
        journal_id,
        sender_account_id,
        Currency::Crypto(btc),
    )
    .await?;
    assert_eq!(btc_debit_balance.settled(), Decimal::from(-2580));

    let usd_credit_balance =
        get_balance(&ledger, journal_id, sender_account_id, Currency::Iso(usd)).await?;
    assert_eq!(usd_credit_balance.settled(), Decimal::from(-200));

    Ok(())
}

async fn get_balance(
    ledger: &SqlxLedger,
    journal_id: JournalId,
    account_id: AccountId,
    currency: Currency,
) -> anyhow::Result<AccountBalance> {
    let balance = ledger
        .balances()
        .find(journal_id, account_id, currency)
        .await?
        .unwrap();
    Ok(balance)
}