Lines
97.53 %
Functions
100 %
Branches
use chrono::{DateTime, Duration, NaiveDate, Utc};
use forms::{FieldError, Form, FormValues};
use server::command::CmdResult;
use server::command::report::fixture::render;
use server::command::report::fixture::seed::{Ledger, seed};
use server::user::User;
use sqlx::PgPool;
use super::test_seed::seed_user;
use crate::forms::{Failure, Outcome, typed_report_on};
/// A ledger transaction posted two days from now, after the end of any default as-of.
pub(super) async fn post_after_today(ledger: &Ledger) -> anyhow::Result<()> {
let later = Utc::now() + Duration::days(2);
ledger
.post(
later,
&[("Checking", "USD", 777, 1), ("Salary", "USD", -777, 1)],
)
.await?;
Ok(())
}
/// The day the default dates are computed from; after every fixture transaction.
pub(super) fn today() -> NaiveDate {
NaiveDate::from_ymd_opt(2030, 6, 15).expect("date")
/// [`today`] at noon UTC, the instant the default dates are computed at.
pub(super) fn now() -> DateTime<Utc> {
today().and_hms_opt(12, 0, 0).expect("time").and_utc()
/// One fixture case: its title in the golden file and the form values that express it.
pub(super) struct Case {
pub title: &'static str,
pub values: FormValues,
pub(super) fn case(title: &'static str, pairs: &[(&str, &str)]) -> Case {
let mut values = FormValues::default();
for (key, value) in pairs {
values.set(key, value);
Case { title, values }
impl Case {
/// A `rows` field of the given rows; no rows marks the field present and empty.
pub(super) fn rows(mut self, key: &str, rows: &[&[(&str, &str)]]) -> Self {
self.values.set_rows(key);
for row in rows {
let cells = row.iter().map(|(k, v)| (k.to_string(), v.to_string()));
self.values.push_row(key, cells.collect());
self
pub(super) async fn ledger(pool: &PgPool) -> anyhow::Result<Ledger> {
let id = seed_user(pool).await?;
seed(User { id }).await
fn result(ledger: &Ledger, outcome: Outcome) -> String {
let data = match outcome {
Outcome::Balance(data) => CmdResult::Report(data),
Outcome::Activity(data) => CmdResult::Activity(data),
Outcome::Breakdown(data) => CmdResult::Breakdown(data),
other => return format!("unexpected outcome {other:?}\n"),
};
render::data(ledger, &data)
/// A refusal reads as the command's own error; the field error the handler raises in its place
/// carries the same message under the `Wrong arguments` prefix.
fn failure(failure: Failure) -> String {
match failure {
Failure::Invalid(errors) => {
let messages: Vec<&str> = errors.iter().map(|e| e.message.as_str()).collect();
render::failure(format!("Wrong arguments: {}", messages.join("; ")))
other => render::failure(other),
/// The cases run through the report form on the fixed [`today`], rendered like the M3k golden
/// file; the as-of that defaults to that day reads as the golden's open end.
pub(super) async fn golden(form: &Form, ledger: &Ledger, cases: &[Case]) -> String {
let mut text = String::new();
for case in cases {
if !text.is_empty() {
text.push('\n');
text.push_str(&format!("== {}\n", case.title));
text.push_str(
&match typed_report_on(ledger.user.id, form, &case.values, now())
.await
.map(Outcome::from)
{
Ok(outcome) => result(ledger, outcome),
Err(error) => failure(error),
},
);
let open_end = format!("to={}T23:59:59.999999+00:00", today());
text.replace(&open_end, "to=-")
fn field_errors(result: Result<Outcome, Failure>) -> anyhow::Result<Vec<(String, String)>> {
match result {
Err(Failure::Invalid(errors)) => Ok(errors
.into_iter()
.map(|FieldError { field, message }| (field, message))
.collect()),
other => anyhow::bail!("expected field errors, got {other:?}"),
pub(super) async fn invalid(
pool: &PgPool,
form: &Form,
values: &FormValues,
) -> anyhow::Result<Vec<(String, String)>> {
let l = ledger(pool).await?;
field_errors(
typed_report_on(l.user.id, form, values, now())
.map(Outcome::from),