Lines
100 %
Functions
Branches
use super::*;
use crate::command::transaction::CROSS_PRICE;
use crate::fleet::jobs::cross_price::fixture::{Rows, seed};
async fn count(pool: &PgPool, sql: &'static str, id: Uuid) -> anyhow::Result<i64> {
Ok(sqlx::query_scalar(sql).bind(id).fetch_one(pool).await?)
}
async fn assert_intact(pool: &PgPool, rows: &Rows) -> anyhow::Result<()> {
for tx in [rows.tx_a, rows.tx_b] {
let sql = "SELECT count(*) FROM transactions WHERE id = $1";
assert_eq!(count(pool, sql, tx).await?, 1);
let sql = "SELECT count(*) FROM splits WHERE tx_id = $1";
assert_eq!(count(pool, sql, tx).await?, 2);
let sql = "SELECT count(*) FROM prices WHERE id = $1";
assert_eq!(count(pool, sql, rows.cross_price).await?, 1);
Ok(())
fn assert_refused<T: std::fmt::Debug>(result: Result<T, CmdError>) {
assert!(
matches!(&result, Err(CmdError::Refused(message)) if message == CROSS_PRICE),
"{result:?}"
);
async fn user_with_rows(pool: &PgPool) -> anyhow::Result<(&'static User, Rows)> {
let user = USER.get().unwrap();
user.commit().await?;
Ok((user, seed(pool).await?))
async fn update(user: &User, tx: Uuid) -> Result<Option<CmdResult>, CmdError> {
UpdateTransaction::new()
.user_id(user.id)
.transaction_id(tx)
.note("changed".to_string())
.run()
.await
async fn delete(user: &User, tx: Uuid) -> Result<Option<CmdResult>, CmdError> {
DeleteTransaction::new()
#[local_db_sqlx_test]
async fn update_of_either_transaction_is_refused(pool: PgPool) -> anyhow::Result<()> {
let (user, rows) = user_with_rows(&pool).await?;
assert_refused(update(user, rows.tx_a).await);
assert_refused(update(user, rows.tx_b).await);
assert_intact(&pool, &rows).await?;
let sql = "SELECT count(*) FROM transaction_tags WHERE tx_id = $1";
assert_eq!(count(&pool, sql, rows.tx_a).await?, 0);
assert_eq!(count(&pool, sql, rows.tx_b).await?, 0);
async fn delete_of_either_transaction_is_refused(pool: PgPool) -> anyhow::Result<()> {
assert_refused(delete(user, rows.tx_a).await);
assert_refused(delete(user, rows.tx_b).await);
async fn an_ordinary_price_still_updates_and_deletes(pool: PgPool) -> anyhow::Result<()> {
let prices = "SELECT count(*) FROM prices WHERE id = $1";
update(user, rows.tx_own).await?;
assert_eq!(count(&pool, prices, rows.own_price).await?, 1);
delete(user, rows.tx_own).await?;
assert_eq!(count(&pool, prices, rows.own_price).await?, 0);
assert_eq!(count(&pool, sql, rows.tx_own).await?, 0);