1
use forms::ACCOUNT_EDIT;
2
use sqlx::PgPool;
3

            
4
use super::test_account_edit::{loaded, rebuilt, seeded, set_tag, tag_row};
5
use super::test_seed::seed_account;
6
use crate::forms::submit;
7
use crate::resolve::Directory;
8

            
9
4
async fn path_of(user: uuid::Uuid, account: uuid::Uuid) -> anyhow::Result<String> {
10
4
    Ok(Directory::load("test", user).await?.account_path(account))
11
4
}
12

            
13
3
async fn tag_pairs(user: uuid::Uuid, account: uuid::Uuid) -> anyhow::Result<Vec<(String, String)>> {
14
3
    let values = loaded(user, account).await?;
15
3
    Ok(values
16
3
        .rows("tags")
17
3
        .unwrap_or_default()
18
3
        .iter()
19
4
        .map(|row| (row["name"].clone(), row["value"].clone()))
20
        .collect())
21
3
}
22

            
23
#[sqlx::test(migrator = "server::db::MIGRATOR")]
24
async fn rename_changes_the_name_and_keeps_the_tags(pool: PgPool) -> anyhow::Result<()> {
25
    let _guard = server::db::install_test_pool(pool.clone());
26
    let fx = seeded(&pool).await?;
27
    let mut values = loaded(fx.user, fx.account).await?;
28
    values.set("name", "  Wallet ");
29

            
30
    let submitted = submit(fx.user, &ACCOUNT_EDIT, &values).await?;
31

            
32
    assert_eq!(submitted.id, fx.account);
33
    assert_eq!(path_of(fx.user, fx.account).await?, "Assets:Wallet");
34
    assert_eq!(tag_pairs(fx.user, fx.account).await?.len(), 2);
35
    Ok(())
36
}
37

            
38
#[sqlx::test(migrator = "server::db::MIGRATOR")]
39
async fn reparent_moves_to_another_parent_and_to_the_root(pool: PgPool) -> anyhow::Result<()> {
40
    let _guard = server::db::install_test_pool(pool.clone());
41
    let fx = seeded(&pool).await?;
42
    let other = seed_account(fx.user, "Other").await?;
43
    let mut values = loaded(fx.user, fx.account).await?;
44
    values.set("parent", &other.to_string());
45
    submit(fx.user, &ACCOUNT_EDIT, &values).await?;
46
    assert_eq!(path_of(fx.user, fx.account).await?, "Other:Cash");
47

            
48
    let mut values = loaded(fx.user, fx.account).await?;
49
    values.set("parent", "");
50
    submit(fx.user, &ACCOUNT_EDIT, &values).await?;
51

            
52
    assert_eq!(path_of(fx.user, fx.account).await?, "Cash");
53
    Ok(())
54
}
55

            
56
#[sqlx::test(migrator = "server::db::MIGRATOR")]
57
async fn tags_are_added_changed_and_removed(pool: PgPool) -> anyhow::Result<()> {
58
    let _guard = server::db::install_test_pool(pool.clone());
59
    let fx = seeded(&pool).await?;
60
    let values = loaded(fx.user, fx.account).await?;
61
    let edited = rebuilt(
62
        &values,
63
        vec![tag_row("type", "bank"), tag_row("iban", "X1")],
64
    );
65

            
66
    submit(fx.user, &ACCOUNT_EDIT, &edited).await?;
67

            
68
    let pairs = tag_pairs(fx.user, fx.account).await?;
69
    let expected = [("iban", "X1"), ("type", "bank")]
70
2
        .map(|(name, value)| (name.to_string(), value.to_string()));
71
    assert_eq!(pairs, expected);
72
    Ok(())
73
}
74

            
75
#[sqlx::test(migrator = "server::db::MIGRATOR")]
76
async fn an_empty_tags_list_clears_every_tag(pool: PgPool) -> anyhow::Result<()> {
77
    let _guard = server::db::install_test_pool(pool.clone());
78
    let fx = seeded(&pool).await?;
79
    let values = loaded(fx.user, fx.account).await?;
80

            
81
    submit(fx.user, &ACCOUNT_EDIT, &rebuilt(&values, vec![])).await?;
82

            
83
    assert!(tag_pairs(fx.user, fx.account).await?.is_empty());
84
    assert_eq!(path_of(fx.user, fx.account).await?, "Assets:Cash");
85
    Ok(())
86
}
87

            
88
#[sqlx::test(migrator = "server::db::MIGRATOR")]
89
async fn a_second_submit_with_the_same_revision_is_stale_after_a_change(
90
    pool: PgPool,
91
) -> anyhow::Result<()> {
92
    let _guard = server::db::install_test_pool(pool.clone());
93
    let fx = seeded(&pool).await?;
94
    let values = loaded(fx.user, fx.account).await?;
95
    submit(fx.user, &ACCOUNT_EDIT, &values).await?;
96
    set_tag(fx.user, fx.account, "type", "other").await?;
97

            
98
    let again = super::test_account_edit::invalid_fields(fx.user, &values).await?;
99

            
100
    assert_eq!(again, ["revision"]);
101
    Ok(())
102
}