Lines
100 %
Functions
Branches
use super::*;
use crate::command::transaction::CROSS_PRICE;
use crate::fleet::jobs::cross_price::fixture::{Rows, seed};
type PriceRow = (Uuid, i64, i64);
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 split_ids(pool: &PgPool, tx: Uuid) -> anyhow::Result<Vec<Uuid>> {
Ok(
sqlx::query_scalar("SELECT id FROM splits WHERE tx_id = $1 ORDER BY id")
.bind(tx)
.fetch_all(pool)
.await?,
)
async fn prices_of(pool: &PgPool, tx: Uuid) -> anyhow::Result<Vec<PriceRow>> {
Ok(sqlx::query_as(
"SELECT p.id, p.value_num, p.value_denom FROM prices p \
JOIN splits s ON s.id = p.commodity_split_id WHERE s.tx_id = $1 ORDER BY p.id",
.await?)
fn price(id: Uuid, legs: &[Uuid], value_num: i64) -> FinanceEntity {
FinanceEntity::Price(Price {
id,
commodity_id: Uuid::from_u128(1),
currency_id: Uuid::from_u128(1),
commodity_split: legs.first().copied(),
currency_split: legs.get(1).copied(),
date: Utc::now(),
value_num,
value_denom: 2,
})
fn split(id: Uuid, tx: Uuid, value_num: i64) -> FinanceEntity {
FinanceEntity::Split(Split {
tx_id: tx,
account_id: Uuid::from_u128(2),
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
async fn update_prices(
user: &User,
tx: Uuid,
prices: Vec<FinanceEntity>,
) -> Result<Option<CmdResult>, CmdError> {
UpdateTransaction::new()
.user_id(user.id)
.transaction_id(tx)
.prices(prices)
.run()
.await
#[local_db_sqlx_test]
async fn a_prices_only_update_replaces_the_old_price(pool: PgPool) -> anyhow::Result<()> {
let (user, rows) = user_with_rows(&pool).await?;
let legs = split_ids(&pool, rows.tx_own).await?;
let new_id = Uuid::new_v4();
update_prices(user, rows.tx_own, vec![price(new_id, &legs, 3)]).await?;
assert_eq!(prices_of(&pool, rows.tx_own).await?, vec![(new_id, 3, 2)]);
let gone: i64 = sqlx::query_scalar("SELECT count(*) FROM prices WHERE id = $1")
.bind(rows.own_price)
.fetch_one(&pool)
.await?;
assert_eq!(gone, 0);
async fn a_resent_price_id_keeps_one_row_with_the_new_value(pool: PgPool) -> anyhow::Result<()> {
update_prices(user, rows.tx_own, vec![price(rows.own_price, &legs, 7)]).await?;
assert_eq!(
prices_of(&pool, rows.tx_own).await?,
vec![(rows.own_price, 7, 2)]
);
async fn an_update_without_prices_keeps_them(pool: PgPool) -> anyhow::Result<()> {
.transaction_id(rows.tx_own)
.note("changed".to_string())
vec![(rows.own_price, 1, 1)]
async fn a_split_replacing_update_drops_or_replaces_the_prices(pool: PgPool) -> anyhow::Result<()> {
let replace = |legs: [Uuid; 2]| {
.splits(vec![
split(legs[0], rows.tx_own, -5),
split(legs[1], rows.tx_own, 5),
])
};
replace([Uuid::new_v4(), Uuid::new_v4()]).run().await?;
assert_eq!(prices_of(&pool, rows.tx_own).await?, vec![]);
let legs = [Uuid::new_v4(), Uuid::new_v4()];
replace(legs)
.prices(vec![price(new_id, &legs, 9)])
assert_eq!(prices_of(&pool, rows.tx_own).await?, vec![(new_id, 9, 2)]);
async fn a_prices_only_update_of_a_cross_price_transaction_is_refused(
pool: PgPool,
) -> anyhow::Result<()> {
let legs = split_ids(&pool, rows.tx_a).await?;
let result = update_prices(user, rows.tx_a, vec![price(Uuid::new_v4(), &legs, 3)]).await;
assert!(
matches!(&result, Err(CmdError::Refused(message)) if message == CROSS_PRICE),
"{result:?}"
prices_of(&pool, rows.tx_a).await?,
vec![(rows.cross_price, 1, 1)]