Skip to content

Commit 618f27b

Browse files
committed
Improve tests based on PR feedback.
Signed-off-by: Hiram Chirino <[email protected]>
1 parent 63f860f commit 618f27b

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed

modules/analysis/src/endpoints/test.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::test::caller;
1+
use crate::test::{caller, has_json_fields};
22
use actix_http::Request;
33
use actix_web::test::TestRequest;
44
use itertools::Itertools;
@@ -662,16 +662,14 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> {
662662
.into_iter()
663663
.flatten()
664664
.filter(|m| {
665-
m == &&json!({
666-
"sbom_id": sbom["sbom_id"],
667-
"node_id": "SPDXRef-83c9faa0-ca85-4e48-9165-707b2f9a324b",
668-
"relationship": "PackageOf",
669-
"purl": [],
670-
"cpe": m["cpe"], // long list assume it's correct
671-
"name": "SATELLITE-6.15-RHEL-8",
672-
"version": "6.15",
673-
"deps": [],
674-
})
665+
has_json_fields(
666+
m,
667+
&json!({
668+
"relationship": "PackageOf",
669+
"name": "SATELLITE-6.15-RHEL-8",
670+
"version": "6.15",
671+
}),
672+
)
675673
})
676674
.collect();
677675

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

688686
let sbom = &response["items"][0];
689687
let matches: Vec<_> = sbom["ancestors"]
690688
.as_array()
691689
.into_iter()
692690
.flatten()
693691
.filter(|m| {
694-
m == &&json!({
695-
"sbom_id": sbom["sbom_id"],
696-
"node_id": m["node_id"],
697-
"relationship": "PackageOf",
698-
"purl": m["purl"], // long list assume it's correct
699-
"cpe": m["cpe"], // long list assume it's correct
700-
"name": "rubygem-google-cloud-compute",
701-
"version": "0.5.0-1.el8sat"
702-
})
692+
has_json_fields(
693+
m,
694+
&json!({
695+
"relationship": "PackageOf",
696+
"name": "rubygem-google-cloud-compute",
697+
"version": "0.5.0-1.el8sat"
698+
}),
699+
)
703700
})
704701
.collect();
705702

modules/analysis/src/test.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,64 @@ use crate::{
33
model::{AncNode, AncestorSummary},
44
};
55
use itertools::Itertools;
6+
use serde_json::{json, Value};
67
use trustify_test_context::{
78
call::{self, CallService},
89
TrustifyContext,
910
};
1011

12+
// This function checks if the actual JSON object has all the fields of the expected JSON object.
13+
pub fn has_json_fields(actual: &Value, expected: &Value) -> bool {
14+
match (actual.as_object(), expected.as_object()) {
15+
(Some(actual), Some(expected)) => {
16+
for (key, value_a) in expected {
17+
if Some(value_a) != actual.get(key.as_str()) {
18+
return false;
19+
}
20+
}
21+
true
22+
}
23+
_ => false,
24+
}
25+
}
26+
27+
#[cfg(test)]
28+
#[test]
29+
fn test_has_json_fields() {
30+
// actual can have additional fields
31+
assert!(has_json_fields(
32+
&json!({
33+
"relationship": "PackageOf",
34+
"other": "test",
35+
}),
36+
&json!({
37+
"relationship": "PackageOf",
38+
}),
39+
));
40+
41+
// case where an expected field does not match
42+
assert!(!has_json_fields(
43+
&json!({
44+
"relationship": "PackageOf",
45+
"other": "test",
46+
}),
47+
&json!({
48+
"relationship": "bad",
49+
}),
50+
));
51+
52+
// case where an expected field is missing
53+
assert!(!has_json_fields(
54+
&json!({
55+
"relationship": "PackageOf",
56+
"other": "test",
57+
}),
58+
&json!({
59+
"name": "SATELLITE-6.15-RHEL-8",
60+
}),
61+
))
62+
}
63+
1164
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService + '_> {
1265
call::caller(|svc| configure(svc, ctx.db.clone())).await
1366
}

0 commit comments

Comments
 (0)