1
//! Error codes and messages of `load-form` that the typed `load` must keep.
2

            
3
use sqlx::PgPool;
4
use uuid::Uuid;
5

            
6
use super::load_form;
7
use super::test_edit::{Fx, condition, fx};
8
use super::tests_edit_load::{leg, raw_transaction};
9

            
10
4
async fn rejected(fx: &Fx, name: &str, id: &str) -> anyhow::Result<(String, String)> {
11
4
    let err = load_form(fx.user, name, id)
12
4
        .await
13
        .err()
14
        .ok_or_else(|| anyhow::anyhow!("load was accepted"))?;
15
4
    let condition = condition(&err);
16
4
    Ok((condition.code, condition.message))
17
4
}
18

            
19
#[sqlx::test(migrator = "server::db::MIGRATOR")]
20
async fn malformed_id_is_an_args_error(pool: PgPool) -> anyhow::Result<()> {
21
    let _guard = server::db::install_test_pool(pool.clone());
22
    let fx = fx(&pool).await?;
23
    let (code, message) = rejected(&fx, "transaction-edit", "nope").await?;
24
    assert_eq!(code, "args");
25
    assert_eq!(message, "invalid transaction id 'nope'");
26
    Ok(())
27
}
28

            
29
#[sqlx::test(migrator = "server::db::MIGRATOR")]
30
async fn missing_transaction_is_a_not_found_error(pool: PgPool) -> anyhow::Result<()> {
31
    let _guard = server::db::install_test_pool(pool.clone());
32
    let fx = fx(&pool).await?;
33
    let id = Uuid::new_v4().to_string();
34
    let (code, message) = rejected(&fx, "transaction-edit", &id).await?;
35
    assert_eq!(code, "not-found");
36
    assert_eq!(message, "Transaction not found");
37
    Ok(())
38
}
39

            
40
#[sqlx::test(migrator = "server::db::MIGRATOR")]
41
async fn unknown_form_is_an_args_error(pool: PgPool) -> anyhow::Result<()> {
42
    let _guard = server::db::install_test_pool(pool.clone());
43
    let fx = fx(&pool).await?;
44
    let (code, message) = rejected(&fx, "nope", &Uuid::new_v4().to_string()).await?;
45
    assert_eq!(code, "args");
46
    assert_eq!(message, "unknown form 'nope'");
47
    Ok(())
48
}
49

            
50
#[sqlx::test(migrator = "server::db::MIGRATOR")]
51
async fn edit_refusal_is_an_args_error_with_its_reason(pool: PgPool) -> anyhow::Result<()> {
52
    let _guard = server::db::install_test_pool(pool.clone());
53
    let fx = fx(&pool).await?;
54
1
    let tx = raw_transaction(&fx, |tx| {
55
1
        vec![
56
1
            leg(&fx, tx, fx.cash, -100),
57
1
            leg(&fx, tx, fx.food, 60),
58
1
            leg(&fx, tx, fx.food, 40),
59
        ]
60
1
    })
61
    .await?;
62
    let (code, message) = rejected(&fx, "transaction-edit", &tx.to_string()).await?;
63
    assert_eq!(code, "args");
64
    assert!(message.contains("paired"), "{message}");
65
    Ok(())
66
}
67

            
68
#[sqlx::test(migrator = "server::db::MIGRATOR")]
69
async fn a_closed_pool_keeps_its_unavailable_condition(pool: PgPool) -> anyhow::Result<()> {
70
    let _guard = server::db::install_test_pool(pool.clone());
71
    let fx = fx(&pool).await?;
72
    pool.close().await;
73
    let err = load_form(fx.user, "transaction-edit", &Uuid::new_v4().to_string())
74
        .await
75
        .err()
76
        .ok_or_else(|| anyhow::anyhow!("load was accepted"))?;
77
    assert_eq!(
78
        crate::host_failure::condition_code(&err),
79
        Some(crate::envelope::ErrorCode::UNAVAILABLE),
80
        "{err}"
81
    );
82
    Ok(())
83
}