Lines
90.24 %
Functions
80 %
Branches
100 %
//! `account-balance` and `get-balance` refuse an account without exactly one commodity as `args`.
use server::command::account::CreateAccount;
use server::command::commodity::CreateCommodity;
use server::command::{CmdResult, FinanceEntity};
use sqlx::PgPool;
use uuid::Uuid;
use super::balance::{resolve_balance, run_get_balance_single};
use crate::host_failure::condition_code;
use crate::natives::transaction::run_logical::run_create_transaction_logical;
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
let user = Uuid::new_v4();
sqlx::query(
"INSERT INTO users \
(id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
VALUES ($1, 'Test', 'test@test.com', 'x', false, 'pw', 'user', 'db', NOW())",
)
.bind(user)
.execute(pool)
.await?;
Ok(user)
}
async fn seed_commodity(user: Uuid, symbol: &str) -> anyhow::Result<Uuid> {
match CreateCommodity::new()
.symbol(symbol.to_string())
.name(symbol.to_string())
.user_id(user)
.run()
.await?
{
Some(CmdResult::String(id)) => Ok(Uuid::parse_str(&id)?),
other => anyhow::bail!("unexpected CreateCommodity result: {other:?}"),
async fn seed_account(user: Uuid, name: &str) -> anyhow::Result<Uuid> {
match CreateAccount::new()
.name(name.to_string())
Some(CmdResult::Entity(FinanceEntity::Account(a))) => Ok(a.id),
other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
async fn transfer(user: Uuid, from: Uuid, to: Uuid, commodity: Uuid) -> anyhow::Result<()> {
let payload = format!(
"(:splits ((:from \"{from}\" :to \"{to}\" \
:from-commodity \"{commodity}\" :to-commodity \"{commodity}\" :value 10)))"
);
run_create_transaction_logical(user, Some(payload))
.await
.map_err(|err| anyhow::anyhow!("{err}"))?;
Ok(())
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn a_multi_currency_account_is_args_for_both_balance_natives(
pool: PgPool,
) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let user = seed_user(&pool).await?;
let usd = seed_commodity(user, "USD").await?;
let eur = seed_commodity(user, "EUR").await?;
let wallet = seed_account(user, "Wallet").await?;
let source = seed_account(user, "Source").await?;
transfer(user, source, wallet, usd).await?;
transfer(user, source, wallet, eur).await?;
let single = run_get_balance_single(user, Some(wallet.to_string()))
.unwrap_err();
assert_eq!(condition_code(&single), Some("args"), "{single}");
let typed = resolve_balance(user, Some(wallet.to_string()))
assert_eq!(condition_code(&typed), Some("args"), "{typed}");
assert!(typed.to_string().contains("get-balances"), "{typed}");
async fn an_account_without_a_commodity_is_args_for_account_balance(
let empty = seed_account(user, "Empty").await?;
let err = resolve_balance(user, Some(empty.to_string()))
assert_eq!(condition_code(&err), Some("args"), "{err}");
async fn a_single_commodity_account_gives_its_balance_and_commodity(
let balance = resolve_balance(user, Some(wallet.to_string()))
assert_eq!(balance, (10, 1, usd));