use bson::Bson;
use super::MongoCarrier;
use crate::{BeI128, BeU64, BeU128, Count, Score};
#[test]
fn each_width_carries_the_arm_the_table_names() {
assert_eq!(true.to_bson(), Bson::Boolean(true));
assert_eq!((-1_i8).to_bson(), Bson::Int32(-1));
assert_eq!((-1_i16).to_bson(), Bson::Int32(-1));
assert_eq!((-1_i32).to_bson(), Bson::Int32(-1));
assert_eq!((-1_i64).to_bson(), Bson::Int64(-1));
assert_eq!(1_u8.to_bson(), Bson::Int32(1));
assert_eq!(1_u16.to_bson(), Bson::Int32(1));
assert_eq!(0.5_f32.to_bson(), Bson::Double(0.5));
assert_eq!(0.5_f64.to_bson(), Bson::Double(0.5));
assert_eq!(String::from("v").to_bson(), Bson::String(String::from("v")));
assert_eq!(Score::ONE.to_bson(), Bson::Double(1.0));
}
#[test]
fn a_small_u32_carries_as_int64_and_not_as_the_from_impl_would() {
assert_eq!(1_u32.to_bson(), Bson::Int64(1));
assert_ne!(1_u32.to_bson(), Bson::from(1_u32));
}
#[test]
fn a_cardinality_carries_as_int64_through_its_own_codec() {
assert_eq!(Count::new(0).to_bson(), Bson::Int64(0));
assert_eq!(Count::new(42).to_bson(), Bson::Int64(42));
assert_eq!(
Count::new(i64::MAX.unsigned_abs()).to_bson(),
Bson::Int64(i64::MAX)
);
assert_eq!(Count::new(u64::MAX).to_bson(), Bson::Int64(i64::MAX));
}
#[test]
fn a_blob_carries_as_generic_binary() {
let Bson::Binary(binary) = vec![1_u8, 2, 3].to_bson() else {
panic!("a blob carries as binary");
};
assert_eq!(binary.subtype, bson::spec::BinarySubtype::Generic);
assert_eq!(binary.bytes, [1, 2, 3]);
}
#[test]
fn a_uuid_carries_as_bindata_subtype_four() {
fn binary(value: Bson) -> bson::Binary {
let Bson::Binary(binary) = value else {
panic!("a uuid carries as binary");
};
binary
}
let low = uuid::Uuid::from_bytes([0x01; 16]);
let high = uuid::Uuid::from_bytes([0x02; 16]);
assert_eq!(
binary(low.to_bson()).subtype,
bson::spec::BinarySubtype::Uuid
);
assert_ne!(
binary(low.to_bson()).subtype,
bson::spec::BinarySubtype::Generic,
"a uuid is not a nameless blob"
);
assert_eq!(binary(low.to_bson()).bytes, low.as_bytes());
assert_eq!(
binary(low.to_bson()).bytes.len(),
16,
"and never the 36 text"
);
assert!(low < high, "the Rust order");
assert!(
binary(low.to_bson()).bytes < binary(high.to_bson()).bytes,
"and the stored order, which is the same order"
);
}
#[test]
fn a_wide_integer_carries_the_bytes_its_carrier_encodes() {
fn bytes(value: Bson) -> Vec<u8> {
let Bson::Binary(binary) = value else {
panic!("a big-endian carrier carries as binary");
};
assert_eq!(binary.subtype, bson::spec::BinarySubtype::Generic);
binary.bytes
}
assert_eq!(bytes(BeU64(1).to_bson()), BeU64(1).to_blob());
assert_eq!(bytes(BeU128(1).to_bson()), BeU128(1).to_blob());
assert_eq!(bytes(BeI128(-1).to_bson()), BeI128(-1).to_blob());
assert_eq!(bytes(BeU64(1).to_bson()), [0, 0, 0, 0, 0, 0, 0, 1]);
assert!(bytes(BeU64(1).to_bson()) < bytes(BeU64(256).to_bson()));
}
#[test]
fn a_borrowed_operand_carries_as_its_referent() {
fn carried<V: MongoCarrier>(value: V) -> Bson {
value.to_bson()
}
let owned = 7_i64;
let borrowed: &i64 = &owned;
assert_eq!(carried(borrowed), carried(owned));
}
#[test]
fn an_instant_carries_as_two_flat_integers() {
use crate::Timestamp;
let at: Timestamp = "2026-08-09T21:14:03.123456789Z".parse().unwrap();
let Bson::Document(carried) = at.to_bson() else {
panic!("an instant carries as a sub-document");
};
assert_eq!(carried.get("secs"), Some(&Bson::Int64(1_786_310_043)));
assert_eq!(carried.get("nanos"), Some(&Bson::Int32(123_456_789)));
assert_eq!(carried.len(), 2, "and nothing else is in it");
assert_ne!(carried.get("nanos"), Some(&Bson::Int32(123_000_000)));
let before: Timestamp = "1969-12-31T23:59:58.5Z".parse().unwrap();
let Bson::Document(carried) = before.to_bson() else {
panic!("an instant carries as a sub-document");
};
assert_eq!(carried.get("secs"), Some(&Bson::Int64(-1)));
assert_eq!(carried.get("nanos"), Some(&Bson::Int32(-500_000_000)));
}
#[cfg(feature = "regex")]
#[test]
fn a_pattern_carries_as_its_text_and_not_as_a_bson_regex() {
let pattern = regex::Regex::new("^[a-z]+(?:-[0-9]+)?$").expect("a pattern");
assert_eq!(
pattern.to_bson(),
Bson::String(String::from("^[a-z]+(?:-[0-9]+)?$"))
);
assert!(
!matches!(pattern.to_bson(), Bson::RegularExpression(_)),
"the operator arm is refused: this column holds a pattern as a VALUE"
);
assert_eq!(
Some(pattern).to_bson(),
Bson::String(String::from("^[a-z]+(?:-[0-9]+)?$"))
);
assert_eq!(None::<regex::Regex>.to_bson(), Bson::Null);
}