use core::{cmp::Ordering, convert::Infallible};
use bson::{Binary, Document, doc, spec::BinarySubtype};
use super::{MongoBytesComparison, MongoBytesCrossingError};
use crate::{
At, BytesFilter, ComplexComparison, FieldName, FixedBytesFilter, Mongo, RecordAddress,
ToDatabaseComparison, TryFromFilter, comparison::mongo::MongoConjunction,
};
const ROW: RecordAddress = RecordAddress::new(FieldName::new("media"), &[FieldName::new("id")]);
fn rendered<C: ComplexComparison<Mongo>>(comparison: &C, column: &str) -> Document {
let frame = At::new(&ROW).frame(FieldName::new(column));
comparison
.apply(frame.at(), MongoConjunction::new())
.expect("a MongoDB lowering cannot refuse")
.into_document()
}
fn blob(bytes: &[u8]) -> Binary {
Binary {
subtype: BinarySubtype::Generic,
bytes: bytes.to_vec(),
}
}
fn hex(bytes: Vec<u8>) -> Result<String, Infallible> {
Ok(bytes.iter().map(|b| format!("{b:02x}")).collect())
}
#[test]
fn the_two_binary_orders_agree_only_at_equal_length() {
fn bytewise(a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}
fn length_first(a: &[u8], b: &[u8]) -> Ordering {
a.len().cmp(&b.len()).then_with(|| a.cmp(b))
}
assert_eq!(bytewise(&[0xff], &[0x00, 0x00]), Ordering::Greater);
assert_eq!(length_first(&[0xff], &[0x00, 0x00]), Ordering::Less);
for a in 0..=u8::MAX {
for b in 0..=u8::MAX {
assert_eq!(bytewise(&[a], &[b]), length_first(&[a], &[b]));
assert_eq!(bytewise(&[a, b], &[b, a]), length_first(&[a, b], &[b, a]));
}
}
for a in 0..=u8::MAX {
assert_eq!(length_first(&[a], &[0x00, 0x00]), Ordering::Less);
}
}
#[test]
fn a_fixed_column_orders_over_the_bytes_it_was_given() {
let comparison = MongoBytesComparison::<Vec<u8>>::try_from_filter(
FixedBytesFilter::<[u8; 4], 4>::new()
.with_gte([0x00, 0x00, 0x00, 0x01])
.with_lt([0xff, 0xff, 0xff, 0xff]),
)
.expect("a fixed-width column answers the ordering operators");
assert_eq!(comparison.conjuncts(), 2);
assert_eq!(
rendered(&comparison, "digest"),
doc! {
"digest": {
"$gte": blob(&[0x00, 0x00, 0x00, 0x01]),
"$lt": blob(&[0xff, 0xff, 0xff, 0xff]),
}
}
);
}
#[test]
fn a_variable_column_compares_and_enumerates() {
let comparison = MongoBytesComparison::<Vec<u8>>::try_from_filter(
BytesFilter::<Vec<u8>>::new()
.with_eq(vec![0x89, 0x50])
.with_ne(vec![0x00])
.with_in_([vec![0xff], vec![0x00, 0x01]])
.with_not_in([Vec::new()]),
)
.expect("a blob column answers equality and membership");
assert_eq!(comparison.conjuncts(), 4);
assert_eq!(
rendered(&comparison, "payload"),
doc! {
"payload": {
"$eq": blob(&[0x89, 0x50]),
"$ne": blob(&[0x00]),
"$in": [blob(&[0xff]), blob(&[0x00, 0x01])],
"$nin": [blob(&[])],
}
}
);
}
#[test]
fn a_variable_column_refuses_every_ordering_operator() {
let refused = [
(BytesFilter::<Vec<u8>>::new().with_gt(vec![0x01]), "gt"),
(BytesFilter::<Vec<u8>>::new().with_gte(vec![0x01]), "gte"),
(BytesFilter::<Vec<u8>>::new().with_lt(vec![0x01]), "lt"),
(BytesFilter::<Vec<u8>>::new().with_lte(vec![0x01]), "lte"),
];
for (filter, operator) in refused {
assert_eq!(
MongoBytesComparison::<Vec<u8>>::try_from_filter(filter).unwrap_err(),
MongoBytesCrossingError::VariableOrdering(operator)
);
}
}
#[test]
fn a_blob_is_refused_a_byte_substring() {
assert_eq!(
MongoBytesComparison::<Vec<u8>>::try_from_filter(
BytesFilter::<Vec<u8>>::new().with_contains(vec![0x0d])
)
.unwrap_err(),
MongoBytesCrossingError::Substring("contains")
);
assert_eq!(
MongoBytesComparison::<Vec<u8>>::try_from_filter(
BytesFilter::<Vec<u8>>::new().with_not_contains(vec![0x0d])
)
.unwrap_err(),
MongoBytesCrossingError::Substring("not_contains")
);
}
#[test]
fn a_crossing_replaces_the_operand_with_its_stored_form() {
let comparison = MongoBytesComparison::<String>::try_from_filter_with(
BytesFilter::<Vec<u8>>::new()
.with_eq(vec![0x89, 0x50])
.with_in_([vec![0xff], vec![0x00]]),
hex,
)
.expect("the crossing into a hex column is total for equality and membership");
assert_eq!(comparison.eq().map(String::as_str), Some("8950"));
assert_eq!(
rendered(&comparison, "payload"),
doc! { "payload": { "$eq": "8950", "$in": ["ff", "00"] } }
);
}
#[test]
fn the_refusals_are_checked_before_the_operands_convert() {
fn refuse(_: Vec<u8>) -> Result<String, &'static str> {
Err("the converter ran")
}
assert_eq!(
MongoBytesComparison::<String>::try_from_filter_with(
BytesFilter::<Vec<u8>>::new().with_contains(vec![0x0d]),
refuse,
)
.unwrap_err(),
MongoBytesCrossingError::Substring("contains")
);
assert_eq!(
MongoBytesComparison::<String>::try_from_filter_with(
BytesFilter::<Vec<u8>>::new().with_gt(vec![0x0d]),
refuse,
)
.unwrap_err(),
MongoBytesCrossingError::VariableOrdering("gt")
);
assert_eq!(
MongoBytesComparison::<String>::try_from_filter_with(
BytesFilter::<Vec<u8>>::new().with_eq(vec![0x0d]),
refuse,
)
.unwrap_err(),
MongoBytesCrossingError::Operand("the converter ran")
);
}
#[test]
fn the_blob_keeps_a_refusal_and_the_fixed_kind_has_none_left() {
let variable: Result<MongoBytesComparison<Vec<u8>>, MongoBytesCrossingError<Infallible>> =
MongoBytesComparison::try_from_filter(BytesFilter::<Vec<u8>>::new().with_lte(vec![0x0d]));
assert_eq!(
variable.unwrap_err(),
MongoBytesCrossingError::VariableOrdering("lte")
);
let fixed: Result<MongoBytesComparison<Vec<u8>>, Infallible> =
MongoBytesComparison::try_from_filter(
FixedBytesFilter::<[u8; 4], 4>::new().with_lte([0x0d, 0x0a, 0x0d, 0x0a]),
);
assert_eq!(fixed.unwrap().conjuncts(), 1);
}
#[test]
fn the_codegen_projection_resolves_for_a_bytes_field_of_either_shape() {
type Blob = <BytesFilter<Vec<u8>> as ToDatabaseComparison<Vec<u8>, Mongo>>::Comparison;
type Hash = <FixedBytesFilter<[u8; 4], 4> as ToDatabaseComparison<Vec<u8>, Mongo>>::Comparison;
let blob: Blob = MongoBytesComparison::<Vec<u8>>::new();
let hash: Hash = MongoBytesComparison::<Vec<u8>>::new();
assert!(blob.is_empty());
assert_eq!(blob, hash);
}