1
use super::*;
2

            
3
const PADDED: &str = "note\t";
4

            
5
3
async fn link_padded_note(pool: &PgPool, tx_id: Uuid, value: &str) -> anyhow::Result<()> {
6
3
    let tag_id = Uuid::new_v4();
7
3
    sqlx::query("INSERT INTO tags (id, tag_name, tag_value) VALUES ($1, $2, $3)")
8
3
        .bind(tag_id)
9
3
        .bind(PADDED)
10
3
        .bind(value)
11
3
        .execute(pool)
12
3
        .await?;
13
3
    sqlx::query("INSERT INTO transaction_tags (tx_id, tag_id) VALUES ($1, $2)")
14
3
        .bind(tx_id)
15
3
        .bind(tag_id)
16
3
        .execute(pool)
17
3
        .await?;
18
3
    Ok(())
19
3
}
20

            
21
#[local_db_sqlx_test]
22
async fn a_new_note_replaces_a_padded_stored_note(pool: PgPool) -> anyhow::Result<()> {
23
    let fx = fixture().await?;
24
    let tx_id = fx.create_tx(None).await?;
25
    link_padded_note(&pool, tx_id, "old").await?;
26

            
27
    UpdateTransaction::new()
28
        .user_id(fx.user.id)
29
        .transaction_id(tx_id)
30
        .note("new".to_string())
31
        .run()
32
        .await?;
33

            
34
    assert_eq!(fx.tx_tags(tx_id).await?, vec![pair("note", "new")]);
35
}
36

            
37
#[local_db_sqlx_test]
38
async fn a_blank_note_removes_a_padded_stored_note(pool: PgPool) -> anyhow::Result<()> {
39
    let fx = fixture().await?;
40
    let tx_id = fx.create_tx(None).await?;
41
    link_padded_note(&pool, tx_id, "old").await?;
42

            
43
    UpdateTransaction::new()
44
        .user_id(fx.user.id)
45
        .transaction_id(tx_id)
46
        .note(" ".to_string())
47
        .run()
48
        .await?;
49

            
50
    assert!(fx.tx_tags(tx_id).await?.is_empty());
51
}
52

            
53
#[local_db_sqlx_test]
54
async fn replacing_the_tags_keeps_a_padded_stored_note(pool: PgPool) -> anyhow::Result<()> {
55
    let fx = fixture().await?;
56
    let tx_id = fx.create_tx(None).await?;
57
    link_padded_note(&pool, tx_id, "kept").await?;
58

            
59
    UpdateTransaction::new()
60
        .user_id(fx.user.id)
61
        .transaction_id(tx_id)
62
        .tags(tag_map(&[("cat", "food")]))
63
        .run()
64
        .await?;
65

            
66
    assert_eq!(
67
        fx.tx_tags(tx_id).await?,
68
        vec![pair("cat", "food"), pair(PADDED, "kept")]
69
    );
70
}