use bson::{Bson, Document, doc};
use super::{removal, removal_where, replacement, stored};
use crate::{
ComplexComparison,
comparison::{At, Backend, mongo::Mongo},
query::mongo::{
MongoQuery, MongoWriteError,
tests::{Shot, ShotFilter, ShotMongo, covering, titled},
},
};
fn shot() -> Shot {
Shot {
media: 7,
id: 3,
title: "wide".to_owned(),
score: Some(0.5),
cover: None,
}
}
fn stored_shot() -> Document {
doc! {
"media": 7i64,
"_id": 3i64,
"title": "wide",
"score": 0.5f64,
"cover_id": Bson::Null,
}
}
#[test]
fn an_insert_sends_the_whole_document_with_the_id_under_underscore_id() {
let document = stored::<ShotMongo>(shot()).expect("the fixture crosses");
assert_eq!(document, stored_shot());
}
#[test]
fn an_absent_optional_is_written_as_null_rather_than_omitted() {
let document = stored::<ShotMongo>(shot()).expect("the fixture crosses");
assert!(document.contains_key("cover_id"));
assert_eq!(document.get("cover_id"), Some(&Bson::Null));
}
#[test]
fn an_upsert_replaces_the_whole_document_at_the_address_the_other_verbs_name() {
let (filter, document) = replacement::<ShotMongo>(shot()).expect("the fixture crosses");
assert_eq!(filter, doc! { "_id": { "$eq": 3i64 } });
assert_eq!(document, stored_shot());
assert_eq!(document, stored::<ShotMongo>(shot()).expect("crosses"));
assert_eq!(filter, removal::<ShotMongo>(&3).expect("the id crosses"));
}
#[test]
fn the_replacement_is_a_document_and_never_an_update_specification() {
let (_, document) = replacement::<ShotMongo>(shot()).expect("the fixture crosses");
assert!(
document.keys().all(|key| !key.starts_with('$')),
"a replacement carries fields, and an operator key would make it an update: {document:?}"
);
assert_eq!(document.len(), <ShotMongo as MongoQuery>::FIELDS.len());
}
#[test]
fn upserting_the_same_record_twice_issues_the_same_command() {
let once = replacement::<ShotMongo>(shot()).expect("the fixture crosses");
let twice = replacement::<ShotMongo>(shot()).expect("the fixture crosses");
assert_eq!(once, twice);
}
#[test]
fn a_delete_names_one_document_by_its_id() {
let filter = removal::<ShotMongo>(&3).expect("the id crosses");
assert_eq!(filter, doc! { "_id": { "$eq": 3i64 } });
}
#[test]
fn a_scoped_delete_lowers_the_callers_own_filter() {
let predicate = removal_where::<ShotMongo, _>(&titled("wide")).expect("a scoped filter lowers");
assert_eq!(predicate, doc! { "title": { "$eq": "wide" } });
}
#[test]
fn an_unfilled_comparison_is_refused_before_anything_lowers() {
let refusal = removal_where::<ShotMongo, _>(&ShotFilter::default())
.expect_err("an empty comparison names no document");
match refusal {
MongoWriteError::Unscoped(collection) => assert_eq!(collection.as_str(), "shot"),
other => panic!("the empty comparison is refused as unscoped, not as `{other}`"),
}
}
#[test]
fn a_comparison_that_over_reports_its_conjuncts_is_still_refused() {
let refusal =
removal_where::<ShotMongo, _>(&Liar).expect_err("a filter that renders `{}` is refused");
match refusal {
MongoWriteError::Unscoped(collection) => assert_eq!(collection.as_str(), "shot"),
other => panic!("the over-reporting comparison is refused as unscoped, not as `{other}`"),
}
}
#[test]
fn a_filter_that_needs_a_lookup_is_refused_rather_than_run_without_it() {
let refusal = removal_where::<ShotMongo, _>(&covering("Autumn"))
.expect_err("a relation predicate has no place in a delete");
match refusal {
MongoWriteError::Joined(stages) => assert_eq!(stages, 3),
other => panic!("the joined filter is refused as joined, not as `{other}`"),
}
}
#[test]
fn the_document_a_write_sends_is_the_document_the_read_extracts() {
let record = shot();
let document = stored::<ShotMongo>(record.clone()).expect("the fixture crosses");
let read = ShotMongo::from_document(&document).expect("the write's own document extracts");
assert_eq!(read.record().expect("the row raises"), record);
}
#[derive(Debug)]
struct Liar;
impl ComplexComparison<Mongo> for Liar {
fn conjuncts(&self) -> usize {
1
}
fn apply<'q>(
&'q self,
_: At<'_>,
builder: <Mongo as Backend>::Builder<'q>,
) -> Result<<Mongo as Backend>::Builder<'q>, <Mongo as Backend>::Error> {
Ok(builder)
}
}