Lines
100 %
Functions
Branches
use chrono::Utc;
use finance::split::Split;
use server::command::FinanceEntity;
use server::command::transaction::CreateTransaction;
use sqlx::PgPool;
use uuid::Uuid;
use super::load_form;
use super::test_edit::{
Fx, condition, create, detail_text, fields, fx, replace_once, rows_of, with_rows,
};
use super::test_seed::submit_edit;
use crate::natives::transaction::run_logical::run_create_transaction_logical;
const POSTED: &str = "2026-10-27T12:00:00Z";
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn load_reply_shape(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fx = fx(&pool).await?;
let (cash, food, usd, eur) = (fx.cash, fx.food, fx.usd, fx.eur);
let created = run_create_transaction_logical(
fx.user,
Some(format!(
"(:date \"{POSTED}\" :note \"hi\" :splits (\
(:from \"{cash}\" :to \"{food}\" :from-commodity \"{usd}\" :to-commodity \"{eur}\" \
:value 10 :to-amount 9/2 :tags ((:name \"memo\" :value \"m\" :description \"d\")))))"
)),
)
.await?;
let reply = load_form(fx.user, "transaction-edit", &created).await?;
let hidden = format!(
"(:from-id \\\"{cash}\\\" :to-id \\\"{food}\\\" :from-commodity-id \\\"{usd}\\\" \
:to-commodity-id \\\"{eur}\\\" :value \\\"10\\\" \
:to-amount \\\"9/2\\\" :price-date \\\"{POSTED}\\\")"
);
let revision = reply
.split(" :revision \"")
.nth(1)
.and_then(|rest| rest.split('"').next())
.unwrap_or_default();
assert_eq!(revision.len(), 64);
assert_eq!(
reply,
format!(
"(:id \"{created}\" :revision \"{revision}\" :date \"{POSTED}\" :note \"hi\" :tags () :splits ((:key \"r1\" :from \"{cash}\" \
:to \"{food}\" :from-commodity \"{usd}\" :to-commodity \"{eur}\" :value \"10.00\" \
:to-amount \"4.50\" :hidden \"{hidden}\")) \
:split-tags ((:row \"r1\" :name \"memo\" :value \"m\" :description \"d\")))"
Ok(())
}
async fn untagged_row_still_carries_hidden_and_empty_note(pool: PgPool) -> anyhow::Result<()> {
let payload = format!("(:date \"{POSTED}\" :splits ({}))", fx.row("1", ""));
let id = super::test_seed::submit_create(fx.user, &payload).await?;
let reply = load_form(fx.user, "transaction-edit", &id).await?;
assert!(reply.contains(":note \"\""), "{reply}");
assert!(reply.contains(":hidden \"(:from-id"), "{reply}");
assert!(reply.ends_with(":split-tags ())"), "{reply}");
assert!(!reply.contains(":price-date"), "{reply}");
assert!(!reply.contains(":to-amount"), "{reply}");
assert!(reply.contains(":to-commodity \"\" "), "{reply}");
async fn expect_refusal(fx: &Fx, id: &str, needle: &str) -> anyhow::Result<()> {
let err = load_form(fx.user, "transaction-edit", id)
.await
.expect_err("must be refused");
let condition = condition(&err);
assert_eq!(condition.code, "args");
assert!(condition.message.contains(needle), "{}", condition.message);
pub(super) fn leg(fx: &Fx, tx_id: Uuid, account_id: Uuid, value_num: i64) -> Split {
Split {
id: Uuid::new_v4(),
tx_id,
account_id,
commodity_id: fx.usd,
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
pub(super) async fn raw_transaction(
fx: &Fx,
splits: impl FnOnce(Uuid) -> Vec<Split>,
) -> anyhow::Result<Uuid> {
let tx_id = Uuid::new_v4();
let splits = splits(tx_id)
.into_iter()
.map(FinanceEntity::Split)
.collect();
CreateTransaction::new()
.user_id(fx.user)
.id(tx_id)
.post_date(Utc::now())
.enter_date(Utc::now())
.splits(splits)
.run()
Ok(tx_id)
async fn ambiguous_pairing_is_refused(pool: PgPool) -> anyhow::Result<()> {
let tx = raw_transaction(&fx, |tx| {
vec![
leg(&fx, tx, fx.cash, -100),
leg(&fx, tx, fx.food, 60),
leg(&fx, tx, fx.food, 40),
]
})
expect_refusal(&fx, &tx.to_string(), "paired").await
async fn reconciliation_data_is_refused(pool: PgPool) -> anyhow::Result<()> {
let mut reconciled = leg(&fx, tx, fx.cash, -100);
reconciled.reconcile_state = Some(true);
vec![reconciled, leg(&fx, tx, fx.food, 100)]
expect_refusal(&fx, &tx.to_string(), "reconciliation").await
async fn uncleared_transaction(fx: &Fx) -> anyhow::Result<Uuid> {
raw_transaction(fx, |tx| {
let mut uncleared = leg(fx, tx, fx.cash, -100);
uncleared.reconcile_state = Some(false);
vec![uncleared, leg(fx, tx, fx.food, 100)]
async fn states(pool: &PgPool, tx: Uuid) -> anyhow::Result<Vec<Option<bool>>> {
Ok(
sqlx::query_scalar(
"SELECT reconcile_state FROM splits WHERE tx_id = $1 ORDER BY value_num",
.bind(tx)
.fetch_all(pool)
.await?,
async fn an_untouched_edit_keeps_a_known_uncleared_split(pool: PgPool) -> anyhow::Result<()> {
let tx = uncleared_transaction(&fx).await?;
let payload = load_form(fx.user, "transaction-edit", &tx.to_string()).await?;
assert!(
payload.contains(":from-reconcile \\\"false\\\""),
"{payload}"
submit_edit(fx.user, &payload).await?;
assert_eq!(states(&pool, tx).await?, [Some(false), None]);
async fn a_note_only_edit_keeps_a_known_uncleared_split(pool: PgPool) -> anyhow::Result<()> {
let edited = replace_once(&payload, ":note \"\"", ":note \"changed\"");
submit_edit(fx.user, &edited).await?;
async fn a_changed_row_keeps_the_state_of_its_splits(pool: PgPool) -> anyhow::Result<()> {
let edited = replace_once(&payload, ":value \"100.00\"", ":value \"90\"");
async fn an_appended_row_has_an_unknown_state(pool: PgPool) -> anyhow::Result<()> {
let mut rows = rows_of(&payload);
rows.push(fx.row("5", ""));
submit_edit(fx.user, &with_rows(&payload, &rows)).await?;
states(&pool, tx).await?,
[Some(false), None, None, None],
"only the loaded row's from split is known"
async fn mismatched_rate_is_refused(pool: PgPool) -> anyhow::Result<()> {
let tx = create(&fx, POSTED, &[fx.fx_row("10")]).await?;
sqlx::query("UPDATE prices SET value_num = value_num + 1")
.execute(&pool)
expect_refusal(&fx, &tx.to_string(), "exchange rate").await
async fn tag_line_break_is_refused(pool: PgPool) -> anyhow::Result<()> {
let (cash, food, usd) = (fx.cash, fx.food, fx.usd);
let id = run_create_transaction_logical(
"(:splits ((:from \"{cash}\" :to \"{food}\" :from-commodity \"{usd}\" \
:to-commodity \"{usd}\" :value 1 :tags ((:name \"memo\" :value \"a\nb\")))))"
expect_refusal(&fx, &id, "line break").await
async fn reconciled_after_load_is_a_stale_revision(pool: PgPool) -> anyhow::Result<()> {
let tx = create(&fx, POSTED, &[fx.row("10", "")]).await?;
sqlx::query("UPDATE splits SET reconcile_state = TRUE")
let err = submit_edit(fx.user, &payload)
.expect_err("must be stale");
assert_eq!(condition.code, "form-invalid");
assert_eq!(fields(&detail_text(&condition)), ["revision"]);
async fn unchanged_transaction_without_refusal_still_submits(pool: PgPool) -> anyhow::Result<()> {
async fn unknown_id_is_not_found_and_bad_input_is_args(pool: PgPool) -> anyhow::Result<()> {
for (name, id, code) in [
("transaction-edit", Uuid::new_v4().to_string(), "not-found"),
("transaction-edit", "nope".to_string(), "args"),
(
"transaction-create",
Uuid::new_v4().to_string(),
"not-found",
),
] {
let err = load_form(fx.user, name, &id).await.expect_err(code);
assert_eq!(condition(&err).code, code, "{name} {id}");