1
use forms::REPORT_ACTIVITY;
2
use sqlx::PgPool;
3

            
4
use super::test_report::{Case, case, golden, ledger};
5

            
6
const GOLDEN: &str = include_str!("../../../../server/tests/fixtures/reports/activity.txt");
7

            
8
const Q1: (&str, &str) = ("2025-01-01T00:00:00Z", "2025-04-01T00:00:00Z");
9
const H1: (&str, &str) = ("2025-01-01T00:00:00Z", "2025-07-01T00:00:00Z");
10
const YEARS: (&str, &str) = ("2024-01-01T00:00:00Z", "2026-01-01T00:00:00Z");
11

            
12
struct Ids {
13
    usd: String,
14
    eur: String,
15
    jpy: String,
16
    living: String,
17
    food: String,
18
}
19

            
20
18
fn over(title: &'static str, (from, to): (&str, &str), extra: &[(&str, &str)]) -> Case {
21
18
    let mut pairs = vec![("from", from), ("to", to)];
22
18
    pairs.extend_from_slice(extra);
23
18
    case(title, &pairs)
24
18
}
25

            
26
1
fn cases(ids: &Ids) -> Vec<Case> {
27
1
    let income = r#"(account-tag= "type" "income")"#;
28
1
    let expense = r#"(account-tag= "type" "expense")"#;
29
1
    let home = r#"(transaction-tag= "project" "home")"#;
30
1
    let no_jpy = format!(r#"(not (commodity= "{}"))"#, ids.jpy);
31
1
    let subtree = format!(r#"(account-subtree "{}")"#, ids.living);
32
1
    let food = format!(r#"(account= "{}")"#, ids.food);
33
7
    let group = |name, filter, flip| [("name", name), ("filter", filter), ("flip", flip)];
34
1
    vec![
35
1
        over(
36
1
            "defaults: one period, income and expense groups; from inclusive, to exclusive",
37
1
            Q1,
38
1
            &[],
39
1
        ),
40
1
        over(
41
1
            "date_to as the end of its day includes that day",
42
1
            (Q1.0, "2025-04-01T23:59:59Z"),
43
1
            &[],
44
1
        ),
45
1
        over("month grouping", Q1, &[("grouping", "month")]),
46
1
        over(
47
1
            "quarter grouping, first bucket clipped to date_from",
48
1
            ("2025-02-15T00:00:00Z", "2025-08-01T00:00:00Z"),
49
1
            &[("grouping", "quarter")],
50
1
        ),
51
1
        over(
52
1
            "year grouping over a year boundary, jpy unconverted",
53
1
            YEARS,
54
1
            &[("grouping", "year")],
55
1
        ),
56
1
        over("two groups: subtree and a flipped group", H1, &[]).rows(
57
1
            "groups",
58
1
            &[
59
1
                &group("Living", &subtree, "no"),
60
1
                &group("Pay", income, "yes"),
61
1
            ],
62
1
        ),
63
1
        over(
64
1
            "overlapping groups count the same splits twice",
65
1
            H1,
66
1
            &[("grouping", "quarter")],
67
1
        )
68
1
        .rows(
69
1
            "groups",
70
1
            &[
71
1
                &group("Expenses", expense, "no"),
72
1
                &group("Food", &food, "no"),
73
1
            ],
74
1
        ),
75
1
        over("a group that matches nothing", H1, &[]).rows(
76
1
            "groups",
77
1
            &[
78
1
                &group("None", r#"(account-tag= "type" "liability")"#, "no"),
79
1
                &group("Income", income, "yes"),
80
1
            ],
81
1
        ),
82
1
        over("empty groups list", Q1, &[("grouping", "month")]).rows("groups", &[]),
83
1
        over(
84
1
            "report filter composed with the default groups (and)",
85
1
            H1,
86
1
            &[("filter", home)],
87
1
        ),
88
1
        over(
89
1
            "report filter composed with custom groups, grouped",
90
1
            H1,
91
1
            &[
92
1
                ("grouping", "quarter"),
93
1
                (
94
1
                    "filter",
95
1
                    &format!(r#"(or {home} (commodity= "{}"))"#, ids.eur),
96
1
                ),
97
1
            ],
98
1
        )
99
1
        .rows("groups", &[&group("Expenses", expense, "no")]),
100
1
        over(
101
1
            "report filter that excludes everything",
102
1
            H1,
103
1
            &[("filter", &format!("(and {expense} {income})"))],
104
1
        ),
105
1
        over(
106
1
            "target usd converts eur",
107
1
            H1,
108
1
            &[("target", &ids.usd), ("grouping", "month")],
109
1
        ),
110
1
        over(
111
1
            "target usd over jpy has no price",
112
1
            YEARS,
113
1
            &[("target", &ids.usd)],
114
1
        ),
115
1
        over(
116
1
            "target usd with jpy filtered out",
117
1
            YEARS,
118
1
            &[("target", &ids.usd), ("filter", &no_jpy)],
119
1
        ),
120
1
        over("target eur: usd has no price", H1, &[("target", &ids.eur)]),
121
1
        over("date_to equal to date_from", (Q1.0, Q1.0), &[]),
122
1
        over(
123
1
            "date_to before date_from, grouped",
124
1
            (Q1.1, Q1.0),
125
1
            &[("grouping", "month")],
126
1
        ),
127
    ]
128
1
}
129

            
130
#[sqlx::test(migrator = "server::db::MIGRATOR")]
131
async fn activity_cases_through_the_form_equal_the_m3k_golden(pool: PgPool) -> anyhow::Result<()> {
132
    let _guard = server::db::install_test_pool(pool.clone());
133
    let l = ledger(&pool).await?;
134
    let ids = Ids {
135
        usd: l.commodity("USD").to_string(),
136
        eur: l.commodity("EUR").to_string(),
137
        jpy: l.commodity("JPY").to_string(),
138
        living: l.account("Living").to_string(),
139
        food: l.account("Food").to_string(),
140
    };
141

            
142
    assert_eq!(golden(&REPORT_ACTIVITY, &l, &cases(&ids)).await, GOLDEN);
143
    Ok(())
144
}