Lines
100 %
Functions
Branches
mod balance;
mod multicurrency;
mod price_links;
use crate::account::{Account, AccountBuilder};
use crate::commodity::{Commodity, CommodityBuilder};
use crate::price::Price;
use crate::split::SplitBuilder;
use crate::tag::Tag;
use crate::transaction::*;
#[cfg(feature = "testlog")]
use sqlx::PgPool;
use sqlx::types::Uuid;
use sqlx::types::chrono::Local;
use tokio::sync::OnceCell;
/// Context for keeping environment intact
static CONTEXT: OnceCell<()> = OnceCell::const_new();
static COMMODITY: OnceCell<Commodity> = OnceCell::const_new();
static WALLET: OnceCell<Account> = OnceCell::const_new();
static SHOP: OnceCell<Account> = OnceCell::const_new();
static FOREIGN_COMMODITY: OnceCell<Commodity> = OnceCell::const_new();
static ONLINE_SHOP: OnceCell<Account> = OnceCell::const_new();
static FOREIGN_COMMODITY_2: OnceCell<Commodity> = OnceCell::const_new();
static ONLINE_SHOP_2: OnceCell<Account> = OnceCell::const_new();
async fn setup(pool: &PgPool) {
let mut conn = pool.acquire().await.unwrap();
CONTEXT
.get_or_init(|| async {
crate::test_logging::init();
})
.await;
COMMODITY
.get_or_init(|| async { CommodityBuilder::new().id(Uuid::new_v4()).build().unwrap() })
COMMODITY.get().unwrap().commit(&mut *conn).await.unwrap();
WALLET
.get_or_init(|| async { AccountBuilder::new().id(Uuid::new_v4()).build().unwrap() })
WALLET.get().unwrap().commit(&mut *conn).await.unwrap();
SHOP.get_or_init(|| async { AccountBuilder::new().id(Uuid::new_v4()).build().unwrap() })
SHOP.get().unwrap().commit(&mut *conn).await.unwrap();
FOREIGN_COMMODITY
.get()
.unwrap()
.commit(&mut *conn)
.await
.unwrap();
ONLINE_SHOP
ONLINE_SHOP.get().unwrap().commit(&mut *conn).await.unwrap();
FOREIGN_COMMODITY_2
ONLINE_SHOP_2
}
#[sqlx::test(migrator = "crate::MIGRATOR")]
async fn test_transaction_store(pool: PgPool) {
setup(&pool).await;
let transaction = Transaction {
id: Uuid::new_v4(),
post_date: Local::now().into(),
enter_date: Local::now().into(),
};
sqlx::query_file!(
"sql/transaction_insert.sql",
&transaction.id,
&transaction.post_date,
&transaction.enter_date
)
.execute(&mut *conn)
let result = sqlx::query!("SELECT id, post_date FROM transactions")
.fetch_one(&mut *conn)
assert_eq!(transaction.id, result.id);
async fn test_transaction_builer(pool: PgPool) -> anyhow::Result<()> {
let build = Transaction::builder().id(Uuid::new_v4()).build();
assert!(build.is_err());
let build = Transaction::builder()
.id(Uuid::new_v4())
.post_date(Local::now().into())
.enter_date(Local::now().into())
.build();
assert!(build.is_ok());
Ok(())
async fn test_create_transaction(pool: PgPool) -> anyhow::Result<()> {
let tx = Transaction::builder()
.build()?;
let mut conn = pool.acquire().await?;
let mut tr = tx.enter(&mut *conn).await?;
tr.rollback().await?;
assert!(
sqlx::query!("SELECT id, post_date FROM transactions")
.is_err()
);
assert!(tr.commit().await.is_err()); // Erroneous split
/// Regression: two transactions tagged with the same
/// `(tag_name, tag_value)` pair must both link to the canonical
/// row. Before the fix `add_tags` linked via the in-memory
/// `Tag.id`, so the second call tripped
/// `transaction_tags_tag_id_fkey` because the canonical row's
/// id was returned by `Tag::commit` but discarded. Same shape
/// for `add_split_tags` — covered as well.
async fn test_add_tags_uses_canonical_id(pool: PgPool) -> anyhow::Result<()> {
let tag_template = || Tag {
tag_name: "category".to_string(),
tag_value: "groceries".to_string(),
description: None,
for value in [-100i64, -200] {
let split_spend = SplitBuilder::new()
.account_id(WALLET.get().unwrap().id)
.commodity_id(COMMODITY.get().unwrap().id)
.value_num(value)
.value_denom(1)
.tx_id(tx.id)
let split_purchase = SplitBuilder::new()
.account_id(SHOP.get().unwrap().id)
.value_num(-value)
tr.add_splits(&[&split_spend, &split_purchase]).await?;
let tx_tag = tag_template();
tr.add_tags(&[&tx_tag]).await?;
tr.add_split_tags(&[(split_spend.id, tag_template())])
.await?;
tr.commit().await?;
let tag_count: i64 =
sqlx::query_scalar("SELECT COUNT(*) FROM tags WHERE tag_name = 'category'")
assert_eq!(tag_count, 1, "tag rows must be canonical");
let link_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM transaction_tags")
assert_eq!(
link_count, 2,
"both transactions linked to the canonical tag"
let split_link_count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM split_tags")
split_link_count, 2,
"both splits linked to the canonical tag"
/// A script may repeat a transaction tag the caller already gave.
async fn test_add_tags_twice_links_once(pool: PgPool) -> anyhow::Result<()> {
let tag = || Tag {
tag_name: "trip".to_string(),
tag_value: "japan".to_string(),
let leg = |account: Uuid, value: i64| {
SplitBuilder::new()
.account_id(account)
.build()
let spend = leg(WALLET.get().unwrap().id, -5)?;
let purchase = leg(SHOP.get().unwrap().id, 5)?;
tr.add_splits(&[&spend, &purchase]).await?;
tr.add_tags(&[&tag(), &tag()]).await?;
let links: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM transaction_tags WHERE tx_id = $1")
.bind(tx.id)
.fetch_one(&pool)
assert_eq!(links, 1);
async fn test_transaction_balance(pool: PgPool) -> anyhow::Result<()> {
.value_num(-100)
.value_num(100)
assert!(tr.commit().await.is_ok());
.value_num(99)
assert!(tr.commit().await.is_err());