1
use super::{KIND, fixture::seed, run};
2
use crate::db::install_test_pool;
3
use sqlx::PgPool;
4
use sqlx::types::Uuid;
5
use supp_macro::local_db_sqlx_test;
6

            
7
3
async fn setup() {}
8

            
9
4
async fn findings(pool: &PgPool) -> anyhow::Result<Vec<(String, Vec<Uuid>, String)>> {
10
    Ok(
11
4
        sqlx::query_as("SELECT kind, subjects, detail FROM data_findings ORDER BY kind, subjects")
12
4
            .fetch_all(pool)
13
4
            .await?,
14
    )
15
4
}
16

            
17
4
async fn job(pool: &PgPool) -> anyhow::Result<()> {
18
4
    let mut conn = pool.acquire().await?;
19
4
    run(&mut conn).await?;
20
4
    Ok(())
21
4
}
22

            
23
#[local_db_sqlx_test]
24
async fn a_cross_transaction_price_is_recorded_once(pool: PgPool) -> anyhow::Result<()> {
25
    let rows = seed(&pool).await?;
26

            
27
    job(&pool).await?;
28

            
29
    let recorded = findings(&pool).await?;
30
    assert_eq!(recorded.len(), 1);
31
    let (kind, subjects, detail) = &recorded[0];
32
    assert_eq!(kind, KIND);
33
    assert_eq!(subjects, &[rows.cross_price, rows.tx_a, rows.tx_b]);
34
    assert!(detail.contains("price id"));
35
}
36

            
37
#[local_db_sqlx_test]
38
async fn a_rerun_records_nothing_new(pool: PgPool) -> anyhow::Result<()> {
39
    seed(&pool).await?;
40

            
41
    job(&pool).await?;
42
    let first = findings(&pool).await?;
43
    job(&pool).await?;
44

            
45
    assert_eq!(findings(&pool).await?, first);
46
}
47

            
48
#[local_db_sqlx_test]
49
async fn an_ordinary_price_is_not_recorded(pool: PgPool) -> anyhow::Result<()> {
50
    let rows = seed(&pool).await?;
51
    sqlx::query("DELETE FROM prices WHERE id = $1")
52
        .bind(rows.cross_price)
53
        .execute(&pool)
54
        .await?;
55

            
56
    job(&pool).await?;
57

            
58
    assert!(findings(&pool).await?.is_empty());
59
}