1
use forms::FieldError;
2

            
3
use super::{Entity, Failure};
4

            
5
1
fn invalid() -> Failure {
6
1
    Failure::Invalid(vec![FieldError {
7
1
        field: "date".to_string(),
8
1
        message: "bad".to_string(),
9
1
    }])
10
1
}
11

            
12
#[test]
13
1
fn every_cause_has_its_status() {
14
1
    let causes = [
15
1
        (
16
1
            Failure::MalformedId {
17
1
                entity: Entity::Transaction,
18
1
                id: "x".to_string(),
19
1
            },
20
1
            400,
21
1
        ),
22
1
        (Failure::UnknownForm("x".to_string()), 400),
23
1
        (Failure::Missing(Entity::Transaction), 404),
24
1
        (Failure::Missing(Entity::Template), 404),
25
1
        (invalid(), 422),
26
1
        (Failure::Refused("no".to_string()), 422),
27
1
        (Failure::Internal(wasmtime::Error::msg("db")), 500),
28
1
    ];
29
1
    for (failure, status) in causes {
30
7
        assert_eq!(failure.http_status(), status, "{failure}");
31
    }
32
1
}
33

            
34
5
pub(crate) fn every_command_failure() -> Vec<server::command::CmdError> {
35
    use finance::error::TransactionError;
36
    use server::command::CmdError;
37
    use server::db::DBError;
38
    use server::error::ServerError;
39
    use uuid::Uuid;
40

            
41
5
    vec![
42
5
        CmdError::Args("a".into()),
43
5
        CmdError::NoPrice {
44
5
            from: Uuid::nil(),
45
5
            to: Uuid::nil(),
46
5
        },
47
5
        CmdError::NotFound("n".into()),
48
5
        CmdError::Unkeepable("u".into()),
49
5
        CmdError::Refused("r".into()),
50
5
        CmdError::ParentGone(Uuid::nil()),
51
5
        CmdError::Disabled,
52
5
        CmdError::Script("s".into()),
53
5
        CmdError::Config(server::config::ConfigError::NoConfig("k".into())),
54
5
        CmdError::DB(sqlx::Error::PoolTimedOut),
55
5
        CmdError::DB(sqlx::Error::RowNotFound),
56
5
        CmdError::Server(ServerError::Lock),
57
5
        CmdError::Server(ServerError::Creation),
58
5
        CmdError::Server(ServerError::PasswordHash),
59
5
        CmdError::Server(ServerError::Finance(TransactionError::ZeroSum.into())),
60
5
        CmdError::Server(ServerError::TagClash {
61
5
            name: "n".into(),
62
5
            value: "v".into(),
63
5
        }),
64
5
        CmdError::Server(DBError::WriteConflict.into()),
65
5
        CmdError::Finance(TransactionError::ZeroSum.into()),
66
5
        CmdError::Finance(
67
5
            finance::error::ReportError::MissingConversion {
68
5
                from_commodity: "JPY".into(),
69
5
                to_commodity: "USD".into(),
70
5
            }
71
5
            .into(),
72
5
        ),
73
5
        CmdError::Server(ServerError::Finance(
74
5
            finance::error::BalanceError::MissingConversion {
75
5
                split_id: Uuid::nil(),
76
5
                from_commodity: "JPY".into(),
77
5
                to_commodity: "USD".into(),
78
5
            }
79
5
            .into(),
80
5
        )),
81
5
        CmdError::Finance(finance::error::FinanceError::Internal("x".into())),
82
    ]
83
5
}
84

            
85
#[test]
86
1
fn a_command_failure_keeps_its_status_through_the_form_failure() {
87
    use crate::status::cmd_status;
88

            
89
1
    for cause in every_command_failure() {
90
21
        let expected = cmd_status(&cause);
91
21
        let label = cause.to_string();
92
21
        assert_eq!(
93
21
            Failure::from_command("ctx", cause).http_status(),
94
21
            expected,
95
            "{label}"
96
        );
97
    }
98
1
}
99

            
100
#[test]
101
1
fn the_report_mapper_keeps_the_status_of_every_command_failure() {
102
    use crate::status::cmd_status;
103

            
104
1
    for cause in every_command_failure() {
105
21
        let expected = cmd_status(&cause);
106
21
        let label = cause.to_string();
107
21
        assert_eq!(
108
21
            super::report::command_failure(cause).http_status(),
109
21
            expected,
110
            "{label}"
111
        );
112
    }
113
1
}