Skip to content

Commit 6dc3028

Browse files
Utkarsh Mehtautkmehta
authored andcommitted
RUST-8 Serialize validator option properly in create collection operations
1 parent 15ba593 commit 6dc3028

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/db/options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub struct CreateCollectionOptions {
6161
/// collection. Expressions can be specified using any query operators except `$near`,
6262
/// `$nearSphere`, `$text`, and `$where`.
6363
#[builder(default)]
64+
#[serde(rename = "validator")]
6465
pub validation: Option<Document>,
6566

6667
/// Specifies how strictly the database should apply the validation rules to existing documents

src/operation/create/test.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
bson::doc,
2+
bson::{doc, Bson},
33
cmap::{CommandResponse, StreamDescription},
44
concern::WriteConcern,
55
error::{ErrorKind, WriteFailure},
@@ -44,6 +44,36 @@ async fn build() {
4444
);
4545
}
4646

47+
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
48+
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
49+
async fn build_validator() {
50+
let query = doc! { "x": { "$gt": 1 } };
51+
let op = Create::new(
52+
Namespace {
53+
db: "test_db".to_string(),
54+
coll: "test_coll".to_string(),
55+
},
56+
Some(CreateCollectionOptions {
57+
validation: Some(query.clone()),
58+
..Default::default()
59+
}),
60+
);
61+
62+
let description = StreamDescription::new_testing();
63+
let cmd = op.build(&description).unwrap();
64+
65+
assert_eq!(cmd.name.as_str(), "create");
66+
assert_eq!(cmd.target_db.as_str(), "test_db");
67+
assert_eq!(cmd.read_pref.as_ref(), None);
68+
assert_eq!(
69+
cmd.body,
70+
doc! {
71+
"create": "test_coll",
72+
"validator": Bson::Document(query)
73+
}
74+
);
75+
}
76+
4777
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
4878
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
4979
async fn handle_success() {

0 commit comments

Comments
 (0)