1
use super::*;
2

            
3
#[local_db_sqlx_test]
4
async fn test_edit_renames_reparents_and_replaces_tags(pool: PgPool) -> Result<(), anyhow::Error> {
5
    let fx = fixture().await?;
6
    let parent = fx.account("Parent", None).await?;
7
    let account = fx.account("Old", None).await?;
8
    fx.set_tag(account, "type", "asset").await?;
9
    fx.set_tag(account, "color", "red").await?;
10
    let revision = fx.revision(account).await?;
11

            
12
    fx.edit(
13
        account,
14
        "New",
15
        Some(parent),
16
        vec![tag("type", "bank"), tag("iban", "X1")],
17
        revision,
18
    )
19
    .await?;
20

            
21
    let stored = fx.stored(account).await?;
22
    assert_eq!(stored.parent, Some(parent));
23
    assert_eq!(
24
        pairs(&stored),
25
        [("iban", "X1"), ("name", "New"), ("type", "bank")]
26
    );
27
}
28

            
29
#[local_db_sqlx_test]
30
async fn test_edit_without_changes_is_a_no_op(pool: PgPool) -> Result<(), anyhow::Error> {
31
    let fx = fixture().await?;
32
    let account = fx.account("Cash", None).await?;
33
    fx.set_tag(account, "type", "asset").await?;
34
    let before = fx.stored(account).await?;
35
2
    let ids: Vec<Uuid> = before.tags.iter().map(|tag| tag.id).collect();
36

            
37
2
    let tags = before.tags.iter().filter(|t| t.tag_name != "name").cloned();
38
    fx.edit(account, "Cash", None, tags.collect(), before.revision())
39
        .await?;
40

            
41
    let after = fx.stored(account).await?;
42
    assert_eq!(after.revision(), before.revision());
43
2
    assert_eq!(after.tags.iter().map(|tag| tag.id).collect::<Vec<_>>(), ids);
44
}
45

            
46
#[local_db_sqlx_test]
47
async fn test_edit_without_tags_clears_them_but_keeps_the_name(
48
    pool: PgPool,
49
) -> Result<(), anyhow::Error> {
50
    let fx = fixture().await?;
51
    let account = fx.account("Cash", None).await?;
52
    fx.set_tag(account, "type", "asset").await?;
53
    let revision = fx.revision(account).await?;
54

            
55
    fx.edit(account, "Cash", None, vec![], revision).await?;
56

            
57
    assert_eq!(pairs(&fx.stored(account).await?), [("name", "Cash")]);
58
}
59

            
60
#[local_db_sqlx_test]
61
async fn test_edit_to_the_root_and_a_changed_description(
62
    pool: PgPool,
63
) -> Result<(), anyhow::Error> {
64
    let fx = fixture().await?;
65
    let parent = fx.account("Parent", None).await?;
66
    let account = fx.account("Child", Some(parent)).await?;
67
    fx.set_tag(account, "type", "asset").await?;
68
    let revision = fx.revision(account).await?;
69
    let described = Tag {
70
        description: Some("kind".to_string()),
71
        ..tag("type", "asset")
72
    };
73

            
74
    fx.edit(account, "Child", None, vec![described], revision)
75
        .await?;
76

            
77
    let stored = fx.stored(account).await?;
78
    assert_eq!(stored.parent, None);
79
2
    let kept = stored.tags.iter().find(|t| t.tag_name == "type");
80
1
    assert_eq!(kept.and_then(|t| t.description.as_deref()), Some("kind"));
81
}
82

            
83
#[local_db_sqlx_test]
84
async fn test_edit_keys_tags_by_their_trimmed_names(pool: PgPool) -> Result<(), anyhow::Error> {
85
    let fx = fixture().await?;
86
    let account = fx.account("Cash", None).await?;
87
    fx.set_tag(account, "type", "asset").await?;
88
    let revision = fx.revision(account).await?;
89

            
90
    fx.edit(
91
        account,
92
        "Cash",
93
        None,
94
        vec![tag(" type ", " liability ")],
95
        revision,
96
    )
97
    .await?;
98

            
99
    assert_eq!(
100
        pairs(&fx.stored(account).await?),
101
        [("name", "Cash"), ("type", "liability")]
102
    );
103
}
104

            
105
#[local_db_sqlx_test]
106
async fn test_edit_removes_and_replaces_tags_stored_with_padded_names(
107
    pool: PgPool,
108
) -> Result<(), anyhow::Error> {
109
    let fx = fixture().await?;
110
    let account = fx.account("Cash", None).await?;
111
    link_raw(&pool, account, " note", "x").await?;
112
    link_raw(&pool, account, "type ", "old").await?;
113
    let revision = fx.revision(account).await?;
114

            
115
    fx.edit(account, "Cash", None, vec![tag("type", "new")], revision)
116
        .await?;
117

            
118
    assert_eq!(
119
        pairs(&fx.stored(account).await?),
120
        [("name", "Cash"), ("type", "new")]
121
    );
122
}