Lines
100 %
Functions
Branches
use forms::REPORT_BALANCE;
use sqlx::PgPool;
use super::test_report::{Case, case, golden, ledger};
const GOLDEN: &str = include_str!("../../../../server/tests/fixtures/reports/balance.txt");
fn cases(usd: &str, eur: &str, jpy: &str, living: &str) -> Vec<Case> {
let income = r#"(account-tag= "type" "income")"#;
let expense = r#"(account-tag= "type" "expense")"#;
let home = r#"(transaction-tag= "project" "home")"#;
let no_jpy = format!(r#"(not (commodity= "{jpy}"))"#);
vec![
case("defaults", &[]),
case(
"as_of exactly on a transaction instant",
&[("as-of", "2025-01-31T23:59:59Z")],
),
"as_of one second before that transaction",
&[("as-of", "2025-01-31T23:59:58Z")],
"as_of before every transaction",
&[("as-of", "2024-01-01T00:00:00Z")],
"snapshot as_of on the quarter instant includes it",
&[("as-of", "2025-04-01T00:00:00Z")],
"date_from alone runs to now",
&[("from", "2025-02-01T00:00:00Z")],
"date_from and as_of: both inclusive",
&[("from", "2025-01-01"), ("as-of", "2025-04-01T00:00:00Z")],
"date_from after as_of",
&[("from", "2025-04-01"), ("as-of", "2025-01-01T00:00:00Z")],
"date_from equal to as_of",
&[("from", "2025-04-01"), ("as-of", "2025-04-01T00:00:00Z")],
"target usd converts eur, as_of before jpy",
&[("target", usd), ("as-of", "2025-06-30T00:00:00Z")],
"target usd with a jpy split has no price",
&[("target", usd)],
"target eur: usd has no price",
&[("target", eur), ("as-of", "2025-06-30T00:00:00Z")],
"target usd over a range",
&[
("target", usd),
("from", "2025-02-01"),
("as-of", "2025-07-01T00:00:00Z"),
],
"target usd with a filter dropping jpy",
&[("target", usd), ("filter", &no_jpy)],
"filter and: expense accounts of project home",
&[("filter", &format!("(and {expense} {home})"))],
"filter or: expense accounts or project home",
&[("filter", &format!("(or {expense} {home})"))],
"filter that excludes everything",
&[("filter", &format!("(and {expense} {income})"))],
"filter nested: income or food-split, without jpy",
&[(
"filter",
&format!(r#"(and (or {income} (split-tag= "category" "food")) {no_jpy})"#),
)],
"filter account subtree",
&[("filter", &format!(r#"(account-subtree "{living}")"#))],
"filter and range",
("from", "2025-01-01"),
("filter", &format!(r#"(commodity= "{usd}")"#)),
]
}
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn balance_cases_through_the_form_equal_the_m3k_golden(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let l = ledger(&pool).await?;
let id = |symbol| l.commodity(symbol).to_string();
let cases = cases(
&id("USD"),
&id("EUR"),
&id("JPY"),
&l.account("Living").to_string(),
);
assert_eq!(golden(&REPORT_BALANCE, &l, &cases).await, GOLDEN);
Ok(())