Lines
75 %
Functions
Branches
100 %
use forms::REPORT_CATEGORY;
use scripting::runtime::HostCondition;
use server::command::report::view::category_csv;
use sqlx::PgPool;
use super::test_report::{case, ledger};
use crate::envelope::ErrorCode;
use crate::forms::{ReportOutcome, typed_report};
use crate::natives::report::run_report_csv;
const RANGE: &str = "(:from \"2025-01-01\" :to \"2025-07-01\")";
async fn typed_csv(user: uuid::Uuid, pairs: &[(&str, &str)]) -> anyhow::Result<String> {
let values = case("", pairs).values;
let ReportOutcome::Breakdown(data) = typed_report(user, &REPORT_CATEGORY, &values).await?
else {
anyhow::bail!("not a category report");
};
Ok(category_csv(&data))
}
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn the_native_returns_the_category_report_as_csv(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let id = ledger(&pool).await?.user.id;
let csv = run_report_csv(id, RANGE).await?;
let expected = typed_csv(id, &[("from", "2025-01-01"), ("to", "2025-07-01")]).await?;
assert_eq!(csv, expected);
assert!(
csv.starts_with("\u{feff}category,Commodity,Amount\r\n"),
"{csv:?}"
);
Ok(())
async fn an_empty_form_is_the_month_to_date_report(pool: PgPool) -> anyhow::Result<()> {
let csv = run_report_csv(id, "()").await?;
assert_eq!(csv, typed_csv(id, &[]).await?);
async fn an_invalid_date_is_a_field_error(pool: PgPool) -> anyhow::Result<()> {
let err = run_report_csv(id, "(:from \"yesterday\")")
.await
.expect_err("a bad date is refused");
let condition = err
.chain()
.find_map(|cause| cause.downcast_ref::<HostCondition>())
.ok_or_else(|| anyhow::anyhow!("not a condition: {err}"))?;
assert_eq!(condition.code, ErrorCode::FORM_INVALID);
format!("{:?}", condition.detail).contains("\"from\""),
"{condition:?}"