Skip to content

Commit

Permalink
Add test to cover balance failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
xkikeg committed Feb 10, 2025
1 parent 8c537a2 commit 62a41f0
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions core/src/report/book_keeping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,58 @@ mod tests {
ret
}

#[test]
fn add_transaction_fails_with_inconsistent_balance() {
let arena = Bump::new();
let mut ctx = ReportContext::new(&arena);
let mut bal = Balance::default();
bal.add_posting_amount(
ctx.accounts.ensure("Account 1"),
PostingAmount::from_value(dec!(1000), ctx.commodities.ensure("JPY")),
);
let input = indoc! {"
2024/08/01 Sample
Account 2
Account 1 200 JPY = 1300 JPY
"};
let txn = parse_transaction(input);
let mut price_repos = PriceRepositoryBuilder::default();

let got_err = add_transaction(&mut ctx, &mut price_repos, &mut bal, &txn).unwrap_err();

assert!(
matches!(got_err, BookKeepError::BalanceAssertionFailure(_, _)),
"unexpected got_err: {:?}",
got_err
);
}

#[test]
fn add_transaction_fails_with_inconsistent_absolute_zero_balance() {
let arena = Bump::new();
let mut ctx = ReportContext::new(&arena);
let mut bal = Balance::default();
bal.add_posting_amount(
ctx.accounts.ensure("Account 1"),
PostingAmount::from_value(dec!(1000), ctx.commodities.ensure("JPY")),
);
let input = indoc! {"
2024/08/01 Sample
Account 2
Account 1 0 CHF = 0 ; must fail because of 1000 JPY
"};
let txn = parse_transaction(input);
let mut price_repos = PriceRepositoryBuilder::default();

let got_err = add_transaction(&mut ctx, &mut price_repos, &mut bal, &txn).unwrap_err();

assert!(
matches!(got_err, BookKeepError::BalanceAssertionFailure(_, _)),
"unexpected got_err: {:?}",
got_err
);
}

#[test]
fn add_transaction_maintains_balance() {
let arena = Bump::new();
Expand Down

0 comments on commit 62a41f0

Please sign in to comment.