1
use super::*;
2
use crate::db::install_test_pool;
3
use sqlx::PgPool;
4
use supp_macro::local_db_sqlx_test;
5

            
6
4
async fn setup() {}
7

            
8
6
async fn record(pool: &PgPool, kind: &str, subjects: &[Uuid], detail: &str) -> anyhow::Result<()> {
9
6
    sqlx::query("INSERT INTO data_findings (kind, subjects, detail) VALUES ($1, $2, $3)")
10
6
        .bind(kind)
11
6
        .bind(subjects)
12
6
        .bind(detail)
13
6
        .execute(pool)
14
6
        .await?;
15
5
    Ok(())
16
6
}
17

            
18
5
async fn list(kind: Option<&str>) -> anyhow::Result<Vec<DataFinding>> {
19
5
    let mut command = ListDataFindings::new();
20
5
    if let Some(kind) = kind {
21
3
        command = command.kind(kind.to_string());
22
2
    }
23
5
    match command.run().await? {
24
5
        Some(CmdResult::DataFindings(findings)) => Ok(findings),
25
        other => anyhow::bail!("expected DataFindings, got {other:?}"),
26
    }
27
5
}
28

            
29
#[local_db_sqlx_test]
30
async fn empty_table_lists_nothing(pool: PgPool) -> anyhow::Result<()> {
31
    assert!(list(None).await?.is_empty());
32
    assert!(list(Some("tag-conflict")).await?.is_empty());
33
}
34

            
35
#[local_db_sqlx_test]
36
async fn lists_every_kind_grouped(pool: PgPool) -> anyhow::Result<()> {
37
    let (a, b) = (Uuid::new_v4(), Uuid::new_v4());
38
    record(&pool, "price-link", &[a], "price spans transactions").await?;
39
    record(&pool, "tag-conflict", &[a, b], "descriptions differ").await?;
40
    record(&pool, "price-link", &[b], "second price").await?;
41

            
42
    let findings = list(None).await?;
43
3
    let kinds: Vec<&str> = findings.iter().map(|f| f.kind.as_str()).collect();
44
    assert_eq!(kinds, ["price-link", "price-link", "tag-conflict"]);
45
    assert_eq!(findings[2].subjects, vec![a, b]);
46
    assert_eq!(findings[2].detail, "descriptions differ");
47
}
48

            
49
#[local_db_sqlx_test]
50
async fn filters_by_kind(pool: PgPool) -> anyhow::Result<()> {
51
    let a = Uuid::new_v4();
52
    record(&pool, "price-link", &[a], "price spans transactions").await?;
53
    record(&pool, "tag-conflict", &[a], "descriptions differ").await?;
54

            
55
    let findings = list(Some("tag-conflict")).await?;
56
    assert_eq!(findings.len(), 1);
57
    assert_eq!(findings[0].kind, "tag-conflict");
58
    assert!(list(Some("unknown-kind")).await?.is_empty());
59
}
60

            
61
#[local_db_sqlx_test]
62
async fn blank_kind_is_refused(pool: PgPool) -> anyhow::Result<()> {
63
    assert!(record(&pool, "  ", &[], "no kind").await.is_err());
64
}