-
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
Trying to come with more clarity in tests #1195
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -656,37 +656,27 @@ async fn spdx_package_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> { | |
let response: Value = app.call_and_read_body_json(request).await; | ||
log::debug!("{response:#?}"); | ||
|
||
let sbom = &response["items"][0]; | ||
let matches: Vec<_> = sbom["deps"] | ||
.as_array() | ||
.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": [], | ||
}) | ||
}) | ||
.collect(); | ||
|
||
assert_eq!(1, matches.len()); | ||
assert_eq!( | ||
response.pointer("/items/0/sbom_id"), | ||
response.pointer("/items/0/deps/0/sbom_id") | ||
); | ||
assert_eq!( | ||
Some(&json!("PackageOf")), | ||
response.pointer("/items/0/deps/0/relationship") | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
#[test_context(TrustifyContext)] | ||
#[test(actix_web::test)] | ||
async fn spdx_ancestor_of(ctx: &TrustifyContext) -> Result<(), anyhow::Error> { | ||
// A helper closure to compare nested json values | ||
let assert = |v: &Value, path, s: &str| assert_eq!(v.pointer(path).unwrap(), &json!(s)); | ||
let app = caller(ctx).await?; | ||
ctx.ingest_documents(["spdx/1178.json"]).await?; | ||
|
||
// This smells a little funny... are they backward? | ||
// The purl's we'll use to invoke the opposing analysis endpoints | ||
let parent = "pkg:rpm/redhat/[email protected]"; | ||
let child = "pkg:generic/[email protected]?arch=src"; | ||
|
||
|
@@ -695,20 +685,17 @@ async fn spdx_ancestor_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::debug!("{response:#?}"); | ||
let item = &response["items"][0]; | ||
let deps = &item["deps"]; | ||
let dep = &deps[0]; | ||
|
||
// assert array lengths | ||
assert!(response["items"].get(1).is_none()); | ||
assert!(item["purl"].get(1).is_none()); | ||
assert!(deps.get(1).is_none()); | ||
assert!(dep["purl"].get(1).is_none()); | ||
|
||
// assert expected values | ||
assert_eq!(parent, item["purl"][0]); | ||
assert_eq!("AncestorOf", dep["relationship"]); | ||
assert_eq!(child, dep["purl"][0]); | ||
assert(&response, "/items/0/purl/0", parent); | ||
assert(&response, "/items/0/deps/0/relationship", "AncestorOf"); | ||
assert(&response, "/items/0/deps/0/purl/0", child); | ||
|
||
// assert array lengths | ||
assert!(response.pointer("/items/1").is_none()); | ||
assert!(response.pointer("/items/0/purl/1").is_none()); | ||
assert!(response.pointer("/items/0/deps/1").is_none()); | ||
assert!(response.pointer("/items/0/deps/0/purl/1").is_none()); | ||
|
||
// Ensure child has ancestors that include the parent | ||
let uri = format!( | ||
|
@@ -718,20 +705,17 @@ async fn spdx_ancestor_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::debug!("{response:#?}"); | ||
let item = &response["items"][0]; | ||
let ancs = &item["ancestors"]; | ||
let anc = &ancs[0]; | ||
|
||
// assert array lengths | ||
assert!(response["items"].get(1).is_none()); | ||
assert!(item["purl"].get(1).is_none()); | ||
assert!(ancs.get(1).is_none()); | ||
assert!(anc["purl"].get(1).is_none()); | ||
|
||
// assert expected values | ||
assert_eq!(child, item["purl"][0]); | ||
assert_eq!("AncestorOf", anc["relationship"]); | ||
assert_eq!(parent, anc["purl"][0]); | ||
assert(&response, "/items/0/purl/0", child); | ||
assert(&response, "/items/0/ancestors/0/relationship", "AncestorOf"); | ||
assert(&response, "/items/0/ancestors/0/purl/0", parent); | ||
|
||
// assert array lengths | ||
assert!(response.pointer("/items/1").is_none()); | ||
assert!(response.pointer("/items/0/purl/1").is_none()); | ||
assert!(response.pointer("/items/0/ancestors/1").is_none()); | ||
assert!(response.pointer("/items/0/ancestors/0/purl/1").is_none()); | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Don't hate it but I do think assuming the array position of the element could be considered brittle. Is that PackageOf relationship pointing at the expected package?
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.
You could include additional assertions about the lengths of the relevant arrays like I do in the test below, e.g.
The
PackageOf
relationship is asserted on lines 663-666.I don't like the pointer path as much as your
ContainsSubset
trait.