1
use super::commodity::CreateCommodity;
2
use super::commodity_edit::EditCommodity;
3
use super::commodity_tag::SetCommodityTag;
4
use super::{CmdError, CmdResult};
5
use crate::commodity_state::StoredCommodity;
6
use crate::{db::install_test_pool, 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
10
async fn setup() {
20
20
    USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
21
10
        .await;
22
10
}
23

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

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

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

            
43
impl Fixture {
44
9
    async fn commodity(&self, symbol: &str, name: &str) -> anyhow::Result<Uuid> {
45
9
        let created = CreateCommodity::new()
46
9
            .symbol(symbol.to_string())
47
9
            .name(name.to_string())
48
9
            .user_id(self.user.id)
49
9
            .run()
50
9
            .await?;
51
9
        match created {
52
9
            Some(CmdResult::String(id)) => Ok(Uuid::parse_str(&id)?),
53
            other => anyhow::bail!("unexpected CreateCommodity result {other:?}"),
54
        }
55
9
    }
56

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

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

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

            
79
15
    async fn edit(
80
15
        &self,
81
15
        commodity: Uuid,
82
15
        (symbol, name): (&str, &str),
83
15
        tags: Vec<Tag>,
84
15
        revision: String,
85
15
    ) -> Result<Option<CmdResult>, CmdError> {
86
15
        EditCommodity::new()
87
15
            .user_id(self.user.id)
88
15
            .commodity_id(commodity)
89
15
            .symbol(symbol.to_string())
90
15
            .name(name.to_string())
91
15
            .tags(tags)
92
15
            .revision(revision)
93
15
            .run()
94
15
            .await
95
15
    }
96
}
97

            
98
/// The stored `(name, value)` pairs of the commodity's tags.
99
6
fn pairs(stored: &StoredCommodity) -> Vec<(&str, &str)> {
100
6
    stored
101
6
        .tags
102
6
        .iter()
103
17
        .map(|tag| (tag.tag_name.as_str(), tag.tag_value.as_str()))
104
        .collect()
105
6
}
106

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