1
use super::account::CreateAccount;
2
use super::account::SetAccountTag;
3
use super::account_edit::EditAccount;
4
use super::{CmdError, CmdResult, FinanceEntity};
5
use crate::account_state::StoredAccount;
6
use crate::{db::install_test_pool, hierarchy::lock_hierarchy, user::User};
7
use finance::tag::Tag;
8
use sqlx::PgPool;
9
use supp_macro::local_db_sqlx_test;
10
use tokio::sync::OnceCell;
11
use uuid::Uuid;
12

            
13
mod concurrency;
14
mod edits;
15
mod refusals;
16

            
17
static USER: OnceCell<User> = OnceCell::const_new();
18

            
19
14
async fn setup() {
20
28
    USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
21
14
        .await;
22
14
}
23

            
24
struct Fixture {
25
    user: &'static User,
26
}
27

            
28
14
async fn fixture() -> anyhow::Result<Fixture> {
29
14
    let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
30
14
    user.commit().await?;
31
14
    Ok(Fixture { user })
32
14
}
33

            
34
10
fn tag(name: &str, value: &str) -> Tag {
35
10
    Tag {
36
10
        id: Uuid::new_v4(),
37
10
        tag_name: name.to_string(),
38
10
        tag_value: value.to_string(),
39
10
        description: None,
40
10
    }
41
10
}
42

            
43
impl Fixture {
44
18
    async fn account(&self, name: &str, parent: Option<Uuid>) -> anyhow::Result<Uuid> {
45
18
        let mut cmd = CreateAccount::new()
46
18
            .name(name.to_string())
47
18
            .user_id(self.user.id);
48
18
        if let Some(parent) = parent {
49
2
            cmd = cmd.parent(parent);
50
16
        }
51
18
        match cmd.run().await? {
52
18
            Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
53
            other => anyhow::bail!("unexpected CreateAccount result {other:?}"),
54
        }
55
18
    }
56

            
57
8
    async fn set_tag(&self, account: Uuid, name: &str, value: &str) -> anyhow::Result<()> {
58
8
        SetAccountTag::new()
59
8
            .user_id(self.user.id)
60
8
            .account_id(account)
61
8
            .tag_name(name.to_string())
62
8
            .tag_value(value.to_string())
63
8
            .run()
64
8
            .await?;
65
8
        Ok(())
66
8
    }
67

            
68
28
    async fn stored(&self, account: Uuid) -> anyhow::Result<StoredAccount> {
69
28
        self.user
70
28
            .account_snapshot(account)
71
28
            .await?
72
            .ok_or_else(|| anyhow::anyhow!("account {account} is gone"))
73
28
    }
74

            
75
15
    async fn revision(&self, account: Uuid) -> anyhow::Result<String> {
76
15
        Ok(self.stored(account).await?.revision())
77
15
    }
78

            
79
18
    async fn edit(
80
18
        &self,
81
18
        account: Uuid,
82
18
        name: &str,
83
18
        parent: Option<Uuid>,
84
18
        tags: Vec<Tag>,
85
18
        revision: String,
86
18
    ) -> Result<Option<CmdResult>, CmdError> {
87
18
        let mut cmd = EditAccount::new()
88
18
            .user_id(self.user.id)
89
18
            .account_id(account)
90
18
            .name(name.to_string())
91
18
            .tags(tags)
92
18
            .revision(revision);
93
18
        if let Some(parent) = parent {
94
5
            cmd = cmd.parent(parent);
95
13
        }
96
18
        cmd.run().await
97
18
    }
98
}
99

            
100
/// The stored `(name, value)` pairs of the account's tags.
101
7
fn pairs(stored: &StoredAccount) -> Vec<(&str, &str)> {
102
7
    stored
103
7
        .tags
104
7
        .iter()
105
14
        .map(|tag| (tag.tag_name.as_str(), tag.tag_value.as_str()))
106
        .collect()
107
7
}
108

            
109
/// Link a tag row whose name is stored with its padding.
110
2
async fn link_raw(pool: &PgPool, owner: Uuid, name: &str, value: &str) -> anyhow::Result<()> {
111
2
    let tag = Uuid::new_v4();
112
2
    sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, $2, $3)")
113
2
        .bind(tag)
114
2
        .bind(name)
115
2
        .bind(value)
116
2
        .execute(pool)
117
2
        .await?;
118
2
    sqlx::query("INSERT INTO account_tags (account_id, tag_id) VALUES ($1, $2)")
119
2
        .bind(owner)
120
2
        .bind(tag)
121
2
        .execute(pool)
122
2
        .await?;
123
2
    Ok(())
124
2
}