Lines
100 %
Functions
Branches
//! The W0a pins of the web transaction edit, flipped in W6: the edit page over
//! the shared forms keeps an unchanged FX row's price date and refuses a
//! stale load.
use axum::http::StatusCode;
use chrono::{DateTime, TimeZone, Utc};
use sqlx::types::Uuid;
use web::model::User;
use crate::common::edit_page::{
browser_pairs, edit_form, fx_transaction, get_edit, ledger, post_edit, with_value,
};
use crate::common::tenant::tenant_connection;
async fn price_dates(user_id: Uuid) -> Vec<DateTime<Utc>> {
let mut conn = tenant_connection(user_id).await;
sqlx::query_scalar("SELECT price_date FROM prices ORDER BY price_date")
.fetch_all(&mut *conn)
.await
.expect("prices")
}
async fn notes(user_id: Uuid) -> Vec<String> {
sqlx::query_scalar(
"SELECT t.tag_value FROM tags t JOIN transaction_tags tt ON tt.tag_id = t.id \
WHERE t.tag_name = 'note' ORDER BY t.tag_value",
)
.expect("notes")
async fn edit_pairs(user: &User, id: Uuid, note: &str) -> Vec<(String, String)> {
let page = get_edit(user, &id.to_string()).await;
assert_eq!(page.status, StatusCode::OK, "{}", page.body);
with_value(&browser_pairs(&page.body), "note", note)
/// Flipped in W6: a note-only edit of an FX transaction keeps its price date.
#[supp_macro::shared_runtime_test]
async fn note_only_fx_edit_keeps_the_price_date() {
let ledger = ledger().await;
let id = fx_transaction(&ledger, "first").await;
let seeded = Utc
.timestamp_opt(1_583_065_815, 654_321_000)
.single()
.expect("instant");
let mut conn = tenant_connection(ledger.user.id).await;
sqlx::query("UPDATE prices SET price_date = $1")
.bind(seeded)
.execute(&mut *conn)
.expect("seed price date");
drop(conn);
let pairs = edit_pairs(&ledger.user, id, "edited").await;
let reply = post_edit(&ledger.user, &pairs).await;
let notes = notes(ledger.user.id).await;
let dates = price_dates(ledger.user.id).await;
assert_eq!(reply.status, StatusCode::OK, "{}", reply.body);
assert_eq!(notes, vec!["edited"]);
assert_eq!(dates, vec![seeded]);
/// Flipped in W6: of two pages loaded from the same revision, the second
/// submit is refused with the reload banner and stores nothing.
async fn concurrent_edit_is_refused_on_revision() {
let first = edit_pairs(&ledger.user, id, "from the first tab").await;
let second = edit_pairs(&ledger.user, id, "from the second tab").await;
let accepted = post_edit(&ledger.user, &first).await;
let refused = post_edit(&ledger.user, &second).await;
assert_eq!(accepted.status, StatusCode::OK, "{}", accepted.body);
assert_eq!(
refused.status,
StatusCode::UNPROCESSABLE_ENTITY,
"{}",
refused.body
);
let form = edit_form(&refused.body);
assert!(
form.contains(&format!(
"class=\"form-reload\" href=\"/transaction/{id}/edit\""
)),
"{form}"
assert_eq!(notes, vec!["from the first tab"]);
/// A rate added after the transaction survives a note-only edit and stays the
/// latest.
async fn a_newer_unrelated_rate_survives_a_note_only_edit() {
let newer = Utc.with_ymd_and_hms(2030, 1, 1, 0, 0, 0).unwrap();
let (commodity, currency): (Uuid, Uuid) =
sqlx::query_as("SELECT commodity_id, currency_id FROM prices")
.fetch_one(&mut *conn)
.expect("transaction price");
sqlx::query(
"INSERT INTO prices (id, commodity_id, currency_id, price_date, value_num, value_denom) \
VALUES ($1, $2, $3, $4, 7, 1)",
.bind(Uuid::new_v4())
.bind(commodity)
.bind(currency)
.bind(newer)
.expect("newer rate");
let latest: i64 = sqlx::query_scalar("SELECT value_num FROM prices ORDER BY price_date DESC")
.expect("latest rate");
assert_eq!(dates.len(), 2, "{dates:?}");
assert_eq!(dates.last(), Some(&newer));
assert_eq!(latest, 7);