From 1fd0e84957deee7c8df1847db5fafd3098ea73bf Mon Sep 17 00:00:00 2001 From: Michael Reimsbach Date: Mon, 21 Oct 2024 14:18:53 +0200 Subject: [PATCH] feat(issue): add e2e test for aggregation --- .../issue/withMetadata.graphql | 19 ++++++++- internal/e2e/issue_query_test.go | 40 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/internal/api/graphql/graph/queryCollection/issue/withMetadata.graphql b/internal/api/graphql/graph/queryCollection/issue/withMetadata.graphql index de78498f..669a1022 100644 --- a/internal/api/graphql/graph/queryCollection/issue/withMetadata.graphql +++ b/internal/api/graphql/graph/queryCollection/issue/withMetadata.graphql @@ -20,8 +20,23 @@ query ($filter: IssueFilter, $first: Int, $after: String) { earliestDiscoveryDate earliestTargetRemediationDate } + issueMatches { + totalCount + edges { + node { + componentInstance { + count + service { + id + } + } + } + } + } + activities { + totalCount + } } - cursor } } -} \ No newline at end of file +} diff --git a/internal/e2e/issue_query_test.go b/internal/e2e/issue_query_test.go index 5198876d..f4846d7b 100644 --- a/internal/e2e/issue_query_test.go +++ b/internal/e2e/issue_query_test.go @@ -199,6 +199,46 @@ var _ = Describe("Getting Issues via API", Label("e2e", "Issues"), func() { Expect(*respData.Issues.PageInfo.PageNumber).To(Equal(1), "Correct page number") }) }) + Context("and we request metadata", Label("withMetadata.graphql"), func() { + It("returns correct metadata counts", func() { + // create a queryCollection (safe to share across requests) + client := graphql.NewClient(fmt.Sprintf("http://localhost:%s/query", cfg.Port)) + + //@todo may need to make this more fault proof?! What if the test is executed from the root dir? does it still work? + b, err := os.ReadFile("../api/graphql/graph/queryCollection/issue/withMetadata.graphql") + + Expect(err).To(BeNil()) + str := string(b) + req := graphql.NewRequest(str) + + req.Var("filter", map[string]string{}) + req.Var("first", 5) + req.Var("after", "0") + + req.Header.Set("Cache-Control", "no-cache") + ctx := context.Background() + + var respData struct { + Issues model.IssueConnection `json:"Issues"` + } + if err := util2.RequestWithBackoff(func() error { return client.Run(ctx, req, &respData) }); err != nil { + logrus.WithError(err).WithField("request", req).Fatalln("Error while unmarshaling") + } + + for _, issueEdge := range respData.Issues.Edges { + ciCount := 0 + serviceIdSet := map[string]bool{} + for _, imEdge := range issueEdge.Node.IssueMatches.Edges { + ciCount += *imEdge.Node.ComponentInstance.Count + serviceIdSet[imEdge.Node.ComponentInstance.Service.ID] = true + } + Expect(issueEdge.Node.Metadata.IssueMatchCount).To(Equal(issueEdge.Node.IssueMatches.TotalCount), "IssueMatchCount is correct") + Expect(issueEdge.Node.Metadata.ComponentInstanceCount).To(Equal(ciCount), "ComponentInstanceCount is correct") + Expect(issueEdge.Node.Metadata.ActivityCount).To(Equal(issueEdge.Node.Activities.TotalCount), "ActivityCount is correct") + Expect(issueEdge.Node.Metadata.ServiceCount).To(Equal(len(serviceIdSet)), "ServiceCount is correct") + } + }) + }) }) }) })