Lines
92.68 %
Functions
85.71 %
Branches
100 %
use super::*;
use crate::command::transaction::ListTransactions;
async fn listed_amount(fx: &Fixture, tx_id: Uuid, account: Option<Uuid>) -> anyhow::Result<String> {
let mut cmd = ListTransactions::new().user_id(fx.user.id);
if let Some(account) = account {
cmd = cmd.account(account);
}
let Some(CmdResult::TaggedTransactions { entities, .. }) = cmd.run().await? else {
anyhow::bail!("expected TaggedTransactions");
};
entities
.into_iter()
.find_map(|(entity, _, amount)| match entity {
FinanceEntity::Transaction(tx) if tx.id == tx_id => amount,
_ => None,
})
.ok_or_else(|| anyhow::anyhow!("transaction not listed"))
#[local_db_sqlx_test]
async fn test_list_amount_is_the_filtered_accounts_signed_sum(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let fx = fixture().await?;
let tx_id = fx.create_tx(None).await?;
let [from, to] = fx.accounts;
assert_eq!(listed_amount(&fx, tx_id, Some(from)).await?, "-100.00 TST");
assert_eq!(listed_amount(&fx, tx_id, Some(to)).await?, "100.00 TST");
async fn test_list_amount_unfiltered_is_the_positive_sum(
assert_eq!(listed_amount(&fx, tx_id, None).await?, "100.00 TST");
async fn test_list_amount_filtered_sums_each_commodity_separately(
let Some(CmdResult::String(id)) = CreateCommodity::new()
.symbol("OTH".to_string())
.name("Other".to_string())
.user_id(fx.user.id)
.run()
.await?
else {
anyhow::bail!("commodity");
let other = Uuid::parse_str(&id)?;
let tx_id = Uuid::new_v4();
let [first, second] = fx.accounts;
let split = |id, account_id, commodity_id, value_num| {
FinanceEntity::Split(Split {
id,
tx_id,
account_id,
commodity_id,
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
let [tst_out, tst_in, oth_in, oth_out] = [(); 4].map(|()| Uuid::new_v4());
let now = Utc::now();
let price = |commodity_split, currency_split| {
FinanceEntity::Price(Price {
id: Uuid::new_v4(),
date: now,
commodity_id: fx.commodity,
currency_id: other,
commodity_split: Some(commodity_split),
currency_split: Some(currency_split),
value_num: 3,
value_denom: 10,
CreateTransaction::new()
.splits(vec![
split(tst_out, first, fx.commodity, -100),
split(tst_in, second, fx.commodity, 100),
split(oth_in, first, other, 30),
split(oth_out, second, other, -30),
])
.prices(vec![price(tst_in, oth_out), price(tst_out, oth_in)])
.id(tx_id)
.post_date(now)
.enter_date(now)
.await?;
assert_eq!(
listed_amount(&fx, tx_id, Some(first)).await?,
"30.00 OTH; -100.00 TST"
);
listed_amount(&fx, tx_id, Some(second)).await?,
"-30.00 OTH; 100.00 TST"