Lines
100 %
Functions
Branches
use chrono::{DateTime, Utc};
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use super::seed::{Ledger, at};
use super::{Golden, account_type, install_test_pool, ledger, project_home, setup};
use crate::command::report::ActivityReport;
use crate::command::{ActivityGroup, PeriodGrouping, ReportFilter};
fn group(label: &str, filter: ReportFilter, flip_sign: bool) -> ActivityGroup {
ActivityGroup {
label: label.to_owned(),
filter,
flip_sign,
}
async fn run(
l: &Ledger,
from: DateTime<Utc>,
to: DateTime<Utc>,
grouping: Option<PeriodGrouping>,
groups: Option<Vec<ActivityGroup>>,
filter: Option<ReportFilter>,
) -> Result<Option<crate::command::CmdResult>, crate::command::CmdError> {
let mut cmd = ActivityReport::new()
.user_id(l.user.id)
.date_from(from)
.date_to(to);
if let Some(grouping) = grouping {
cmd = cmd.period_grouping(grouping);
if let Some(groups) = groups {
cmd = cmd.groups(groups);
if let Some(filter) = filter {
cmd = cmd.report_filter(filter);
cmd.run().await
#[local_db_sqlx_test]
async fn activity_report_contract(pool: PgPool) {
let l = ledger().await?;
let q1 = (at(2025, 1, 1, 0, 0, 0), at(2025, 4, 1, 0, 0, 0));
let h1 = (at(2025, 1, 1, 0, 0, 0), at(2025, 7, 1, 0, 0, 0));
let years = (at(2024, 1, 1, 0, 0, 0), at(2026, 1, 1, 0, 0, 0));
let no_jpy = || ReportFilter::Not(Box::new(ReportFilter::CommodityEq(l.commodity("JPY"))));
let mut g = Golden::new("activity.txt");
g.case(
&l,
"defaults: one period, income and expense groups; from inclusive, to exclusive",
run(&l, q1.0, q1.1, None, None, None).await,
);
"date_to as the end of its day includes that day",
run(&l, q1.0, at(2025, 4, 1, 23, 59, 59), None, None, None).await,
"month grouping",
run(&l, q1.0, q1.1, Some(PeriodGrouping::Month), None, None).await,
"quarter grouping, first bucket clipped to date_from",
run(
at(2025, 2, 15, 0, 0, 0),
at(2025, 8, 1, 0, 0, 0),
Some(PeriodGrouping::Quarter),
None,
)
.await,
"year grouping over a year boundary, jpy unconverted",
run(&l, years.0, years.1, Some(PeriodGrouping::Year), None, None).await,
"two groups: subtree and a flipped group",
h1.0,
h1.1,
Some(vec![
group(
"Living",
ReportFilter::AccountSubtree(l.account("Living")),
false,
),
group("Pay", account_type("income"), true),
]),
"overlapping groups count the same splits twice",
group("Expenses", account_type("expense"), false),
group("Food", ReportFilter::AccountEq(l.account("Food")), false),
"a group that matches nothing",
group("None", account_type("liability"), false),
group("Income", account_type("income"), true),
"empty groups list",
q1.0,
q1.1,
Some(PeriodGrouping::Month),
Some(vec![]),
"report filter composed with the default groups (and)",
run(&l, h1.0, h1.1, None, None, Some(project_home())).await,
"report filter composed with custom groups, grouped",
Some(vec![group("Expenses", account_type("expense"), false)]),
Some(ReportFilter::Or(vec![
project_home(),
ReportFilter::CommodityEq(l.commodity("EUR")),
])),
"report filter that excludes everything",
Some(ReportFilter::And(vec![
account_type("expense"),
account_type("income"),
"target usd converts eur",
ActivityReport::new()
.date_from(h1.0)
.date_to(h1.1)
.target_commodity_id(l.commodity("USD"))
.period_grouping(PeriodGrouping::Month)
.run()
"target usd over jpy has no price",
.date_from(years.0)
.date_to(years.1)
"target usd with jpy filtered out",
.report_filter(no_jpy())
"target eur: usd has no price",
.target_commodity_id(l.commodity("EUR"))
"date_to equal to date_from",
run(&l, q1.0, q1.0, None, None, None).await,
"date_to before date_from, grouped",
run(&l, q1.1, q1.0, Some(PeriodGrouping::Month), None, None).await,
g.check()?;