Lines
100 %
Functions
Branches
use forms::TRANSACTION_CREATE;
use super::super::create_from_template;
use super::*;
use crate::flat::load_values;
use crate::template;
const LUNCH: &str = r#"(set-draft-note "Lunch")(set-draft-date "2026-06-15")
(draft-tag "category" "food")
(draft-split (get-account "Cash") (get-commodity "USD") -5/1)
(draft-split (get-account "Food") (get-commodity "USD") 5/1)
(draft-split-tag 0 "memo" "lunch")"#;
const UNDATED: &str = r#"(set-draft-note "Coffee")
(draft-split (get-account "Cash") (get-commodity "USD") -2/1)
(draft-split (get-account "Food") (get-commodity "USD") 2/1)"#;
/// The `show` lines without the transaction id.
async fn shown(session: &mut Session, id: &str) -> anyhow::Result<Vec<String>> {
Ok(show(session, id).await?.into_iter().skip(1).collect())
}
/// What the web does: load the template's values and submit them unchanged.
async fn created_as_web_does(session: &mut Session, template_id: &str) -> anyhow::Result<String> {
let loaded = load_values(session, &TRANSACTION_CREATE, template_id).await?;
Ok(eval_text(session, &submit_call(&TRANSACTION_CREATE, &loaded)).await?)
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn creating_from_a_template_matches_the_web_create(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let mut session = seeded_session(&pool).await?;
let template_id = template::create(&mut session, Some("Lunch"), LUNCH).await?;
let ours = create_from_template(&mut session, &template_id, &FormValues::default()).await?;
let web = created_as_web_does(&mut session, &template_id).await?;
let shown_ours = shown(&mut session, &ours).await?;
assert_eq!(shown_ours, shown(&mut session, &web).await?);
assert!(
shown_ours.contains(&"Note: Lunch".to_string()),
"{shown_ours:?}"
);
shown_ours.iter().any(|l| l.contains("memo")),
Ok(())
async fn given_values_override_the_draft(pool: PgPool) -> anyhow::Result<()> {
let template_id = template::create(&mut session, None, LUNCH).await?;
let overrides = changes(&[("note", "Dinner"), ("date", "2026-07-01")], &[]);
let id = create_from_template(&mut session, &template_id, &overrides).await?;
let lines = shown(&mut session, &id).await?;
lines.iter().any(|l| l.starts_with("Date: 2026-07-01")),
"{lines:?}"
assert!(lines.contains(&"Note: Dinner".to_string()), "{lines:?}");
assert!(lines.iter().any(|l| l.contains("Cash")), "{lines:?}");
async fn a_given_row_is_added_to_the_draft_rows(pool: PgPool) -> anyhow::Result<()> {
let extra = row(&[
("from", "Cash"),
("to", "Food"),
("from-commodity", "USD"),
("value", "1"),
]);
let id = create_from_template(&mut session, &template_id, &changes(&[], &[extra])).await?;
assert_eq!(
lines.iter().filter(|l| l.contains("Food")).count(),
2,
async fn a_template_without_a_date_gets_now(pool: PgPool) -> anyhow::Result<()> {
let template_id = template::create(&mut session, None, UNDATED).await?;
let id = create_from_template(&mut session, &template_id, &FormValues::default()).await?;
let today = forms::now_instant();
lines
.iter()
.any(|l| l.starts_with(&format!("Date: {}", &today[..10]))),
async fn an_unknown_template_is_refused(pool: PgPool) -> anyhow::Result<()> {
let refused = create_from_template(
&mut session,
&Uuid::new_v4().to_string(),
&FormValues::default(),
)
.await;
assert!(refused.is_err(), "{refused:?}");