feat(db): add authority repository with multilingual labels
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
use db::{Db, authority};
|
||||
use domain::{AuthorityKind, LocalizedLabel, NewAuthority};
|
||||
use sqlx::PgPool;
|
||||
|
||||
fn new_person(name_sv: &str, name_en: &str) -> NewAuthority {
|
||||
NewAuthority {
|
||||
kind: AuthorityKind::Person,
|
||||
external_uri: None,
|
||||
labels: vec![
|
||||
LocalizedLabel {
|
||||
lang: "sv".into(),
|
||||
label: name_sv.into(),
|
||||
},
|
||||
LocalizedLabel {
|
||||
lang: "en".into(),
|
||||
label: name_en.into(),
|
||||
},
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
async fn authority_round_trips_with_labels(pool: PgPool) {
|
||||
let db = Db::from_pool(pool);
|
||||
|
||||
let mut tx = db.pool().begin().await.unwrap();
|
||||
let id = authority::create_authority(&mut *tx, &new_person("Carl Larsson", "Carl Larsson"))
|
||||
.await
|
||||
.unwrap();
|
||||
tx.commit().await.unwrap();
|
||||
|
||||
let got = authority::authority_by_id(db.pool(), id)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(got.id, id);
|
||||
assert_eq!(got.kind, AuthorityKind::Person);
|
||||
assert_eq!(got.labels.len(), 2);
|
||||
assert_eq!(
|
||||
domain::pick_label(&got.labels, "sv", "en"),
|
||||
Some("Carl Larsson")
|
||||
);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
async fn list_by_kind_filters(pool: PgPool) {
|
||||
let db = Db::from_pool(pool);
|
||||
|
||||
let mut tx = db.pool().begin().await.unwrap();
|
||||
authority::create_authority(&mut *tx, &new_person("A", "A"))
|
||||
.await
|
||||
.unwrap();
|
||||
authority::create_authority(
|
||||
&mut *tx,
|
||||
&NewAuthority {
|
||||
kind: AuthorityKind::Place,
|
||||
external_uri: None,
|
||||
labels: vec![LocalizedLabel {
|
||||
lang: "en".into(),
|
||||
label: "Stockholm".into(),
|
||||
}],
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
tx.commit().await.unwrap();
|
||||
|
||||
let people = authority::list_by_kind(db.pool(), AuthorityKind::Person)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(people.len(), 1);
|
||||
assert_eq!(people[0].kind, AuthorityKind::Person);
|
||||
|
||||
let places = authority::list_by_kind(db.pool(), AuthorityKind::Place)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(places.len(), 1);
|
||||
assert_eq!(places[0].kind, AuthorityKind::Place);
|
||||
}
|
||||
|
||||
#[sqlx::test]
|
||||
async fn resolve_authority_returns_kind(pool: PgPool) {
|
||||
let db = Db::from_pool(pool);
|
||||
|
||||
let mut tx = db.pool().begin().await.unwrap();
|
||||
let id = authority::create_authority(&mut *tx, &new_person("X", "X"))
|
||||
.await
|
||||
.unwrap();
|
||||
tx.commit().await.unwrap();
|
||||
|
||||
let r = authority::resolve_authority(db.pool(), id)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(r.authority_id(), id);
|
||||
assert_eq!(r.kind(), AuthorityKind::Person);
|
||||
|
||||
let missing = authority::resolve_authority(db.pool(), domain::AuthorityId::new())
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(missing.is_none());
|
||||
}
|
||||
Reference in New Issue
Block a user