1
use super::*;
2
use rpc::TransactionDraft;
3
use rpc::forms::FormOption;
4
use uuid::Uuid;
5

            
6
4
fn option(id: Uuid, label: &str) -> FormOption {
7
4
    FormOption {
8
4
        id,
9
4
        label: label.to_string(),
10
4
        alias: String::new(),
11
4
        path: String::new(),
12
4
        commodity: None,
13
4
    }
14
4
}
15

            
16
4
fn split(account: Uuid, commodity: Uuid, num: i64) -> DraftSplit {
17
4
    DraftSplit {
18
4
        account_id: account.to_string(),
19
4
        commodity_id: commodity.to_string(),
20
4
        value_num: num,
21
4
        value_denom: 1,
22
4
        tags: vec![DraftTag {
23
4
            name: "memo".into(),
24
4
            value: "x".into(),
25
4
        }],
26
4
    }
27
4
}
28

            
29
#[test]
30
2
fn a_draft_view_labels_the_accounts_and_commodities() {
31
2
    let (account, commodity, unknown) = (Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4());
32
2
    let references = References {
33
2
        accounts: vec![option(account, "Assets:Checking")],
34
2
        commodities: vec![option(commodity, "USD")],
35
2
        ..References::default()
36
2
    };
37
2
    let tested = TemplateDraft {
38
2
        draft: TransactionDraft {
39
2
            note: Some("Groceries".into()),
40
2
            date: Some("15th of June".into()),
41
2
            splits: vec![
42
2
                split(account, commodity, -50),
43
2
                split(unknown, commodity, 50),
44
2
            ],
45
2
            tags: vec![DraftTag {
46
2
                name: "category".into(),
47
2
                value: "food".into(),
48
2
            }],
49
2
        },
50
2
        refusal: Some("it has an odd number of splits".into()),
51
2
    };
52

            
53
2
    let shown = view(tested, &references);
54

            
55
2
    assert_eq!(shown.splits[0].account, "Assets:Checking");
56
2
    assert_eq!(shown.splits[0].commodity, "USD");
57
2
    assert_eq!(shown.splits[0].amount, "-50");
58
2
    assert_eq!(shown.splits[0].tags, ["memo=x"]);
59
2
    assert_eq!(shown.splits[1].account, unknown.to_string());
60
2
    assert_eq!(shown.tags, ["category=food"]);
61
2
    assert_eq!(shown.date.as_deref(), Some("15th of June"));
62
2
    assert_eq!(
63
2
        shown.reason.as_deref(),
64
2
        Some("it has an odd number of splits")
65
    );
66
2
}
67

            
68
#[test]
69
2
fn an_invalid_answer_names_the_field_and_retargets_the_result() {
70
2
    let response = invalid(vec![FieldError {
71
2
        field: "source".into(),
72
2
        message: "unclosed".into(),
73
2
    }])
74
2
    .expect("renders");
75

            
76
2
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
77
2
    assert_eq!(
78
2
        response.headers().get("hx-retarget"),
79
2
        Some(&HeaderValue::from_static(RESULT_TARGET))
80
    );
81
2
}