use super::*;
#[test]
fn version_matches_cargo_toml() {
assert_eq!(VERSION, "0.2.1-alpha");
}
#[test]
fn scalar_display_covers_all_temporal_variants() {
use document::Scalar;
assert_eq!(Scalar::Date("2024-01-01".into()).to_string(), "2024-01-01");
assert_eq!(Scalar::Time("12:30:00".into()).to_string(), "12:30:00");
assert_eq!(
Scalar::Datetime("2024-01-01T12:30:00".into()).to_string(),
"2024-01-01T12:30:00"
);
}
#[test]
fn materialize_accepts_infers_own_source_data() {
use document::Value;
use indexmap::IndexMap;
fn obj(pairs: &[(&str, Value)]) -> Value {
let mut m = IndexMap::new();
for (k, v) in pairs {
m.insert((*k).to_string(), v.clone());
}
Value::Object(m)
}
let samples = vec![
document::Doc::of(&obj(&[
("name", Value::Str("alice".into())),
("age", Value::Int((30).into())),
(
"address",
obj(&[
("city", Value::Str("NYC".into())),
("zip", Value::Str("10001".into())),
]),
),
(
"tags",
Value::Array(vec![Value::Str("a".into()), Value::Str("b".into())]),
),
]))
.unwrap(),
document::Doc::of(&obj(&[
("name", Value::Str("bob".into())),
(
"address",
obj(&[
("city", Value::Str("LA".into())),
("zip", Value::Str("90001".into())),
]),
),
("tags", Value::Array(vec![Value::Str("c".into())])),
]))
.unwrap(),
];
let schema = infer(&samples, "Root").expect("infer should draft a schema for these samples");
for sample in &samples {
let raw = sample.root().to_raw();
materialize(&raw, Some(&schema))
.expect("an inferred schema must accept its own source data");
}
}
#[test]
fn materialize_of_infer_upgrades_an_integer_number_mix_to_number() {
use document::Value;
use indexmap::IndexMap;
fn obj(pairs: &[(&str, Value)]) -> Value {
let mut m = IndexMap::new();
for (k, v) in pairs {
m.insert((*k).to_string(), v.clone());
}
Value::Object(m)
}
let samples = vec![
document::Doc::of(&obj(&[("price", Value::Int((3).into()))])).unwrap(),
document::Doc::of(&obj(&[("price", Value::Float(3.5))])).unwrap(),
];
let schema = infer(&samples, "Root").unwrap();
assert_eq!(
schema.env()["Root"].field("price").unwrap().ty,
schema::FieldType::Scalar(schema::NUMBER)
);
for sample in &samples {
let raw = sample.root().to_raw();
let out = materialize(&raw, Some(&schema)).unwrap();
let rebuilt = document::Doc::from_raw(out).unwrap();
let price = rebuilt.root().get_one("price").unwrap();
assert!(matches!(price.value().unwrap(), document::Scalar::Float(_)));
}
}