Lines
95 %
Functions
100 %
Branches
use super::*;
use crate::db::install_test_pool;
use sqlx::PgPool;
use supp_macro::local_db_sqlx_test;
async fn setup() {}
async fn record(pool: &PgPool, kind: &str, subjects: &[Uuid], detail: &str) -> anyhow::Result<()> {
sqlx::query("INSERT INTO data_findings (kind, subjects, detail) VALUES ($1, $2, $3)")
.bind(kind)
.bind(subjects)
.bind(detail)
.execute(pool)
.await?;
Ok(())
}
async fn list(kind: Option<&str>) -> anyhow::Result<Vec<DataFinding>> {
let mut command = ListDataFindings::new();
if let Some(kind) = kind {
command = command.kind(kind.to_string());
match command.run().await? {
Some(CmdResult::DataFindings(findings)) => Ok(findings),
other => anyhow::bail!("expected DataFindings, got {other:?}"),
#[local_db_sqlx_test]
async fn empty_table_lists_nothing(pool: PgPool) -> anyhow::Result<()> {
assert!(list(None).await?.is_empty());
assert!(list(Some("tag-conflict")).await?.is_empty());
async fn lists_every_kind_grouped(pool: PgPool) -> anyhow::Result<()> {
let (a, b) = (Uuid::new_v4(), Uuid::new_v4());
record(&pool, "price-link", &[a], "price spans transactions").await?;
record(&pool, "tag-conflict", &[a, b], "descriptions differ").await?;
record(&pool, "price-link", &[b], "second price").await?;
let findings = list(None).await?;
let kinds: Vec<&str> = findings.iter().map(|f| f.kind.as_str()).collect();
assert_eq!(kinds, ["price-link", "price-link", "tag-conflict"]);
assert_eq!(findings[2].subjects, vec![a, b]);
assert_eq!(findings[2].detail, "descriptions differ");
async fn filters_by_kind(pool: PgPool) -> anyhow::Result<()> {
let a = Uuid::new_v4();
record(&pool, "tag-conflict", &[a], "descriptions differ").await?;
let findings = list(Some("tag-conflict")).await?;
assert_eq!(findings.len(), 1);
assert_eq!(findings[0].kind, "tag-conflict");
assert!(list(Some("unknown-kind")).await?.is_empty());
async fn blank_kind_is_refused(pool: PgPool) -> anyhow::Result<()> {
assert!(record(&pool, " ", &[], "no kind").await.is_err());