7782bd764a
Also fix pre-existing clippy::explicit_auto_deref in all db test files (&mut *tx → &mut tx across authority.rs and vocab.rs).
157 lines
4.0 KiB
Rust
157 lines
4.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 term_with_no_labels_round_trips_empty(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: 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);
|
|
vocab::create_vocabulary(db.pool(), "material")
|
|
.await
|
|
.unwrap();
|
|
let err = vocab::create_vocabulary(db.pool(), "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 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()
|
|
);
|
|
}
|