1
use super::*;
2
use crate::command::transaction::ListTransactions;
3

            
4
5
async fn listed_amount(fx: &Fixture, tx_id: Uuid, account: Option<Uuid>) -> anyhow::Result<String> {
5
5
    let mut cmd = ListTransactions::new().user_id(fx.user.id);
6
5
    if let Some(account) = account {
7
4
        cmd = cmd.account(account);
8
1
    }
9
5
    let Some(CmdResult::TaggedTransactions { entities, .. }) = cmd.run().await? else {
10
        anyhow::bail!("expected TaggedTransactions");
11
    };
12
5
    entities
13
5
        .into_iter()
14
5
        .find_map(|(entity, _, amount)| match entity {
15
5
            FinanceEntity::Transaction(tx) if tx.id == tx_id => amount,
16
            _ => None,
17
5
        })
18
        .ok_or_else(|| anyhow::anyhow!("transaction not listed"))
19
5
}
20

            
21
#[local_db_sqlx_test]
22
async fn test_list_amount_is_the_filtered_accounts_signed_sum(
23
    pool: PgPool,
24
) -> Result<(), anyhow::Error> {
25
    let fx = fixture().await?;
26
    let tx_id = fx.create_tx(None).await?;
27
    let [from, to] = fx.accounts;
28

            
29
    assert_eq!(listed_amount(&fx, tx_id, Some(from)).await?, "-100.00 TST");
30
    assert_eq!(listed_amount(&fx, tx_id, Some(to)).await?, "100.00 TST");
31
}
32

            
33
#[local_db_sqlx_test]
34
async fn test_list_amount_unfiltered_is_the_positive_sum(
35
    pool: PgPool,
36
) -> Result<(), anyhow::Error> {
37
    let fx = fixture().await?;
38
    let tx_id = fx.create_tx(None).await?;
39

            
40
    assert_eq!(listed_amount(&fx, tx_id, None).await?, "100.00 TST");
41
}
42

            
43
#[local_db_sqlx_test]
44
async fn test_list_amount_filtered_sums_each_commodity_separately(
45
    pool: PgPool,
46
) -> Result<(), anyhow::Error> {
47
    let fx = fixture().await?;
48
    let Some(CmdResult::String(id)) = CreateCommodity::new()
49
        .symbol("OTH".to_string())
50
        .name("Other".to_string())
51
        .user_id(fx.user.id)
52
        .run()
53
        .await?
54
    else {
55
        anyhow::bail!("commodity");
56
    };
57
    let other = Uuid::parse_str(&id)?;
58
    let tx_id = Uuid::new_v4();
59
    let [first, second] = fx.accounts;
60
4
    let split = |id, account_id, commodity_id, value_num| {
61
4
        FinanceEntity::Split(Split {
62
4
            id,
63
4
            tx_id,
64
4
            account_id,
65
4
            commodity_id,
66
4
            value_num,
67
4
            value_denom: 1,
68
4
            reconcile_state: None,
69
4
            reconcile_date: None,
70
4
            lot_id: None,
71
4
        })
72
4
    };
73
4
    let [tst_out, tst_in, oth_in, oth_out] = [(); 4].map(|()| Uuid::new_v4());
74
    let now = Utc::now();
75
2
    let price = |commodity_split, currency_split| {
76
2
        FinanceEntity::Price(Price {
77
2
            id: Uuid::new_v4(),
78
2
            date: now,
79
2
            commodity_id: fx.commodity,
80
2
            currency_id: other,
81
2
            commodity_split: Some(commodity_split),
82
2
            currency_split: Some(currency_split),
83
2
            value_num: 3,
84
2
            value_denom: 10,
85
2
        })
86
2
    };
87
    CreateTransaction::new()
88
        .user_id(fx.user.id)
89
        .splits(vec![
90
            split(tst_out, first, fx.commodity, -100),
91
            split(tst_in, second, fx.commodity, 100),
92
            split(oth_in, first, other, 30),
93
            split(oth_out, second, other, -30),
94
        ])
95
        .prices(vec![price(tst_in, oth_out), price(tst_out, oth_in)])
96
        .id(tx_id)
97
        .post_date(now)
98
        .enter_date(now)
99
        .run()
100
        .await?;
101

            
102
    assert_eq!(
103
        listed_amount(&fx, tx_id, Some(first)).await?,
104
        "30.00 OTH; -100.00 TST"
105
    );
106
    assert_eq!(
107
        listed_amount(&fx, tx_id, Some(second)).await?,
108
        "-30.00 OTH; 100.00 TST"
109
    );
110
}