Skip to content

Commit

Permalink
Ent - IngestScorecards implementation with tests (#1271)
Browse files Browse the repository at this point in the history
Signed-off-by: mrizzi <mrizzi@redhat.com>
  • Loading branch information
mrizzi committed Sep 15, 2023
1 parent 69586ae commit 37fecf4
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
13 changes: 13 additions & 0 deletions pkg/assembler/backends/ent/backend/scorecard.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/guacsec/guac/pkg/assembler/backends/ent/sourcenamespace"
"github.com/guacsec/guac/pkg/assembler/backends/ent/sourcetype"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
"github.com/vektah/gqlparser/v2/gqlerror"
)

func (b *EntBackend) Scorecards(ctx context.Context, filter *model.CertifyScorecardSpec) ([]*model.CertifyScorecard, error) {
Expand Down Expand Up @@ -107,6 +108,18 @@ func (b *EntBackend) IngestScorecard(ctx context.Context, source model.SourceInp
return toModelCertifyScorecard(csc), nil
}

func (b *EntBackend) IngestScorecards(ctx context.Context, sources []*model.SourceInputSpec, scorecards []*model.ScorecardInputSpec) ([]*model.CertifyScorecard, error) {
var modelScorecards []*model.CertifyScorecard
for i, sc := range scorecards {
modelScorecard, err := b.IngestScorecard(ctx, *sources[i], *sc)
if err != nil {
return nil, gqlerror.Errorf("IngestScorecards failed with err: %v", err)
}
modelScorecards = append(modelScorecards, modelScorecard)
}
return modelScorecards, nil
}

func upsertScorecard(ctx context.Context, tx *ent.Tx, source model.SourceInputSpec, scorecardInput model.ScorecardInputSpec) (*ent.CertifyScorecard, error) {
checks := make([]*model.ScorecardCheck, len(scorecardInput.Checks))
for i, check := range scorecardInput.Checks {
Expand Down
180 changes: 180 additions & 0 deletions pkg/assembler/backends/ent/backend/scorecard_test.go
Expand Up @@ -449,3 +449,183 @@ func (s *Suite) TestCertifyScorecard() {
})
}
}

func (s *Suite) TestIngestScorecards() {
type call struct {
Src []*model.SourceInputSpec
SC []*model.ScorecardInputSpec
}
tests := []struct {
Name string
InSrc []*model.SourceInputSpec
Calls []call
Query *model.CertifyScorecardSpec
ExpSC []*model.CertifyScorecard
ExpIngestErr bool
ExpQueryErr bool
}{
{
Name: "HappyPath",
InSrc: []*model.SourceInputSpec{s1},
Calls: []call{
{
Src: []*model.SourceInputSpec{s1},
SC: []*model.ScorecardInputSpec{
{
Origin: "test origin",
},
},
},
},
Query: &model.CertifyScorecardSpec{
Origin: ptrfrom.String("test origin"),
},
ExpSC: []*model.CertifyScorecard{
{
Source: s1out,
Scorecard: &model.Scorecard{
Checks: []*model.ScorecardCheck{},
Origin: "test origin",
},
},
},
},
{
Name: "Ingest same",
InSrc: []*model.SourceInputSpec{s1},
Calls: []call{
{
Src: []*model.SourceInputSpec{s1, s1},
SC: []*model.ScorecardInputSpec{
{
Origin: "test origin",
},
{
Origin: "test origin",
},
},
},
},
Query: &model.CertifyScorecardSpec{
Origin: ptrfrom.String("test origin"),
},
ExpSC: []*model.CertifyScorecard{
{
Source: s1out,
Scorecard: &model.Scorecard{
Checks: []*model.ScorecardCheck{},
Origin: "test origin",
},
},
},
},
{
Name: "Query multiple",
InSrc: []*model.SourceInputSpec{s1},
Calls: []call{
{
Src: []*model.SourceInputSpec{s1, s1, s1},
SC: []*model.ScorecardInputSpec{
{
Origin: "test origin one",
},
{
AggregateScore: 4.4,
Origin: "test origin two",
},
{
AggregateScore: 4.9,
Origin: "test origin two",
},
},
},
},
Query: &model.CertifyScorecardSpec{
Origin: ptrfrom.String("test origin two"),
},
ExpSC: []*model.CertifyScorecard{
{
Source: s1out,
Scorecard: &model.Scorecard{
Checks: []*model.ScorecardCheck{},
AggregateScore: 4.4,
Origin: "test origin two",
},
},
{
Source: s1out,
Scorecard: &model.Scorecard{
Checks: []*model.ScorecardCheck{},
AggregateScore: 4.9,
Origin: "test origin two",
},
},
},
},
{
Name: "Query Source",
InSrc: []*model.SourceInputSpec{s1, s2},
Calls: []call{
{
Src: []*model.SourceInputSpec{s1, s2},
SC: []*model.ScorecardInputSpec{
{
Origin: "test origin",
},
{
Origin: "test origin",
},
},
},
},
Query: &model.CertifyScorecardSpec{
Source: &model.SourceSpec{
Namespace: ptrfrom.String("github.com/jeff"),
},
},
ExpSC: []*model.CertifyScorecard{
{
Source: s1out,
Scorecard: &model.Scorecard{
Checks: []*model.ScorecardCheck{},
Origin: "test origin",
},
},
},
},
}
ctx := s.Ctx
for _, test := range tests {
s.Run(test.Name, func() {
t := s.T()
b, err := GetBackend(s.Client)
if err != nil {
t.Fatalf("Could not instantiate testing backend: %v", err)
}
for _, s := range test.InSrc {
if _, err := b.IngestSource(ctx, *s); err != nil {
t.Fatalf("Could not ingest source: %v", err)
}
}
for _, o := range test.Calls {
_, err := b.IngestScorecards(ctx, o.Src, o.SC)
if (err != nil) != test.ExpIngestErr {
t.Fatalf("did not get expected ingest error, want: %v, got: %v", test.ExpIngestErr, err)
}
if err != nil {
return
}
}
got, err := b.Scorecards(ctx, test.Query)
if (err != nil) != test.ExpQueryErr {
t.Fatalf("did not get expected query error, want: %v, got: %v", test.ExpQueryErr, err)
}
if err != nil {
return
}
if diff := cmp.Diff(test.ExpSC, got, ignoreID); diff != "" {
t.Errorf("Unexpected results. (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 37fecf4

Please sign in to comment.