-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add tests for spdx "relationshipType": "PACKAGE_OF" #1186
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,64 @@ use crate::{ | |
model::{AncNode, AncestorSummary}, | ||
}; | ||
use itertools::Itertools; | ||
use serde_json::{json, Value}; | ||
use trustify_test_context::{ | ||
call::{self, CallService}, | ||
TrustifyContext, | ||
}; | ||
|
||
// This function checks if the actual JSON object has all the fields of the expected JSON object. | ||
pub fn has_json_fields(actual: &Value, expected: &Value) -> bool { | ||
match (actual.as_object(), expected.as_object()) { | ||
(Some(actual), Some(expected)) => { | ||
for (key, value_a) in expected { | ||
if Some(value_a) != actual.get(key.as_str()) { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
_ => false, | ||
} | ||
} | ||
Comment on lines
+12
to
+25
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very cool idea! I want it to do more, though, so forgive me for challenging you. 😄 The function expects You'll essentially have two branches:
And no, that won't actually compile due to And this will be useful all over, so let's stick it somewhere in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even better... pub trait Contains {
fn contains(&self, subset: Value) -> bool;
}
impl Contains for Value {
fn contains(&self, subset: Value) -> bool {
match (self.as_object(), subset.as_object()) {
(Some(src), Some(tgt)) => tgt
.iter()
.all(|(k, v)| src.get(k).is_some_and(|x| x.contains(v.clone()))),
_ => subset == *self,
}
}
} Accepting a reference to self and taking ownership of .filter(|m| {
m.contains(json!({
"relationship": "PackageOf",
"name": "rubygem-google-cloud-compute",
"version": "0.5.0-1.el8sat"
}))
}) Deciding to take a reference instead of ownership would be determined by whether we think most of our test subsets will be "one-off's". If we often re-use the same one, we might take a reference to avoid having to Make sense? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't help myself... If you add a branch for impl Contains for Value {
fn contains(&self, subset: Value) -> bool {
match (self, &subset) {
(Value::Object(src), Value::Object(tgt)) => tgt
.iter()
.all(|(k, v)| src.get(k).is_some_and(|x| x.contains(v.clone()))),
(Value::Array(src), Value::Array(tgt)) => tgt
.iter()
.all(|v| src.iter().any(|x| x.contains(v.clone()))),
_ => subset == *self,
}
}
} You can assert things like this: assert!(response.contains(json!({
"items": [
{
"deps": [
{
"relationship": "PackageOf",
"name": "SATELLITE-6.15-RHEL-8",
"version": "6.15",
}
]
}
]
}))); There's a tradeoff, though. Sometimes you might want arrays to match on the exact contents, e.g. you might want to assert that |
||
|
||
#[cfg(test)] | ||
#[test] | ||
fn test_has_json_fields() { | ||
// actual can have additional fields | ||
assert!(has_json_fields( | ||
&json!({ | ||
"relationship": "PackageOf", | ||
"other": "test", | ||
}), | ||
&json!({ | ||
"relationship": "PackageOf", | ||
}), | ||
)); | ||
|
||
// case where an expected field does not match | ||
assert!(!has_json_fields( | ||
&json!({ | ||
"relationship": "PackageOf", | ||
"other": "test", | ||
}), | ||
&json!({ | ||
"relationship": "bad", | ||
}), | ||
)); | ||
|
||
// case where an expected field is missing | ||
assert!(!has_json_fields( | ||
&json!({ | ||
"relationship": "PackageOf", | ||
"other": "test", | ||
}), | ||
&json!({ | ||
"name": "SATELLITE-6.15-RHEL-8", | ||
}), | ||
)) | ||
} | ||
|
||
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService + '_> { | ||
call::caller(|svc| configure(svc, ctx.db.clone())).await | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really wish serde's impl of
:#?
usedto_string_pretty
. I can remember the former, but never the latter. :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
100%