66ad67ca77
Implements create_field_definition, field_definition_by_key, and list_field_definitions in db::fields, with TDD integration tests covering text, term, and authority field type round-trips. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
119 lines
3.1 KiB
Rust
119 lines
3.1 KiB
Rust
use db::{Db, fields, vocab};
|
|
use domain::{AuthorityKind, FieldType, LocalizedLabel, NewFieldDefinition};
|
|
use sqlx::PgPool;
|
|
|
|
fn labels() -> Vec<LocalizedLabel> {
|
|
vec![
|
|
LocalizedLabel {
|
|
lang: "sv".into(),
|
|
label: "material".into(),
|
|
},
|
|
LocalizedLabel {
|
|
lang: "en".into(),
|
|
label: "material".into(),
|
|
},
|
|
]
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn text_field_round_trips(pool: PgPool) {
|
|
let db = Db::from_pool(pool);
|
|
let mut tx = db.pool().begin().await.unwrap();
|
|
let id = fields::create_field_definition(
|
|
&mut tx,
|
|
&NewFieldDefinition {
|
|
key: "comments".into(),
|
|
field_type: FieldType::Text,
|
|
required: false,
|
|
group_key: Some("identification".into()),
|
|
labels: labels(),
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
tx.commit().await.unwrap();
|
|
|
|
let def = fields::field_definition_by_key(db.pool(), "comments")
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(def.id, id);
|
|
assert_eq!(def.field_type, FieldType::Text);
|
|
assert_eq!(def.group_key.as_deref(), Some("identification"));
|
|
assert_eq!(def.labels.len(), 2);
|
|
assert!(
|
|
fields::field_definition_by_key(db.pool(), "nope")
|
|
.await
|
|
.unwrap()
|
|
.is_none()
|
|
);
|
|
}
|
|
|
|
#[sqlx::test]
|
|
async fn term_and_authority_fields_round_trip_their_binding(pool: PgPool) {
|
|
let db = Db::from_pool(pool);
|
|
let material = vocab::create_vocabulary(db.pool(), "material")
|
|
.await
|
|
.unwrap();
|
|
|
|
let mut tx = db.pool().begin().await.unwrap();
|
|
fields::create_field_definition(
|
|
&mut tx,
|
|
&NewFieldDefinition {
|
|
key: "material".into(),
|
|
field_type: FieldType::Term {
|
|
vocabulary_id: material.id,
|
|
},
|
|
required: true,
|
|
group_key: None,
|
|
labels: labels(),
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
fields::create_field_definition(
|
|
&mut tx,
|
|
&NewFieldDefinition {
|
|
key: "maker".into(),
|
|
field_type: FieldType::Authority {
|
|
kind: Some(AuthorityKind::Person),
|
|
},
|
|
required: false,
|
|
group_key: None,
|
|
labels: vec![LocalizedLabel {
|
|
lang: "en".into(),
|
|
label: "maker".into(),
|
|
}],
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
tx.commit().await.unwrap();
|
|
|
|
let material_def = fields::field_definition_by_key(db.pool(), "material")
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(
|
|
material_def.field_type,
|
|
FieldType::Term {
|
|
vocabulary_id: material.id
|
|
}
|
|
);
|
|
assert!(material_def.required);
|
|
|
|
let maker_def = fields::field_definition_by_key(db.pool(), "maker")
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert_eq!(
|
|
maker_def.field_type,
|
|
FieldType::Authority {
|
|
kind: Some(AuthorityKind::Person)
|
|
}
|
|
);
|
|
|
|
let all = fields::list_field_definitions(db.pool()).await.unwrap();
|
|
assert_eq!(all.len(), 2);
|
|
}
|