Lines
100 %
Functions
Branches
use forms::REPORT_ACTIVITY;
use sqlx::PgPool;
use super::test_report::{Case, case, golden, ledger};
const GOLDEN: &str = include_str!("../../../../server/tests/fixtures/reports/activity.txt");
const Q1: (&str, &str) = ("2025-01-01T00:00:00Z", "2025-04-01T00:00:00Z");
const H1: (&str, &str) = ("2025-01-01T00:00:00Z", "2025-07-01T00:00:00Z");
const YEARS: (&str, &str) = ("2024-01-01T00:00:00Z", "2026-01-01T00:00:00Z");
struct Ids {
usd: String,
eur: String,
jpy: String,
living: String,
food: String,
}
fn over(title: &'static str, (from, to): (&str, &str), extra: &[(&str, &str)]) -> Case {
let mut pairs = vec![("from", from), ("to", to)];
pairs.extend_from_slice(extra);
case(title, &pairs)
fn cases(ids: &Ids) -> 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= "{}"))"#, ids.jpy);
let subtree = format!(r#"(account-subtree "{}")"#, ids.living);
let food = format!(r#"(account= "{}")"#, ids.food);
let group = |name, filter, flip| [("name", name), ("filter", filter), ("flip", flip)];
vec![
over(
"defaults: one period, income and expense groups; from inclusive, to exclusive",
Q1,
&[],
),
"date_to as the end of its day includes that day",
(Q1.0, "2025-04-01T23:59:59Z"),
over("month grouping", Q1, &[("grouping", "month")]),
"quarter grouping, first bucket clipped to date_from",
("2025-02-15T00:00:00Z", "2025-08-01T00:00:00Z"),
&[("grouping", "quarter")],
"year grouping over a year boundary, jpy unconverted",
YEARS,
&[("grouping", "year")],
over("two groups: subtree and a flipped group", H1, &[]).rows(
"groups",
&[
&group("Living", &subtree, "no"),
&group("Pay", income, "yes"),
],
"overlapping groups count the same splits twice",
H1,
)
.rows(
&group("Expenses", expense, "no"),
&group("Food", &food, "no"),
over("a group that matches nothing", H1, &[]).rows(
&group("None", r#"(account-tag= "type" "liability")"#, "no"),
&group("Income", income, "yes"),
over("empty groups list", Q1, &[("grouping", "month")]).rows("groups", &[]),
"report filter composed with the default groups (and)",
&[("filter", home)],
"report filter composed with custom groups, grouped",
("grouping", "quarter"),
(
"filter",
&format!(r#"(or {home} (commodity= "{}"))"#, ids.eur),
.rows("groups", &[&group("Expenses", expense, "no")]),
"report filter that excludes everything",
&[("filter", &format!("(and {expense} {income})"))],
"target usd converts eur",
&[("target", &ids.usd), ("grouping", "month")],
"target usd over jpy has no price",
&[("target", &ids.usd)],
"target usd with jpy filtered out",
&[("target", &ids.usd), ("filter", &no_jpy)],
over("target eur: usd has no price", H1, &[("target", &ids.eur)]),
over("date_to equal to date_from", (Q1.0, Q1.0), &[]),
"date_to before date_from, grouped",
(Q1.1, Q1.0),
&[("grouping", "month")],
]
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn activity_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 ids = Ids {
usd: l.commodity("USD").to_string(),
eur: l.commodity("EUR").to_string(),
jpy: l.commodity("JPY").to_string(),
living: l.account("Living").to_string(),
food: l.account("Food").to_string(),
};
assert_eq!(golden(&REPORT_ACTIVITY, &l, &cases(&ids)).await, GOLDEN);
Ok(())