Repository navigation
test(profiles): add comprehensive unit tests for profile service engine - #7071
ravindra-RKB wants to merge 1 commit into
Conversation
evankanderson
left a comment
There was a problem hiding this comment.
Thanks for writing (prompting?) this coverage. Unfortunately, the repeated subtest style doesn't really match the pattern we use elsewhere. For example, see pkg/ruletypes/service_test.go -- that one may have gone a bit farther on helpers and DRY (don't repeat yourself) than necessary.
| t.Parallel() | ||
|
|
||
| ctrl := gomock.NewController(t) | ||
| store := mockdb.NewMockStore(ctrl) | ||
| pub := mockevents.NewMockPublisher(ctrl) | ||
| sel := mocksel.NewMockSelectionChecker(ctrl) | ||
| svc := profiles.NewProfileService(pub, sel) | ||
|
|
||
| ruleTypeParam(store) | ||
|
|
||
| store.EXPECT().ListProfilesByProjectIDAndLabel(gomock.Any(), gomock.Any()).Return(nil, nil) |
There was a problem hiding this comment.
There's a lot of common setup and checking in these tests. Can you refactor this to use the "table driven tests" model used elsewhere in the codebase, possibly with a setup(*mockdb.MockStore, ...) and profileArg() fields?
If not, it would be good to explain why the pattern that works elsewhere doesn't work here in a comment. My suspicion (from having seen it myself) is that it's easier for the LLM to generate these types of test cases without refactoring out the common code. Unfortunately, that doesn't make it easier for humans to read the interesting parts of the code, nor does it make it easier to catch unintentional drift between different t.Run() blocks.
There was a problem hiding this comment.
Refactored the tests across TestDeleteProfile, TestCreateProfile, TestUpdateProfile, and TestPatchProfile to follow the standard table-driven pattern with a shared test environment helper and scenario structs (ProfileArg, DBSetup(store, pub, sel), ExpectedError). This keeps the test cases DRY and matches the conventions used elsewhere in the codebase.
48be45c to
83427fb
Compare
Signed-off-by: Ravindra Kumar Bundela <ravindrabundela1205@gmail.com>
83427fb to
edc8660
Compare
Summary
This PR introduces a comprehensive unit test suite (
service_test.go, +1,241 LOC) for the profile service engine inpkg/profiles/service.go.Prior to this PR,
service.gohad 0% unit test coverage (explicitly acknowledged in a codebase comment: "Note that there are no unit tests for these methods in this package..."). This PR implements complete unit test coverage across all core service lifecycle methods (CreateProfile,UpdateProfile,PatchProfile, andDeleteProfile) using gomock stores, selection checkers, and event publishers.Detailed Test Coverage
1. Profile Deletion (
TestDeleteProfile)qtx.DeleteProfile, and returning the deleted profile record.GetProfileByNameAndLockwhen the identifier argument is not a valid UUID string.codes.NotFound/ user-visible errors when DB queries returnsql.ErrNoRows.GetProfileByIDAndLockorDeleteProfileencounters database failure.codes.InvalidArgument) when attempting to delete a profile managed by a bundle subscription (SubscriptionID.Valid == true).2. Profile Creation (
TestCreateProfile)CreateProfileForEntity, and reconciliation event publication toTopicQueueReconcileProfileInit.dbEnt.Valid = false).23505) and returns user-visibleAlreadyExistserrors.ListProfilesByProjectIDAndLabel).CreateProfile,UpsertRuleInstance, or selector creation DB queries fail.3. Profile Updates (
TestUpdateProfile)DeleteNonUpdatedRules.len(rules) == 0),DeleteProfileForEntityis executed for that entity.cannot change profile name).cannot change profile project).codes.NotFoundif target profile does not exist.updateSelectorsworkflow including selector syntax checking viaSelectionCheckerandDeleteSelectorsByProfileID.DeleteNonUpdatedRulesorupdateSelectorsencounters database errors.4. Profile Patching (
TestPatchProfile)displayName,remediate) viafieldmaskpb.FieldMask, preserving existing rules and properties.GetProfileByProjectAndIDreturns multiple rows or unexpected records, an error is returned.read replica down), lock acquisition timeouts, and non-existent profile lookups.Verification
t.Parallel()).gci write -s standard -s default -s "prefix(github.com/mindersec/minder)".go test -v -count=1 ./pkg/profiles/... go vet ./pkg/profiles/...