Lines
100 %
Functions
Branches
use server::command::transaction::{SetTransactionTag, UpdateTransaction};
use sqlx::PgPool;
use uuid::Uuid;
use super::test_edit::{
Fx, condition, create, detail_text, fields, fx, post_date, price_dates, rename_symbol,
replace_once, row, rows_of, set_price_date, split_accounts, split_commodities, with_rows,
without_revision,
};
use super::test_seed::submit_edit;
use super::test_seed::{seed_account, seed_child_account, seed_commodity};
use super::{form_options, load_form};
const POSTED: &str = "2026-10-27T12:00:00Z";
const OLD_PRICE: &str = "2026-10-20T10:11:12.345678Z";
async fn dates_by_rate(pool: &PgPool) -> anyhow::Result<Vec<(i64, String)>> {
let rows: Vec<(i64, chrono::DateTime<chrono::Utc>)> =
sqlx::query_as("SELECT value_num, price_date FROM prices ORDER BY value_num")
.fetch_all(pool)
.await?;
Ok(rows
.into_iter()
.map(|(rate, date)| (rate, date.to_rfc3339()))
.collect())
}
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn defaulted_to_commodity_never_drops_a_renamed_row(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = fx(&pool).await?;
let tx = create(&fx, POSTED, &[fx.fx_row("10")]).await?;
let payload = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
rename_symbol(&pool, "USD", "DOLLAR").await?;
let cleared = replace_once(&payload, &format!(":to-commodity \"{}\" ", fx.eur), "");
let err = submit_edit(fx.user, &cleared)
.await
.expect_err("the row now has a to-amount without a second commodity");
assert_eq!(condition(&err).code, "form-invalid");
assert_eq!(split_commodities(&pool, tx).await?.len(), 2);
let plain = replace_once(&cleared, ":to-amount \"9.00\" ", "");
submit_edit(fx.user, &plain).await?;
let commodities = split_commodities(&pool, tx).await?;
assert_eq!(commodities, [fx.usd, fx.usd]);
Ok(())
async fn value_spelling_keeps_the_price_date(pool: PgPool) -> anyhow::Result<()> {
set_price_date(&pool, OLD_PRICE).await?;
let before = price_dates(&pool).await?;
for spelling in ["10", "20/2"] {
let respelled = replace_once(
&payload,
":value \"10.00\" :to-amount",
&format!(":value \"{spelling}\" :to-amount"),
);
submit_edit(fx.user, &respelled).await?;
assert_eq!(price_dates(&pool).await?, before, "{spelling}");
async fn reordered_fx_rows_keep_their_own_dates(pool: PgPool) -> anyhow::Result<()> {
let rows = [
fx.fx_row("10"),
fx.row(
"20",
&format!(":to-commodity \"{}\" :to-amount \"15\"", fx.eur),
),
];
let tx = create(&fx, POSTED, &rows).await?;
sqlx::query("UPDATE prices SET price_date = '2026-10-01T00:00:00.5Z' WHERE value_num = 10")
.execute(&pool)
sqlx::query("UPDATE prices SET price_date = '2026-10-02T00:00:00.25Z' WHERE value_num = 4")
let before = dates_by_rate(&pool).await?;
assert_ne!(before[0].1, before[1].1);
let mut rows = rows_of(&payload);
rows.reverse();
submit_edit(fx.user, &with_rows(&payload, &rows)).await?;
assert_eq!(dates_by_rate(&pool).await?, before);
async fn appended_fx_row_takes_the_post_date(pool: PgPool) -> anyhow::Result<()> {
rows.push(format!(
"(:key \"\" :from \"{}\" :to \"{}\" :from-commodity \"{}\" :to-commodity \"{}\" \
:value \"5\" :to-amount \"4\")",
fx.cash, fx.food, fx.usd, fx.eur
));
let posted = post_date(&pool, tx).await?.to_rfc3339();
let dates = dates_by_rate(&pool).await?;
assert_eq!(dates.len(), 2);
assert_eq!(dates[0], (5, posted));
assert_eq!(dates[1].0, 10);
assert!(dates[1].1.starts_with("2026-10-20T10:11:12.345678"));
async fn renamed_commodity_keeps_its_identity(pool: PgPool) -> anyhow::Result<()> {
let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
let impostor = seed_commodity(fx.user, "USD").await?;
submit_edit(fx.user, &payload).await?;
assert_eq!(split_commodities(&pool, tx).await?, [fx.usd, fx.usd]);
assert_ne!(fx.usd, impostor);
async fn a_changed_cell_moves_the_split(pool: PgPool) -> anyhow::Result<()> {
let other = seed_account(fx.user, "Other").await?;
let moved = replace_once(
&format!(":to \"{}\" ", fx.food),
&format!(":to \"{other}\" "),
submit_edit(fx.user, &moved).await?;
let accounts = split_accounts(&pool, tx).await?;
assert!(accounts.contains(&other) && !accounts.contains(&fx.food));
async fn transaction_tags_survive_an_edit(pool: PgPool) -> anyhow::Result<()> {
SetTransactionTag::new()
.user_id(fx.user)
.transaction_id(tx)
.tag_name("project".to_string())
.tag_value("x".to_string())
.run()
let tags: Vec<String> = sqlx::query_scalar(
"SELECT t.tag_name || '=' || t.tag_value FROM transaction_tags tt \
JOIN tags t ON t.id = tt.tag_id WHERE tt.tx_id = $1 AND t.tag_name <> 'note'",
)
.bind(tx)
.fetch_all(&pool)
assert_eq!(tags, ["project=x"]);
async fn stale_load_is_refused_and_reload_accepted(pool: PgPool) -> anyhow::Result<()> {
let stale = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
UpdateTransaction::new()
.note("changed elsewhere".to_string())
let err = submit_edit(fx.user, &stale).await.expect_err("stale");
let condition = condition(&err);
assert_eq!(condition.code, "form-invalid");
let detail = detail_text(&condition);
assert_eq!(fields(&detail), ["revision"]);
assert!(detail.contains("changed since it was loaded"), "{detail}");
let fresh = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
assert!(fresh.contains(":note \"changed elsewhere\""), "{fresh}");
submit_edit(fx.user, &fresh).await?;
async fn padded_name_gets_a_uuid_key_and_its_twin_stays_pickable(
pool: PgPool,
) -> anyhow::Result<()> {
let padded = seed_account(fx.user, "Padded").await?;
sqlx::query("UPDATE tags SET tag_value = 'Cash ' WHERE tag_value = 'Padded'")
let twin = fx.cash;
let tx = create(
&fx,
POSTED,
&[row(
&padded.to_string(),
&fx.food.to_string(),
&fx.usd.to_string(),
"10",
"",
)],
let options = form_options(fx.user, "accounts").await?;
assert!(options.contains(&format!(
":id \"{padded}\" :label \"Cash \" :alias \"Cash \""
)));
assert!(options.contains(&format!(":id \"{twin}\" :label \"Cash\" :alias \"Cash\"")));
assert!(
payload.contains(&format!(":from \"{padded}\"")),
"{payload}"
assert!(split_accounts(&pool, tx).await?.contains(&padded));
let picked = replace_once(
&format!(":from \"{padded}\""),
&format!(":from \"{twin}\""),
submit_edit(fx.user, &picked).await?;
assert!(accounts.contains(&twin) && !accounts.contains(&padded));
fn fx_row_from(fx: &Fx, from: Uuid, value: &str) -> String {
row(
&from.to_string(),
value,
fn wallet_row(fx: &Fx, from: &str) -> String {
format!(
"(:key \"\" :from \"{from}\" :to \"{}\" :from-commodity \"{}\" :value \"2\")",
fx.food, fx.usd
async fn path_labels_are_refused_and_ambiguous_ones_key_by_uuid(
let assets = seed_account(fx.user, "Assets").await?;
let nested = seed_child_account(fx.user, "Wallet", assets).await?;
rows.push(wallet_row(&fx, &nested.to_string()));
assert!(split_accounts(&pool, tx).await?.contains(&nested));
let twin = seed_account(fx.user, "Assets:Wallet").await?;
let siblings = seed_child_account(fx.user, "Wallet", assets).await?;
for id in [nested, twin, siblings] {
assert!(options.contains(&format!("(:id \"{id}\"")), "{options}");
rows.push(wallet_row(&fx, "Assets:Wallet"));
let err = submit_edit(fx.user, &with_rows(&payload, &rows))
.expect_err("a path is not an id");
let detail = detail_text(&condition(&err));
assert_eq!(fields(&detail), ["splits.2.from"]);
async fn unfit_names_round_trip_by_id(pool: PgPool) -> anyhow::Result<()> {
let pipe = seed_account(fx.user, "Expenses|Fx").await?;
let newline = seed_account(fx.user, "Line\nBreak").await?;
let cr = seed_account(fx.user, "A\rB").await?;
&[
fx_row_from(&fx, pipe, "10"),
fx_row_from(&fx, newline, "5"),
fx_row_from(&fx, cr, "2"),
],
for id in [pipe, newline, cr] {
assert!(!options.contains(&format!(":id \"{id}\" :label \"{id}\"")));
assert!(payload.contains(&format!(":from \"{id}\"")), "{payload}");
assert!(!payload.contains("Expenses|Fx"), "{payload}");
let again = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
assert_eq!(without_revision(&payload), without_revision(&again));
assert!(accounts.contains(&pipe) && accounts.contains(&newline) && accounts.contains(&cr));