Lines
0 %
Functions
Branches
100 %
//! `update-transaction-logical` keeps the stored post date unless `:date` is given.
use chrono::{DateTime, TimeZone, Utc};
use sqlx::PgPool;
use uuid::Uuid;
use super::run_logical::run_update_transaction_logical;
use super::tests_logical_update::{seed_account, seed_commodity, seed_transaction, seed_user};
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn logical_update_preserves_post_date_without_date(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user_id = Uuid::new_v4();
seed_user(&pool, user_id).await?;
let usd_id = seed_commodity(user_id, "USD", "US Dollar").await?;
let a_id = seed_account(user_id, "Assets").await?;
let b_id = seed_account(user_id, "Expenses").await?;
let c_id = seed_account(user_id, "Income").await?;
let original = Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).single().unwrap();
let tx_id = seed_transaction(user_id, a_id, b_id, usd_id, original).await?;
// Split-only edit — no :date — must leave the stored post_date intact.
let no_date_payload = format!(
r#"(:transaction-id "{tx_id}"
:splits ((:from "{a_id}" :to "{c_id}"
:from-commodity "{usd_id}" :to-commodity "{usd_id}"
:value 50)))"#
);
run_update_transaction_logical(user_id, Some(no_date_payload))
.await
.map_err(|e| anyhow::anyhow!("{e}"))?;
let after_no_date =
sqlx::query_scalar::<_, DateTime<Utc>>("SELECT post_date FROM transactions WHERE id = $1")
.bind(tx_id)
.fetch_one(&pool)
.await?;
assert_eq!(
after_no_date, original,
"a split-only edit must not rewrite post_date"
// Now supply :date — the stored post_date must change to it.
let with_date_payload = format!(
:date "2021-06-15"
run_update_transaction_logical(user_id, Some(with_date_payload))
let after_with_date =
let expected = Utc
.with_ymd_and_hms(2021, 6, 15, 12, 0, 0)
.single()
.unwrap();
after_with_date, expected,
"an explicit :date must rewrite post_date"
Ok(())
}