Skip to content

Commit 12d827a

Browse files
authored
RUST-967 Fold modifiers subfield into parent struct (#459)
1 parent a0fa2b0 commit 12d827a

File tree

8 files changed

+209
-147
lines changed

8 files changed

+209
-147
lines changed

src/cmap/test/mod.rs

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ use crate::{
2020
sdam::{ServerUpdate, ServerUpdateSender},
2121
test::{
2222
assert_matches,
23+
eq_matches,
2324
run_spec_test,
2425
EventClient,
26+
MatchErrExt,
2527
Matchable,
2628
CLIENT_OPTIONS,
2729
LOCK,
@@ -321,42 +323,51 @@ impl Operation {
321323
}
322324

323325
impl Matchable for TlsOptions {
324-
fn content_matches(&self, expected: &TlsOptions) -> bool {
326+
fn content_matches(&self, expected: &TlsOptions) -> std::result::Result<(), String> {
325327
self.allow_invalid_certificates
326328
.matches(&expected.allow_invalid_certificates)
327-
&& self
328-
.ca_file_path
329-
.as_ref()
330-
.map(|pb| pb.display().to_string())
331-
.matches(
332-
&expected
333-
.ca_file_path
334-
.as_ref()
335-
.map(|pb| pb.display().to_string()),
336-
)
337-
&& self
338-
.cert_key_file_path
339-
.as_ref()
340-
.map(|pb| pb.display().to_string())
341-
.matches(
342-
&expected
343-
.cert_key_file_path
344-
.as_ref()
345-
.map(|pb| pb.display().to_string()),
346-
)
329+
.prefix("allow_invalid_certificates")?;
330+
self.ca_file_path
331+
.as_ref()
332+
.map(|pb| pb.display().to_string())
333+
.matches(
334+
&expected
335+
.ca_file_path
336+
.as_ref()
337+
.map(|pb| pb.display().to_string()),
338+
)
339+
.prefix("ca_file_path")?;
340+
self.cert_key_file_path
341+
.as_ref()
342+
.map(|pb| pb.display().to_string())
343+
.matches(
344+
&expected
345+
.cert_key_file_path
346+
.as_ref()
347+
.map(|pb| pb.display().to_string()),
348+
)
349+
.prefix("cert_key_file_path")?;
350+
Ok(())
347351
}
348352
}
349353

350354
impl Matchable for EventOptions {
351-
fn content_matches(&self, expected: &EventOptions) -> bool {
352-
self.max_idle_time.matches(&expected.max_idle_time)
353-
&& self.max_pool_size.matches(&expected.max_pool_size)
354-
&& self.min_pool_size.matches(&expected.min_pool_size)
355+
fn content_matches(&self, expected: &EventOptions) -> std::result::Result<(), String> {
356+
self.max_idle_time
357+
.matches(&expected.max_idle_time)
358+
.prefix("max_idle_time")?;
359+
self.max_pool_size
360+
.matches(&expected.max_pool_size)
361+
.prefix("max_pool_size")?;
362+
self.min_pool_size
363+
.matches(&expected.min_pool_size)
364+
.prefix("min_pool_size")?;
365+
Ok(())
355366
}
356367
}
357368

358369
impl Matchable for Event {
359-
fn content_matches(&self, expected: &Event) -> bool {
370+
fn content_matches(&self, expected: &Event) -> std::result::Result<(), String> {
360371
match (self, expected) {
361372
(Event::PoolCreated(actual), Event::PoolCreated(ref expected)) => {
362373
actual.options.matches(&expected.options)
@@ -368,8 +379,12 @@ impl Matchable for Event {
368379
actual.connection_id.matches(&expected.connection_id)
369380
}
370381
(Event::ConnectionClosed(actual), Event::ConnectionClosed(ref expected)) => {
371-
actual.reason == expected.reason
372-
&& actual.connection_id.matches(&expected.connection_id)
382+
eq_matches("reason", &actual.reason, &expected.reason)?;
383+
actual
384+
.connection_id
385+
.matches(&expected.connection_id)
386+
.prefix("connection_id")?;
387+
Ok(())
373388
}
374389
(Event::ConnectionCheckedOut(actual), Event::ConnectionCheckedOut(ref expected)) => {
375390
actual.connection_id.matches(&expected.connection_id)
@@ -380,12 +395,21 @@ impl Matchable for Event {
380395
(
381396
Event::ConnectionCheckOutFailed(actual),
382397
Event::ConnectionCheckOutFailed(ref expected),
383-
) => actual.reason == expected.reason,
384-
(Event::ConnectionCheckOutStarted(_), Event::ConnectionCheckOutStarted(_)) => true,
385-
(Event::PoolCleared(_), Event::PoolCleared(_)) => true,
386-
(Event::PoolReady(_), Event::PoolReady(_)) => true,
387-
(Event::PoolClosed(_), Event::PoolClosed(_)) => true,
388-
_ => false,
398+
) => {
399+
if actual.reason == expected.reason {
400+
Ok(())
401+
} else {
402+
Err(format!(
403+
"expected reason {:?}, got {:?}",
404+
expected.reason, actual.reason
405+
))
406+
}
407+
}
408+
(Event::ConnectionCheckOutStarted(_), Event::ConnectionCheckOutStarted(_)) => Ok(()),
409+
(Event::PoolCleared(_), Event::PoolCleared(_)) => Ok(()),
410+
(Event::PoolReady(_), Event::PoolReady(_)) => Ok(()),
411+
(Event::PoolClosed(_), Event::PoolClosed(_)) => Ok(()),
412+
(actual, expected) => Err(format!("expected event {:?}, got {:?}", actual, expected)),
389413
}
390414
}
391415
}

src/test/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub(crate) use self::{
1515
spec::{run_single_test, run_spec_test, run_spec_test_with_path, RunOn, Serverless, Topology},
1616
util::{
1717
assert_matches,
18+
eq_matches,
1819
CmapEvent,
1920
CommandEvent,
2021
Event,
@@ -23,6 +24,7 @@ pub(crate) use self::{
2324
FailCommandOptions,
2425
FailPoint,
2526
FailPointMode,
27+
MatchErrExt,
2628
Matchable,
2729
SdamEvent,
2830
TestClient,

src/test/spec/command_monitoring/event.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::Deserialize;
22

33
use crate::{
44
bson::Document,
5-
test::{CommandEvent, Matchable},
5+
test::{eq_matches, CommandEvent, MatchErrExt, Matchable},
66
};
77

88
#[derive(Debug, Deserialize, PartialEq)]
@@ -23,7 +23,7 @@ pub enum TestEvent {
2323
}
2424

2525
impl Matchable for TestEvent {
26-
fn content_matches(&self, actual: &TestEvent) -> bool {
26+
fn content_matches(&self, actual: &TestEvent) -> Result<(), String> {
2727
match (self, actual) {
2828
(
2929
TestEvent::Started {
@@ -37,9 +37,14 @@ impl Matchable for TestEvent {
3737
command: expected_command,
3838
},
3939
) => {
40-
actual_command_name == expected_command_name
41-
&& actual_database_name == expected_database_name
42-
&& actual_command.matches(expected_command)
40+
eq_matches("command_name", actual_command_name, expected_command_name)?;
41+
eq_matches(
42+
"database_name",
43+
actual_database_name,
44+
expected_database_name,
45+
)?;
46+
actual_command.matches(expected_command).prefix("command")?;
47+
Ok(())
4348
}
4449
(
4550
TestEvent::Succeeded {
@@ -51,7 +56,9 @@ impl Matchable for TestEvent {
5156
reply: expected_reply,
5257
},
5358
) => {
54-
actual_command_name == expected_command_name && actual_reply.matches(expected_reply)
59+
eq_matches("command_name", actual_command_name, expected_command_name)?;
60+
actual_reply.matches(expected_reply).prefix("reply")?;
61+
Ok(())
5562
}
5663
(
5764
TestEvent::Failed {
@@ -60,8 +67,8 @@ impl Matchable for TestEvent {
6067
TestEvent::Failed {
6168
command_name: expected_command_name,
6269
},
63-
) => actual_command_name == expected_command_name,
64-
_ => false,
70+
) => eq_matches("command_name", actual_command_name, expected_command_name),
71+
_ => Err(format!("expected event {:?}, got {:?}", self, actual)),
6572
}
6673
}
6774
}

src/test/spec/command_monitoring/operation.rs

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -110,43 +110,8 @@ impl TestOperation for DeleteOne {
110110
}
111111
}
112112

113-
// This struct is necessary because the command monitoring tests specify the options in a very old
114-
// way (SPEC-1519).
115-
#[derive(Debug, Deserialize, Default)]
116-
struct FindModifiers {
117-
#[serde(rename = "$comment", default)]
118-
comment: Option<String>,
119-
#[serde(rename = "$hint", default)]
120-
hint: Option<Hint>,
121-
#[serde(
122-
rename = "$maxTimeMS",
123-
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
124-
default
125-
)]
126-
max_time: Option<Duration>,
127-
#[serde(rename = "$min", default)]
128-
min: Option<Document>,
129-
#[serde(rename = "$max", default)]
130-
max: Option<Document>,
131-
#[serde(rename = "$returnKey", default)]
132-
return_key: Option<bool>,
133-
#[serde(rename = "$showDiskLoc", default)]
134-
show_disk_loc: Option<bool>,
135-
}
136-
137-
impl FindModifiers {
138-
fn update_options(&self, options: &mut FindOptions) {
139-
options.comment = self.comment.clone();
140-
options.hint = self.hint.clone();
141-
options.max_time = self.max_time;
142-
options.min = self.min.clone();
143-
options.max = self.max.clone();
144-
options.return_key = self.return_key;
145-
options.show_record_id = self.show_disk_loc;
146-
}
147-
}
148-
149113
#[derive(Debug, Default, Deserialize)]
114+
#[serde(deny_unknown_fields)]
150115
pub(super) struct Find {
151116
filter: Option<Document>,
152117
#[serde(default)]
@@ -158,7 +123,23 @@ pub(super) struct Find {
158123
#[serde(default)]
159124
limit: Option<i64>,
160125
#[serde(default)]
161-
modifiers: Option<FindModifiers>,
126+
comment: Option<String>,
127+
#[serde(default)]
128+
hint: Option<Hint>,
129+
#[serde(
130+
rename = "maxTimeMS",
131+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
132+
default
133+
)]
134+
max_time: Option<Duration>,
135+
#[serde(default)]
136+
min: Option<Document>,
137+
#[serde(default)]
138+
max: Option<Document>,
139+
#[serde(rename = "returnKey", default)]
140+
return_key: Option<bool>,
141+
#[serde(rename = "showRecordId", default)]
142+
show_record_id: Option<bool>,
162143
}
163144

164145
impl TestOperation for Find {
@@ -168,18 +149,21 @@ impl TestOperation for Find {
168149

169150
fn execute(&self, collection: Collection<Document>) -> BoxFuture<Result<()>> {
170151
async move {
171-
let mut options = FindOptions {
152+
let options = FindOptions {
172153
sort: self.sort.clone(),
173154
skip: self.skip,
174155
batch_size: self.batch_size.map(|i| i as u32),
175156
limit: self.limit,
157+
comment: self.comment.clone(),
158+
hint: self.hint.clone(),
159+
max_time: self.max_time,
160+
min: self.min.clone(),
161+
max: self.max.clone(),
162+
return_key: self.return_key,
163+
show_record_id: self.show_record_id,
176164
..Default::default()
177165
};
178166

179-
if let Some(ref modifiers) = self.modifiers {
180-
modifiers.update_options(&mut options);
181-
}
182-
183167
let mut cursor = collection.find(self.filter.clone(), options).await?;
184168

185169
while let Some(result) = cursor.next().await {

src/test/spec/json/command-monitoring/legacy/find.json

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,19 @@
8989
"skip": {
9090
"$numberLong": "2"
9191
},
92-
"modifiers": {
93-
"$comment": "test",
94-
"$hint": {
95-
"_id": 1
96-
},
97-
"$max": {
98-
"_id": 6
99-
},
100-
"$maxTimeMS": 6000,
101-
"$min": {
102-
"_id": 0
103-
},
104-
"$returnKey": false,
105-
"$showDiskLoc": false
106-
}
92+
"comment": "test",
93+
"hint": {
94+
"_id": 1
95+
},
96+
"max": {
97+
"_id": 6
98+
},
99+
"maxTimeMS": 6000,
100+
"min": {
101+
"_id": 0
102+
},
103+
"returnKey": false,
104+
"showRecordId": false
107105
}
108106
},
109107
"expectations": [

src/test/spec/v2_runner/test_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl CommandStartedEvent {
3636
}
3737
}
3838

39-
self.command.content_matches(&expected)
39+
self.command.content_matches(&expected).is_ok()
4040
}
4141
}
4242

0 commit comments

Comments
 (0)