1
use chrono::{DateTime, Utc};
2
use sqlx::PgPool;
3
use supp_macro::local_db_sqlx_test;
4

            
5
use super::seed::{Ledger, at};
6
use super::{Golden, account_type, install_test_pool, ledger, project_home, setup};
7
use crate::command::report::ActivityReport;
8
use crate::command::{ActivityGroup, PeriodGrouping, ReportFilter};
9

            
10
7
fn group(label: &str, filter: ReportFilter, flip_sign: bool) -> ActivityGroup {
11
7
    ActivityGroup {
12
7
        label: label.to_owned(),
13
7
        filter,
14
7
        flip_sign,
15
7
    }
16
7
}
17

            
18
14
async fn run(
19
14
    l: &Ledger,
20
14
    from: DateTime<Utc>,
21
14
    to: DateTime<Utc>,
22
14
    grouping: Option<PeriodGrouping>,
23
14
    groups: Option<Vec<ActivityGroup>>,
24
14
    filter: Option<ReportFilter>,
25
14
) -> Result<Option<crate::command::CmdResult>, crate::command::CmdError> {
26
14
    let mut cmd = ActivityReport::new()
27
14
        .user_id(l.user.id)
28
14
        .date_from(from)
29
14
        .date_to(to);
30
14
    if let Some(grouping) = grouping {
31
7
        cmd = cmd.period_grouping(grouping);
32
7
    }
33
14
    if let Some(groups) = groups {
34
5
        cmd = cmd.groups(groups);
35
9
    }
36
14
    if let Some(filter) = filter {
37
3
        cmd = cmd.report_filter(filter);
38
11
    }
39
14
    cmd.run().await
40
14
}
41

            
42
#[local_db_sqlx_test]
43
async fn activity_report_contract(pool: PgPool) {
44
    let l = ledger().await?;
45
    let q1 = (at(2025, 1, 1, 0, 0, 0), at(2025, 4, 1, 0, 0, 0));
46
    let h1 = (at(2025, 1, 1, 0, 0, 0), at(2025, 7, 1, 0, 0, 0));
47
    let years = (at(2024, 1, 1, 0, 0, 0), at(2026, 1, 1, 0, 0, 0));
48
1
    let no_jpy = || ReportFilter::Not(Box::new(ReportFilter::CommodityEq(l.commodity("JPY"))));
49
    let mut g = Golden::new("activity.txt");
50

            
51
    g.case(
52
        &l,
53
        "defaults: one period, income and expense groups; from inclusive, to exclusive",
54
        run(&l, q1.0, q1.1, None, None, None).await,
55
    );
56
    g.case(
57
        &l,
58
        "date_to as the end of its day includes that day",
59
        run(&l, q1.0, at(2025, 4, 1, 23, 59, 59), None, None, None).await,
60
    );
61
    g.case(
62
        &l,
63
        "month grouping",
64
        run(&l, q1.0, q1.1, Some(PeriodGrouping::Month), None, None).await,
65
    );
66
    g.case(
67
        &l,
68
        "quarter grouping, first bucket clipped to date_from",
69
        run(
70
            &l,
71
            at(2025, 2, 15, 0, 0, 0),
72
            at(2025, 8, 1, 0, 0, 0),
73
            Some(PeriodGrouping::Quarter),
74
            None,
75
            None,
76
        )
77
        .await,
78
    );
79
    g.case(
80
        &l,
81
        "year grouping over a year boundary, jpy unconverted",
82
        run(&l, years.0, years.1, Some(PeriodGrouping::Year), None, None).await,
83
    );
84
    g.case(
85
        &l,
86
        "two groups: subtree and a flipped group",
87
        run(
88
            &l,
89
            h1.0,
90
            h1.1,
91
            None,
92
            Some(vec![
93
                group(
94
                    "Living",
95
                    ReportFilter::AccountSubtree(l.account("Living")),
96
                    false,
97
                ),
98
                group("Pay", account_type("income"), true),
99
            ]),
100
            None,
101
        )
102
        .await,
103
    );
104
    g.case(
105
        &l,
106
        "overlapping groups count the same splits twice",
107
        run(
108
            &l,
109
            h1.0,
110
            h1.1,
111
            Some(PeriodGrouping::Quarter),
112
            Some(vec![
113
                group("Expenses", account_type("expense"), false),
114
                group("Food", ReportFilter::AccountEq(l.account("Food")), false),
115
            ]),
116
            None,
117
        )
118
        .await,
119
    );
120
    g.case(
121
        &l,
122
        "a group that matches nothing",
123
        run(
124
            &l,
125
            h1.0,
126
            h1.1,
127
            None,
128
            Some(vec![
129
                group("None", account_type("liability"), false),
130
                group("Income", account_type("income"), true),
131
            ]),
132
            None,
133
        )
134
        .await,
135
    );
136
    g.case(
137
        &l,
138
        "empty groups list",
139
        run(
140
            &l,
141
            q1.0,
142
            q1.1,
143
            Some(PeriodGrouping::Month),
144
            Some(vec![]),
145
            None,
146
        )
147
        .await,
148
    );
149
    g.case(
150
        &l,
151
        "report filter composed with the default groups (and)",
152
        run(&l, h1.0, h1.1, None, None, Some(project_home())).await,
153
    );
154
    g.case(
155
        &l,
156
        "report filter composed with custom groups, grouped",
157
        run(
158
            &l,
159
            h1.0,
160
            h1.1,
161
            Some(PeriodGrouping::Quarter),
162
            Some(vec![group("Expenses", account_type("expense"), false)]),
163
            Some(ReportFilter::Or(vec![
164
                project_home(),
165
                ReportFilter::CommodityEq(l.commodity("EUR")),
166
            ])),
167
        )
168
        .await,
169
    );
170
    g.case(
171
        &l,
172
        "report filter that excludes everything",
173
        run(
174
            &l,
175
            h1.0,
176
            h1.1,
177
            None,
178
            None,
179
            Some(ReportFilter::And(vec![
180
                account_type("expense"),
181
                account_type("income"),
182
            ])),
183
        )
184
        .await,
185
    );
186
    g.case(
187
        &l,
188
        "target usd converts eur",
189
        ActivityReport::new()
190
            .user_id(l.user.id)
191
            .date_from(h1.0)
192
            .date_to(h1.1)
193
            .target_commodity_id(l.commodity("USD"))
194
            .period_grouping(PeriodGrouping::Month)
195
            .run()
196
            .await,
197
    );
198
    g.case(
199
        &l,
200
        "target usd over jpy has no price",
201
        ActivityReport::new()
202
            .user_id(l.user.id)
203
            .date_from(years.0)
204
            .date_to(years.1)
205
            .target_commodity_id(l.commodity("USD"))
206
            .run()
207
            .await,
208
    );
209
    g.case(
210
        &l,
211
        "target usd with jpy filtered out",
212
        ActivityReport::new()
213
            .user_id(l.user.id)
214
            .date_from(years.0)
215
            .date_to(years.1)
216
            .target_commodity_id(l.commodity("USD"))
217
            .report_filter(no_jpy())
218
            .run()
219
            .await,
220
    );
221
    g.case(
222
        &l,
223
        "target eur: usd has no price",
224
        ActivityReport::new()
225
            .user_id(l.user.id)
226
            .date_from(h1.0)
227
            .date_to(h1.1)
228
            .target_commodity_id(l.commodity("EUR"))
229
            .run()
230
            .await,
231
    );
232
    g.case(
233
        &l,
234
        "date_to equal to date_from",
235
        run(&l, q1.0, q1.0, None, None, None).await,
236
    );
237
    g.case(
238
        &l,
239
        "date_to before date_from, grouped",
240
        run(&l, q1.1, q1.0, Some(PeriodGrouping::Month), None, None).await,
241
    );
242
    g.check()?;
243
}