1
use super::super::transaction::{ACCOUNT_GONE, UpdateTransaction};
2
use super::*;
3

            
4
4
fn split(tx_id: Uuid, account_id: Uuid, commodity_id: Uuid, value_num: i64) -> FinanceEntity {
5
4
    FinanceEntity::Split(Split {
6
4
        id: Uuid::new_v4(),
7
4
        tx_id,
8
4
        account_id,
9
4
        commodity_id,
10
4
        value_num,
11
4
        value_denom: 1,
12
4
        reconcile_state: None,
13
4
        reconcile_date: None,
14
4
        lot_id: None,
15
4
    })
16
4
}
17

            
18
#[local_db_sqlx_test]
19
async fn test_a_create_onto_a_merged_account_is_refused(pool: PgPool) -> Result<(), anyhow::Error> {
20
    let fx = fixture().await?;
21
    let cash = fx.account("Cash", None).await?;
22
    let into = fx.account("Cafe", None).await?;
23
    let from = fx.account("Cafe", None).await?;
24
    fx.merge(into, from).await?;
25
    let tx_id = Uuid::new_v4();
26
    let now = Utc::now();
27

            
28
    let created = CreateTransaction::new()
29
        .user_id(fx.user.id)
30
        .splits(vec![
31
            split(tx_id, cash, fx.commodity, -5),
32
            split(tx_id, from, fx.commodity, 5),
33
        ])
34
        .id(tx_id)
35
        .post_date(now)
36
        .enter_date(now)
37
        .run()
38
        .await;
39

            
40
    assert!(
41
        matches!(&created, Err(CmdError::Refused(message)) if message == ACCOUNT_GONE),
42
        "{created:?}"
43
    );
44
    assert_eq!(fx.split_count(cash).await?, 0);
45
}
46

            
47
#[local_db_sqlx_test]
48
async fn test_an_edit_onto_a_merged_account_is_refused(pool: PgPool) -> Result<(), anyhow::Error> {
49
    let fx = fixture().await?;
50
    let cash = fx.account("Cash", None).await?;
51
    let into = fx.account("Cafe", None).await?;
52
    let from = fx.account("Cafe", None).await?;
53
    let tx_id = fx.transfer(cash, into, 5).await?;
54
    fx.merge(into, from).await?;
55

            
56
    let updated = UpdateTransaction::new()
57
        .user_id(fx.user.id)
58
        .transaction_id(tx_id)
59
        .splits(vec![
60
            split(tx_id, cash, fx.commodity, -5),
61
            split(tx_id, from, fx.commodity, 5),
62
        ])
63
        .run()
64
        .await;
65

            
66
    assert!(
67
        matches!(&updated, Err(CmdError::Refused(message)) if message == ACCOUNT_GONE),
68
        "{updated:?}"
69
    );
70
    assert_eq!(fx.split_count(into).await?, 1, "the edit rolled back");
71
}