1
use super::*;
2

            
3
#[local_db_sqlx_test]
4
async fn test_edit_replaces_symbol_name_and_tags(pool: PgPool) -> Result<(), anyhow::Error> {
5
    let fx = fixture().await?;
6
    let commodity = fx.commodity("OLD", "Old name").await?;
7
    fx.set_tag(commodity, "kind", "stock").await?;
8
    fx.set_tag(commodity, "color", "red").await?;
9
    let revision = fx.revision(commodity).await?;
10

            
11
    fx.edit(
12
        commodity,
13
        ("NEW", "New name"),
14
        vec![tag("kind", "bond"), tag("isin", "X1")],
15
        revision,
16
    )
17
    .await?;
18

            
19
    assert_eq!(
20
        pairs(&fx.stored(commodity).await?),
21
        [
22
            ("isin", "X1"),
23
            ("kind", "bond"),
24
            ("name", "New name"),
25
            ("symbol", "NEW")
26
        ]
27
    );
28
}
29

            
30
#[local_db_sqlx_test]
31
async fn test_edit_without_changes_is_a_no_op(pool: PgPool) -> Result<(), anyhow::Error> {
32
    let fx = fixture().await?;
33
    let commodity = fx.commodity("USD", "Dollar").await?;
34
    fx.set_tag(commodity, "kind", "fiat").await?;
35
    let before = fx.stored(commodity).await?;
36
3
    let ids: Vec<Uuid> = before.tags.iter().map(|tag| tag.id).collect();
37

            
38
3
    let others = before.tags.iter().filter(|t| t.tag_name == "kind").cloned();
39
    fx.edit(
40
        commodity,
41
        ("USD", "Dollar"),
42
        others.collect(),
43
        before.revision(),
44
    )
45
    .await?;
46

            
47
    let after = fx.stored(commodity).await?;
48
    assert_eq!(after.revision(), before.revision());
49
3
    assert_eq!(after.tags.iter().map(|tag| tag.id).collect::<Vec<_>>(), ids);
50
}
51

            
52
#[local_db_sqlx_test]
53
async fn test_edit_without_tags_clears_them_but_keeps_symbol_and_name(
54
    pool: PgPool,
55
) -> Result<(), anyhow::Error> {
56
    let fx = fixture().await?;
57
    let commodity = fx.commodity("USD", "Dollar").await?;
58
    fx.set_tag(commodity, "kind", "fiat").await?;
59
    let revision = fx.revision(commodity).await?;
60

            
61
    fx.edit(commodity, ("USD", "Dollar"), vec![], revision)
62
        .await?;
63

            
64
    assert_eq!(
65
        pairs(&fx.stored(commodity).await?),
66
        [("name", "Dollar"), ("symbol", "USD")]
67
    );
68
}
69

            
70
#[local_db_sqlx_test]
71
async fn test_edit_changes_a_description(pool: PgPool) -> Result<(), anyhow::Error> {
72
    let fx = fixture().await?;
73
    let commodity = fx.commodity("USD", "Dollar").await?;
74
    fx.set_tag(commodity, "kind", "fiat").await?;
75
    let revision = fx.revision(commodity).await?;
76
    let described = Tag {
77
        description: Some("note".to_string()),
78
        ..tag("kind", "fiat")
79
    };
80

            
81
    fx.edit(commodity, ("USD", "Dollar"), vec![described], revision)
82
        .await?;
83

            
84
    let stored = fx.stored(commodity).await?;
85
1
    let kept = stored.tags.iter().find(|t| t.tag_name == "kind");
86
1
    assert_eq!(kept.and_then(|t| t.description.as_deref()), Some("note"));
87
}
88

            
89
#[local_db_sqlx_test]
90
async fn test_edit_trims_the_symbol_name_and_tag_keys(pool: PgPool) -> Result<(), anyhow::Error> {
91
    let fx = fixture().await?;
92
    let commodity = fx.commodity("USD", "Dollar").await?;
93
    fx.set_tag(commodity, "kind", "fiat").await?;
94
    let revision = fx.revision(commodity).await?;
95

            
96
    fx.edit(
97
        commodity,
98
        (" EUR ", " Euro "),
99
        vec![tag(" kind ", " coin ")],
100
        revision,
101
    )
102
    .await?;
103

            
104
    assert_eq!(
105
        pairs(&fx.stored(commodity).await?),
106
        [("kind", "coin"), ("name", "Euro"), ("symbol", "EUR")]
107
    );
108
}
109

            
110
#[local_db_sqlx_test]
111
async fn test_edit_removes_and_replaces_tags_stored_with_padded_names(
112
    pool: PgPool,
113
) -> Result<(), anyhow::Error> {
114
    let fx = fixture().await?;
115
    let commodity = fx.commodity("USD", "Dollar").await?;
116
    link_raw(&pool, commodity, " note", "x").await?;
117
    link_raw(&pool, commodity, "symbol ", "OLD").await?;
118
    let revision = fx.revision(commodity).await?;
119

            
120
    fx.edit(commodity, ("EUR", "Dollar"), vec![], revision)
121
        .await?;
122

            
123
    assert_eq!(
124
        pairs(&fx.stored(commodity).await?),
125
        [("name", "Dollar"), ("symbol", "EUR")]
126
    );
127
}