1
//! `account-balance` and `get-balance` refuse an account without exactly one commodity as `args`.
2

            
3
use server::command::account::CreateAccount;
4
use server::command::commodity::CreateCommodity;
5
use server::command::{CmdResult, FinanceEntity};
6
use sqlx::PgPool;
7
use uuid::Uuid;
8

            
9
use super::balance::{resolve_balance, run_get_balance_single};
10
use crate::host_failure::condition_code;
11
use crate::natives::transaction::run_logical::run_create_transaction_logical;
12

            
13
3
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
14
3
    let user = Uuid::new_v4();
15
3
    sqlx::query(
16
3
        "INSERT INTO users \
17
3
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
18
3
         VALUES ($1, 'Test', 'test@test.com', 'x', false, 'pw', 'user', 'db', NOW())",
19
3
    )
20
3
    .bind(user)
21
3
    .execute(pool)
22
3
    .await?;
23
3
    Ok(user)
24
3
}
25

            
26
3
async fn seed_commodity(user: Uuid, symbol: &str) -> anyhow::Result<Uuid> {
27
3
    match CreateCommodity::new()
28
3
        .symbol(symbol.to_string())
29
3
        .name(symbol.to_string())
30
3
        .user_id(user)
31
3
        .run()
32
3
        .await?
33
    {
34
3
        Some(CmdResult::String(id)) => Ok(Uuid::parse_str(&id)?),
35
        other => anyhow::bail!("unexpected CreateCommodity result: {other:?}"),
36
    }
37
3
}
38

            
39
5
async fn seed_account(user: Uuid, name: &str) -> anyhow::Result<Uuid> {
40
5
    match CreateAccount::new()
41
5
        .name(name.to_string())
42
5
        .user_id(user)
43
5
        .run()
44
5
        .await?
45
    {
46
5
        Some(CmdResult::Entity(FinanceEntity::Account(a))) => Ok(a.id),
47
        other => anyhow::bail!("unexpected CreateAccount result: {other:?}"),
48
    }
49
5
}
50

            
51
3
async fn transfer(user: Uuid, from: Uuid, to: Uuid, commodity: Uuid) -> anyhow::Result<()> {
52
3
    let payload = format!(
53
3
        "(:splits ((:from \"{from}\" :to \"{to}\" \
54
3
         :from-commodity \"{commodity}\" :to-commodity \"{commodity}\" :value 10)))"
55
    );
56
3
    run_create_transaction_logical(user, Some(payload))
57
3
        .await
58
        .map_err(|err| anyhow::anyhow!("{err}"))?;
59
3
    Ok(())
60
3
}
61

            
62
#[sqlx::test(migrator = "server::db::MIGRATOR")]
63
async fn a_multi_currency_account_is_args_for_both_balance_natives(
64
    pool: PgPool,
65
) -> anyhow::Result<()> {
66
    let _guard = server::db::install_test_pool(pool.clone());
67
    let user = seed_user(&pool).await?;
68
    let usd = seed_commodity(user, "USD").await?;
69
    let eur = seed_commodity(user, "EUR").await?;
70
    let wallet = seed_account(user, "Wallet").await?;
71
    let source = seed_account(user, "Source").await?;
72
    transfer(user, source, wallet, usd).await?;
73
    transfer(user, source, wallet, eur).await?;
74

            
75
    let single = run_get_balance_single(user, Some(wallet.to_string()))
76
        .await
77
        .unwrap_err();
78
    assert_eq!(condition_code(&single), Some("args"), "{single}");
79
    let typed = resolve_balance(user, Some(wallet.to_string()))
80
        .await
81
        .unwrap_err();
82
    assert_eq!(condition_code(&typed), Some("args"), "{typed}");
83
    assert!(typed.to_string().contains("get-balances"), "{typed}");
84
    Ok(())
85
}
86

            
87
#[sqlx::test(migrator = "server::db::MIGRATOR")]
88
async fn an_account_without_a_commodity_is_args_for_account_balance(
89
    pool: PgPool,
90
) -> anyhow::Result<()> {
91
    let _guard = server::db::install_test_pool(pool.clone());
92
    let user = seed_user(&pool).await?;
93
    let empty = seed_account(user, "Empty").await?;
94

            
95
    let err = resolve_balance(user, Some(empty.to_string()))
96
        .await
97
        .unwrap_err();
98
    assert_eq!(condition_code(&err), Some("args"), "{err}");
99
    Ok(())
100
}
101

            
102
#[sqlx::test(migrator = "server::db::MIGRATOR")]
103
async fn a_single_commodity_account_gives_its_balance_and_commodity(
104
    pool: PgPool,
105
) -> anyhow::Result<()> {
106
    let _guard = server::db::install_test_pool(pool.clone());
107
    let user = seed_user(&pool).await?;
108
    let usd = seed_commodity(user, "USD").await?;
109
    let wallet = seed_account(user, "Wallet").await?;
110
    let source = seed_account(user, "Source").await?;
111
    transfer(user, source, wallet, usd).await?;
112

            
113
    let balance = resolve_balance(user, Some(wallet.to_string()))
114
        .await
115
        .map_err(|err| anyhow::anyhow!("{err}"))?;
116
    assert_eq!(balance, (10, 1, usd));
117
    Ok(())
118
}