Skip to content

Commit

Permalink
Improve tests based on PR feedback.
Browse files Browse the repository at this point in the history
Signed-off-by: Hiram Chirino <[email protected]>
  • Loading branch information
chirino committed Jan 23, 2025
1 parent 63f860f commit 618f27b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 21 deletions.
39 changes: 18 additions & 21 deletions modules/analysis/src/endpoints/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::test::caller;
use crate::test::{caller, has_json_fields};
use actix_http::Request;
use actix_web::test::TestRequest;
use itertools::Itertools;
Expand Down Expand Up @@ -662,16 +662,14 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
.into_iter()
.flatten()
.filter(|m| {
m == &&json!({
"sbom_id": sbom["sbom_id"],
"node_id": "SPDXRef-83c9faa0-ca85-4e48-9165-707b2f9a324b",
"relationship": "PackageOf",
"purl": [],
"cpe": m["cpe"], // long list assume it's correct
"name": "SATELLITE-6.15-RHEL-8",
"version": "6.15",
"deps": [],
})
has_json_fields(
m,
&json!({
"relationship": "PackageOf",
"name": "SATELLITE-6.15-RHEL-8",
"version": "6.15",
}),
)
})
.collect();

Expand All @@ -683,23 +681,22 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
);
let request: Request = TestRequest::get().uri(&uri).to_request();
let response: Value = app.call_and_read_body_json(request).await;
log::info!("{}", serde_json::to_string_pretty(&response)?);
log::debug!("{}", serde_json::to_string_pretty(&response)?);

let sbom = &response["items"][0];
let matches: Vec<_> = sbom["ancestors"]
.as_array()
.into_iter()
.flatten()
.filter(|m| {
m == &&json!({
"sbom_id": sbom["sbom_id"],
"node_id": m["node_id"],
"relationship": "PackageOf",
"purl": m["purl"], // long list assume it's correct
"cpe": m["cpe"], // long list assume it's correct
"name": "rubygem-google-cloud-compute",
"version": "0.5.0-1.el8sat"
})
has_json_fields(
m,
&json!({
"relationship": "PackageOf",
"name": "rubygem-google-cloud-compute",
"version": "0.5.0-1.el8sat"
}),
)
})
.collect();

Expand Down
53 changes: 53 additions & 0 deletions modules/analysis/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

#[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
}
Expand Down

0 comments on commit 618f27b

Please sign in to comment.