Lines
95.38 %
Functions
93.75 %
Branches
100 %
use std::collections::HashMap;
use chrono::Utc;
use finance::split::Split;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
use tokio::sync::OnceCell;
use uuid::Uuid;
use super::ListAccountSingleCommodities;
use crate::command::account::CreateAccount;
use crate::command::commodity::CreateCommodity;
use crate::command::transaction::CreateTransaction;
use crate::command::{CmdResult, FinanceEntity};
use crate::db::install_test_pool;
use crate::user::User;
static USER: OnceCell<User> = OnceCell::const_new();
async fn setup() {
USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
.await;
}
async fn user() -> anyhow::Result<&'static User> {
let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
user.commit().await?;
Ok(user)
async fn commodity(user: &User, symbol: &str) -> anyhow::Result<Uuid> {
let Some(CmdResult::String(id)) = CreateCommodity::new()
.symbol(symbol.to_string())
.name(symbol.to_string())
.user_id(user.id)
.run()
.await?
else {
anyhow::bail!("commodity");
};
Ok(Uuid::parse_str(&id)?)
async fn account(user: &User, name: &str) -> anyhow::Result<Uuid> {
let Some(CmdResult::Entity(FinanceEntity::Account(account))) = CreateAccount::new()
.name(name.to_string())
anyhow::bail!("account");
Ok(account.id)
async fn transfer(user: &User, legs: [(Uuid, Uuid); 2]) -> anyhow::Result<()> {
let tx_id = Uuid::new_v4();
let now = Utc::now();
let splits = legs
.into_iter()
.zip([-5, 5])
.map(|((account_id, commodity_id), value_num)| {
FinanceEntity::Split(Split {
id: Uuid::new_v4(),
tx_id,
account_id,
commodity_id,
value_num,
value_denom: 1,
reconcile_state: None,
reconcile_date: None,
lot_id: None,
})
.collect();
CreateTransaction::new()
.splits(splits)
.id(tx_id)
.post_date(now)
.enter_date(now)
.await?;
Ok(())
async fn single_commodities(user: &User) -> anyhow::Result<HashMap<Uuid, Uuid>> {
match ListAccountSingleCommodities::new()
{
Some(CmdResult::SingleCommodities(held)) => Ok(held),
other => anyhow::bail!("unexpected result: {other:?}"),
#[local_db_sqlx_test]
async fn test_only_single_commodity_accounts_are_listed(pool: PgPool) -> Result<(), anyhow::Error> {
let user = user().await?;
let usd = commodity(user, "USD").await?;
let eur = commodity(user, "EUR").await?;
let wallet = account(user, "Wallet").await?;
let cash = account(user, "Cash").await?;
let food = account(user, "Food").await?;
let idle = account(user, "Idle").await?;
transfer(user, [(cash, usd), (wallet, usd)]).await?;
transfer(user, [(cash, eur), (food, eur)]).await?;
let held = single_commodities(user).await?;
assert_eq!(
held,
HashMap::from([(wallet, usd), (food, eur)]),
"cash holds two commodities and idle none"
);
assert!(!held.contains_key(&idle));
async fn test_a_commodity_without_a_name_tag_does_not_count(
pool: PgPool,
) -> Result<(), anyhow::Error> {
let nameless = commodity(user, "NMS").await?;
let mixed = account(user, "Mixed").await?;
let other = account(user, "Other").await?;
transfer(user, [(mixed, usd), (wallet, usd)]).await?;
transfer(user, [(mixed, nameless), (other, nameless)]).await?;
sqlx::query(
"DELETE FROM commodity_tags WHERE commodity_id = $1 \
AND tag_id IN (SELECT id FROM tags WHERE tag_name = 'name')",
)
.bind(nameless)
.execute(&pool)
assert_eq!(held.get(&mixed), Some(&usd));
assert_eq!(held.get(&wallet), Some(&usd));
assert!(!held.contains_key(&other));