diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 73c06882d0..04407c0016 100644 --- a/database/query/eval_history.sql +++ b/database/query/eval_history.sql @@ -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); diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index a9f1fa5c86..9735f2b7dd 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -67,8 +67,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 = $1 AND j.id = $2 diff --git a/internal/db/eval_history_test.go b/internal/db/eval_history_test.go index 2cdabbbebb..7ececb29bf 100644 --- a/internal/db/eval_history_test.go +++ b/internal/db/eval_history_test.go @@ -19,6 +19,7 @@ import ( "context" "database/sql" "encoding/json" + "errors" "fmt" "testing" "time" @@ -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) }