1
use super::tests::parse;
2
use super::*;
3

            
4
2
fn from_template(args: &[&str]) -> anyhow::Result<(Uuid, FormValues)> {
5
2
    match parse(args)? {
6
        TransactionCmd::CreateFromTemplate {
7
            template,
8
            overrides,
9
2
        } => Ok((template, overrides)),
10
        other => anyhow::bail!("expected create from template, got {other:?}"),
11
    }
12
2
}
13

            
14
#[test]
15
1
fn template_names_the_draft_to_start_from() -> anyhow::Result<()> {
16
1
    let id = Uuid::new_v4();
17

            
18
1
    let (template, overrides) = from_template(&["create", "--template", &id.to_string()])?;
19

            
20
1
    assert_eq!(template, id);
21
1
    assert_eq!(overrides, FormValues::default());
22
1
    Ok(())
23
1
}
24

            
25
#[test]
26
1
fn the_other_flags_override_the_draft() -> anyhow::Result<()> {
27
1
    let id = Uuid::new_v4();
28

            
29
1
    let (_, overrides) = from_template(&[
30
1
        "create",
31
1
        "--template",
32
1
        &id.to_string(),
33
1
        "--note",
34
1
        "Dinner",
35
1
        "--date",
36
1
        "2026-07-01",
37
1
        "--row",
38
1
        "from=Cash,to=Food,from-commodity=USD,value=1",
39
1
    ])?;
40

            
41
1
    assert_eq!(overrides.text("note"), Some("Dinner"));
42
1
    assert_eq!(overrides.text("date"), Some("2026-07-01"));
43
1
    assert_eq!(overrides.rows("splits").map(<[_]>::len), Some(1));
44
1
    Ok(())
45
1
}
46

            
47
#[test]
48
1
fn without_a_template_the_date_still_defaults_to_now() -> anyhow::Result<()> {
49
1
    assert!(matches!(
50
1
        parse(&["create", "--note", "x"])?,
51
1
        TransactionCmd::Create(values) if values.text("date").is_some()
52
    ));
53
1
    Ok(())
54
1
}
55

            
56
#[test]
57
1
fn a_template_that_is_not_an_id_is_a_usage_error() {
58
1
    assert!(parse(&["create", "--template", "lunch"]).is_err());
59
1
}