Files
biggus-dickus/crates/db/tests/vocab.rs
T
2026-06-02 08:48:51 +02:00

115 lines
3.0 KiB
Rust

use db::{Db, vocab};
use domain::{LocalizedLabel, NewTerm};
use sqlx::PgPool;
#[sqlx::test]
async fn vocabulary_create_and_lookup(pool: PgPool) {
let db = Db::from_pool(pool);
let v = vocab::create_vocabulary(db.pool(), "material")
.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 v = vocab::create_vocabulary(db.pool(), "material")
.await
.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let term_id = vocab::add_term(
&mut *tx,
&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 resolve_term_checks_vocabulary_membership(pool: PgPool) {
let db = Db::from_pool(pool);
let material = vocab::create_vocabulary(db.pool(), "material")
.await
.unwrap();
let technique = vocab::create_vocabulary(db.pool(), "technique")
.await
.unwrap();
let mut tx = db.pool().begin().await.unwrap();
let term_id = vocab::add_term(
&mut *tx,
&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()
);
}