Files
biggus-dickus/crates/db/tests/vocab.rs
T

172 lines
4.7 KiB
Rust

use db::{Db, vocab};
use domain::{AuditActor, LocalizedLabel, NewTerm};
use sqlx::PgPool;
#[sqlx::test]
async fn vocabulary_create_and_lookup(pool: PgPool) {
let db = Db::from_pool(pool);
let mut tx = db.pool().begin().await.unwrap();
let v = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap();
tx.commit().await.unwrap();
let found = vocab::vocabulary_by_key(db.pool(), "material")
.await
.unwrap()
.unwrap();
assert_eq!(found.id, v.id);
assert_eq!(found.key, "material");
assert!(
vocab::vocabulary_by_key(db.pool(), "nope")
.await
.unwrap()
.is_none()
);
}
#[sqlx::test]
async fn term_with_multilingual_labels_round_trips(pool: PgPool) {
let db = Db::from_pool(pool);
let mut tx = db.pool().begin().await.unwrap();
let v = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let term_id = vocab::add_term(
&mut tx,
AuditActor::System,
&NewTerm {
vocabulary_id: v.id,
external_uri: Some("http://vocab.getty.edu/aat/300011914".into()),
labels: vec![
LocalizedLabel {
lang: "sv".into(),
label: "trä".into(),
},
LocalizedLabel {
lang: "en".into(),
label: "wood".into(),
},
],
},
)
.await
.unwrap();
tx.commit().await.unwrap();
let term = vocab::term_by_id(db.pool(), term_id)
.await
.unwrap()
.unwrap();
assert_eq!(term.vocabulary_id, v.id);
assert_eq!(
term.external_uri.as_deref(),
Some("http://vocab.getty.edu/aat/300011914")
);
assert_eq!(term.labels.len(), 2);
assert_eq!(domain::pick_label(&term.labels, "sv", "en"), Some("trä"));
assert_eq!(domain::pick_label(&term.labels, "de", "en"), Some("wood"));
let listed = vocab::list_terms(db.pool(), v.id).await.unwrap();
assert_eq!(listed.len(), 1);
assert_eq!(listed[0].id, term_id);
}
#[sqlx::test]
async fn term_with_no_labels_round_trips_empty(pool: PgPool) {
let db = Db::from_pool(pool);
let mut tx = db.pool().begin().await.unwrap();
let v = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let term_id = vocab::add_term(
&mut tx,
AuditActor::System,
&NewTerm {
vocabulary_id: v.id,
external_uri: None,
labels: vec![],
},
)
.await
.unwrap();
tx.commit().await.unwrap();
let term = vocab::term_by_id(db.pool(), term_id)
.await
.unwrap()
.unwrap();
assert!(term.labels.is_empty());
}
#[sqlx::test]
async fn duplicate_vocabulary_key_is_rejected(pool: PgPool) {
let db = Db::from_pool(pool);
let mut tx = db.pool().begin().await.unwrap();
vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let err = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap_err();
assert!(
matches!(err, sqlx::Error::Database(_)),
"expected a unique-violation DB error, got: {err:?}"
);
}
#[sqlx::test]
async fn resolve_term_checks_vocabulary_membership(pool: PgPool) {
let db = Db::from_pool(pool);
let mut tx = db.pool().begin().await.unwrap();
let material = vocab::create_vocabulary(&mut tx, AuditActor::System, "material")
.await
.unwrap();
let technique = vocab::create_vocabulary(&mut tx, AuditActor::System, "technique")
.await
.unwrap();
tx.commit().await.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let term_id = vocab::add_term(
&mut tx,
AuditActor::System,
&NewTerm {
vocabulary_id: material.id,
external_uri: None,
labels: vec![LocalizedLabel {
lang: "en".into(),
label: "wood".into(),
}],
},
)
.await
.unwrap();
tx.commit().await.unwrap();
assert!(
vocab::resolve_term(db.pool(), material.id, term_id)
.await
.unwrap()
.is_some()
);
assert!(
vocab::resolve_term(db.pool(), technique.id, term_id)
.await
.unwrap()
.is_none()
);
}