Skip to content

Commit

Permalink
Fix GetEvaluationHistory from clause. (#4364)
Browse files Browse the repository at this point in the history
  • Loading branch information
blkt authored Sep 4, 2024
1 parent a50982b commit 87aae86
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
2 changes: 1 addition & 1 deletion database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ FROM evaluation_statuses s
JOIN rule_instances ri ON ere.rule_id = ri.id
JOIN rule_type rt ON ri.rule_type_id = rt.id
JOIN profiles p ON ri.profile_id = p.id
JOIN projects j ON ei.project_id = j.id
JOIN entity_instances ei ON ere.entity_instance_id = ei.id
JOIN projects j ON ei.project_id = j.id
LEFT JOIN remediation_events re ON re.evaluation_id = s.id
LEFT JOIN alert_events ae ON ae.evaluation_id = s.id
WHERE s.id = sqlc.arg(evaluation_id) AND j.id = sqlc.arg(project_id);
Expand Down
2 changes: 1 addition & 1 deletion internal/db/eval_history.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions internal/db/eval_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -671,6 +672,92 @@ func TestListEvaluationHistoryPagination(t *testing.T) {
}
}

func TestGetEvaluationHistory(t *testing.T) {
t.Parallel()

org := createRandomOrganization(t)
proj := createRandomProject(t, org.ID)
prov := createRandomProvider(t, proj.ID)

repos := make([]Repository, 0)
for i := 0; i < 10; i++ {
repos = append(repos, createRandomRepository(t, proj.ID, prov))
}
ruleType1 := createRandomRuleType(t, proj.ID)
profile1 := createRandomProfile(t, proj.ID, []string{})
riID1 := createRandomRuleInstance(
t,
proj.ID,
profile1.ID,
ruleType1.ID,
)

ess := make([]uuid.UUID, 0)
for i := 0; i < 10; i++ {
ere := createRandomEvaluationRuleEntity(t, riID1, repos[i].ID)
ess = append(ess, createRandomEvaluationStatus(t, ere))
}

absent, err := uuid.NewRandom()
require.NoError(t, err)

tests := []struct {
name string
params GetEvaluationHistoryParams
checkf func(*testing.T, GetEvaluationHistoryRow)
norows bool
error bool
}{
{
name: "present",
params: GetEvaluationHistoryParams{
EvaluationID: ess[0],
ProjectID: proj.ID,
},
checkf: func(t *testing.T, row GetEvaluationHistoryRow) {
t.Helper()
require.Equal(t, ess[0], row.EvaluationID)
},
},
{
name: "absent",
params: GetEvaluationHistoryParams{
EvaluationID: absent,
ProjectID: proj.ID,
},
norows: true,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

row, err := testQueries.GetEvaluationHistory(
context.Background(),
tt.params,
)

if tt.error {
require.Error(t, err)
require.Nil(t, row)
return
}

if tt.norows {
require.Error(t, err)
require.True(t, errors.Is(err, sql.ErrNoRows))
return
}

require.NoError(t, err)
tt.checkf(t, row)
})
}
}

func fullRepoName(r Repository) string {
return fmt.Sprintf("%s/%s", r.RepoOwner, r.RepoName)
}
Expand Down

0 comments on commit 87aae86

Please sign in to comment.