1
use super::*;
2
use crate::command::transaction::CROSS_PRICE;
3
use crate::fleet::jobs::cross_price::fixture::{Rows, seed};
4

            
5
20
async fn count(pool: &PgPool, sql: &'static str, id: Uuid) -> anyhow::Result<i64> {
6
20
    Ok(sqlx::query_scalar(sql).bind(id).fetch_one(pool).await?)
7
20
}
8

            
9
3
async fn assert_intact(pool: &PgPool, rows: &Rows) -> anyhow::Result<()> {
10
3
    for tx in [rows.tx_a, rows.tx_b] {
11
6
        let sql = "SELECT count(*) FROM transactions WHERE id = $1";
12
6
        assert_eq!(count(pool, sql, tx).await?, 1);
13
6
        let sql = "SELECT count(*) FROM splits WHERE tx_id = $1";
14
6
        assert_eq!(count(pool, sql, tx).await?, 2);
15
    }
16
3
    let sql = "SELECT count(*) FROM prices WHERE id = $1";
17
3
    assert_eq!(count(pool, sql, rows.cross_price).await?, 1);
18
3
    Ok(())
19
3
}
20

            
21
4
fn assert_refused<T: std::fmt::Debug>(result: Result<T, CmdError>) {
22
4
    assert!(
23
4
        matches!(&result, Err(CmdError::Refused(message)) if message == CROSS_PRICE),
24
        "{result:?}"
25
    );
26
4
}
27

            
28
3
async fn user_with_rows(pool: &PgPool) -> anyhow::Result<(&'static User, Rows)> {
29
3
    let user = USER.get().unwrap();
30
3
    user.commit().await?;
31
3
    Ok((user, seed(pool).await?))
32
3
}
33

            
34
3
async fn update(user: &User, tx: Uuid) -> Result<Option<CmdResult>, CmdError> {
35
3
    UpdateTransaction::new()
36
3
        .user_id(user.id)
37
3
        .transaction_id(tx)
38
3
        .note("changed".to_string())
39
3
        .run()
40
3
        .await
41
3
}
42

            
43
3
async fn delete(user: &User, tx: Uuid) -> Result<Option<CmdResult>, CmdError> {
44
3
    DeleteTransaction::new()
45
3
        .user_id(user.id)
46
3
        .transaction_id(tx)
47
3
        .run()
48
3
        .await
49
3
}
50

            
51
#[local_db_sqlx_test]
52
async fn update_of_either_transaction_is_refused(pool: PgPool) -> anyhow::Result<()> {
53
    let (user, rows) = user_with_rows(&pool).await?;
54

            
55
    assert_refused(update(user, rows.tx_a).await);
56
    assert_refused(update(user, rows.tx_b).await);
57

            
58
    assert_intact(&pool, &rows).await?;
59
    let sql = "SELECT count(*) FROM transaction_tags WHERE tx_id = $1";
60
    assert_eq!(count(&pool, sql, rows.tx_a).await?, 0);
61
    assert_eq!(count(&pool, sql, rows.tx_b).await?, 0);
62
}
63

            
64
#[local_db_sqlx_test]
65
async fn delete_of_either_transaction_is_refused(pool: PgPool) -> anyhow::Result<()> {
66
    let (user, rows) = user_with_rows(&pool).await?;
67

            
68
    assert_refused(delete(user, rows.tx_a).await);
69
    assert_refused(delete(user, rows.tx_b).await);
70

            
71
    assert_intact(&pool, &rows).await?;
72
}
73

            
74
#[local_db_sqlx_test]
75
async fn an_ordinary_price_still_updates_and_deletes(pool: PgPool) -> anyhow::Result<()> {
76
    let (user, rows) = user_with_rows(&pool).await?;
77
    let prices = "SELECT count(*) FROM prices WHERE id = $1";
78

            
79
    update(user, rows.tx_own).await?;
80
    assert_eq!(count(&pool, prices, rows.own_price).await?, 1);
81

            
82
    delete(user, rows.tx_own).await?;
83
    assert_eq!(count(&pool, prices, rows.own_price).await?, 0);
84
    let sql = "SELECT count(*) FROM transactions WHERE id = $1";
85
    assert_eq!(count(&pool, sql, rows.tx_own).await?, 0);
86
    assert_intact(&pool, &rows).await?;
87
}