Skip to content

Commit

Permalink
Add a unit test for switch of a profile state to pending
Browse files Browse the repository at this point in the history
We recently suspected that one of our stored procedures is not behaving
as it should. This turned out to be false, but during debugging of the
stored procedure I already wrote a unit test for the behaviour that was
not working at the moment.

It makes sense to submit it in case we regress in the future.
  • Loading branch information
jhrozek committed Apr 6, 2024
1 parent deefb74 commit 0e034f9
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions internal/db/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,3 +859,52 @@ func TestListRuleEvaluations(t *testing.T) {
})
}
}

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

randomEntities := createTestRandomEntities(t)
require.NotNil(t, randomEntities)

profile := createRandomProfile(t, randomEntities.proj.ID, []string{})
require.NotEmpty(t, profile)

_, err := testQueries.UpsertProfileForEntity(context.Background(), UpsertProfileForEntityParams{
ProfileID: profile.ID,
Entity: EntitiesRepository,
ContextualRules: json.RawMessage(`{"key": "value"}`), // the content doesn't matter
})
require.NoError(t, err)

id, err := testQueries.UpsertRuleEvaluations(context.Background(), UpsertRuleEvaluationsParams{
ProfileID: profile.ID,
RepositoryID: uuid.NullUUID{
UUID: randomEntities.repo.ID,
Valid: true,
},
RuleTypeID: randomEntities.ruleType1.ID,
RuleName: randomEntities.ruleType1.Name,
Entity: EntitiesRepository,
})
require.NoError(t, err)
require.NotNil(t, id)

_, err = testQueries.UpsertRuleDetailsEval(context.Background(), UpsertRuleDetailsEvalParams{
RuleEvalID: id,
Status: EvalStatusTypesFailure,
})
require.NoError(t, err)

prfStatusRow := profileIDStatusByIdAndProject(t, profile.ID, randomEntities.proj.ID)
require.Equal(t, EvalStatusTypesFailure, prfStatusRow.ProfileStatus)

err = testQueries.DeleteRuleStatusesForProfileAndRuleType(context.Background(), DeleteRuleStatusesForProfileAndRuleTypeParams{
ProfileID: profile.ID,
RuleTypeID: randomEntities.ruleType1.ID,
RuleName: randomEntities.ruleType1.Name,
})
require.NoError(t, err)

prfStatusRow = profileIDStatusByIdAndProject(t, profile.ID, randomEntities.proj.ID)
require.Equal(t, EvalStatusTypesPending, prfStatusRow.ProfileStatus)
}

0 comments on commit 0e034f9

Please sign in to comment.