1
use super::account::{CreateAccount, SetAccountTag};
2
use super::account_name::{account_name_notice, duplicate_name_notice};
3
use super::{CmdResult, FinanceEntity};
4
use crate::{db::install_test_pool, user::User};
5
use sqlx::PgPool;
6
use supp_macro::local_db_sqlx_test;
7
use tokio::sync::OnceCell;
8
use uuid::Uuid;
9

            
10
static USER: OnceCell<User> = OnceCell::const_new();
11

            
12
3
async fn setup() {
13
6
    USER.get_or_init(|| async { User { id: Uuid::new_v4() } })
14
3
        .await;
15
3
}
16

            
17
3
async fn user() -> anyhow::Result<&'static User> {
18
3
    let user = USER.get().ok_or_else(|| anyhow::anyhow!("no user"))?;
19
3
    user.commit().await?;
20
3
    Ok(user)
21
3
}
22

            
23
8
async fn account(user: &User, name: &str, parent: Option<Uuid>) -> anyhow::Result<Uuid> {
24
8
    let mut cmd = CreateAccount::new().name(name.to_string()).user_id(user.id);
25
8
    if let Some(parent) = parent {
26
3
        cmd = cmd.parent(parent);
27
5
    }
28
8
    match cmd.run().await? {
29
8
        Some(CmdResult::Entity(FinanceEntity::Account(account))) => Ok(account.id),
30
        other => anyhow::bail!("unexpected CreateAccount result {other:?}"),
31
    }
32
8
}
33

            
34
#[local_db_sqlx_test]
35
async fn test_a_twin_under_the_same_parent_gets_the_notice(
36
    pool: PgPool,
37
) -> Result<(), anyhow::Error> {
38
    let user = user().await?;
39
    let food = account(user, "Food", None).await?;
40
    let first = account(user, "Cafe", Some(food)).await?;
41
    assert_eq!(account_name_notice(user.id, first).await?, None);
42

            
43
    let twin = account(user, "Cafe", Some(food)).await?;
44

            
45
    assert_eq!(
46
        account_name_notice(user.id, twin).await?,
47
        Some(duplicate_name_notice("Cafe"))
48
    );
49
    assert_eq!(
50
        account_name_notice(user.id, first).await?,
51
        Some(duplicate_name_notice("Cafe"))
52
    );
53
}
54

            
55
#[local_db_sqlx_test]
56
async fn test_the_same_name_elsewhere_is_no_twin(pool: PgPool) -> Result<(), anyhow::Error> {
57
    let user = user().await?;
58
    let food = account(user, "Food", None).await?;
59
    account(user, "Cafe", None).await?;
60
    let nested = account(user, "Cafe", Some(food)).await?;
61

            
62
    assert_eq!(account_name_notice(user.id, nested).await?, None);
63
}
64

            
65
#[local_db_sqlx_test]
66
async fn test_a_rename_into_a_twin_gets_the_notice(pool: PgPool) -> Result<(), anyhow::Error> {
67
    let user = user().await?;
68
    account(user, "Cafe", None).await?;
69
    let bar = account(user, "Bar", None).await?;
70

            
71
    SetAccountTag::new()
72
        .user_id(user.id)
73
        .account_id(bar)
74
        .tag_name("name".to_string())
75
        .tag_value("Cafe".to_string())
76
        .run()
77
        .await?;
78

            
79
    assert_eq!(
80
        account_name_notice(user.id, bar).await?,
81
        Some(duplicate_name_notice("Cafe"))
82
    );
83
    assert_eq!(account_name_notice(user.id, Uuid::new_v4()).await?, None);
84
}