这是indexloc提供的服务,不要输入任何密码
Skip to content

fix bson serialization test for case where double and int unify to double #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/cli/src/introspection/type_unification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ pub fn unify_object_types(
merged_type_map.into_values().collect()
}

/// True iff we consider a to be a supertype of b.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super minor spelling iff

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"iff" is an abbreviation for "if and only if"

///
/// Note that if you add more supertypes here then it is important to also update the custom
/// equality check in our tests in mongodb_agent_common::query::serialization::tests. Equality
/// needs to be transitive over supertypes, so for example if we have,
///
/// (Double, Int), (Decimal, Double)
///
/// then in addition to comparing ints to doubles, and doubles to decimals, we also need to compare
/// decimals to ints.
fn is_supertype(a: &BsonScalarType, b: &BsonScalarType) -> bool {
matches!((a, b), (Double, Int))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ cc 2efdea7f185f2f38ae643782b3523014ab7b8236e36a79cc6b7a7cac74b06f79 # shrinks to
cc 26e2543468ab6d4ffa34f9f8a2c920801ef38a35337557a8f4e74c92cf57e344 # shrinks to bson = Document({" ": Document({"¡": DateTime(1970-01-01 0:00:00.001 +00:00:00)})})
cc 7d760e540b56fedac7dd58e5bdb5bb9613b9b0bc6a88acfab3fc9c2de8bf026d # shrinks to bson = Document({"A": Array([Null, Undefined])})
cc 21360610045c5a616b371fb8d5492eb0c22065d62e54d9c8a8761872e2e192f3 # shrinks to bson = Array([Document({}), Document({" ": Null})])
cc 8842e7f78af24e19847be5d8ee3d47c547ef6c1bb54801d360a131f41a87f4fa
25 changes: 23 additions & 2 deletions crates/mongodb-agent-common/src/query/serialization/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ proptest! {

let json = bson_to_json(&inferred_type, bson.clone()).map_err(|e| error_context("error converting bson to json", e.to_string()))?;
let actual = json_to_bson(&inferred_type, json.clone()).map_err(|e| error_context("error converting json to bson", e.to_string()))?;
prop_assert_eq!(actual, bson,
"\ninferred type: {:?}\nobject types: {:?}\njson_representation: {}",
prop_assert!(custom_eq(&actual, &bson),
"`(left == right)`\nleft: `{:?}`\nright: `{:?}`\ninferred type: {:?}\nobject types: {:?}\njson_representation: {}",
actual,
bson,
inferred_type,
object_types,
serde_json::to_string_pretty(&json).unwrap()
Expand All @@ -40,3 +42,22 @@ proptest! {
prop_assert_eq!(actual, bson, "json representation: {}", json)
}
}

/// We are treating doubles as a superset of ints, so we need an equality check that allows
/// comparing those types.
fn custom_eq(a: &Bson, b: &Bson) -> bool {
match (a, b) {
(Bson::Double(a), Bson::Int32(b)) | (Bson::Int32(b), Bson::Double(a)) => *a == *b as f64,
(Bson::Array(xs), Bson::Array(ys)) => {
xs.len() == ys.len() && xs.iter().zip(ys.iter()).all(|(x, y)| custom_eq(x, y))
}
(Bson::Document(a), Bson::Document(b)) => {
a.len() == b.len()
&& a.iter().all(|(key_a, value_a)| match b.get(key_a) {
Some(value_b) => custom_eq(value_a, value_b),
None => false,
})
}
_ => a == b,
}
}
Loading