Skip to content

Commit

Permalink
test: fix and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Luthfi Fahlevi committed Nov 14, 2024
1 parent c491f0f commit 7a2e267
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 384 deletions.
4 changes: 2 additions & 2 deletions core/asset/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func TestService_DeleteAsset(t *testing.T) {
Err: errors.New("unknown error"),
},
{
Description: `should call DeleteByURN on repositories by fetching URN when given a UUID`,
Description: `should call DeleteByURN on repositories by fetching URN when given an ID`,
ID: assetID,
Setup: func(ctx context.Context, ar *mocks.AssetRepository, dr *mocks.DiscoveryRepository, lr *mocks.LineageRepository) {
ar.EXPECT().GetByID(ctx, assetID).Return(asset.Asset{ID: assetID, URN: urn}, nil)
Expand All @@ -419,7 +419,7 @@ func TestService_DeleteAsset(t *testing.T) {
Err: nil,
},
{
Description: `should call DeleteByURN on repositories when not given a UUID`,
Description: `should call DeleteByURN on repositories when not given an ID`,
ID: urn,
Setup: func(ctx context.Context, ar *mocks.AssetRepository, dr *mocks.DiscoveryRepository, lr *mocks.LineageRepository) {
ar.EXPECT().DeleteByURN(ctx, urn).Return(nil)
Expand Down
2 changes: 1 addition & 1 deletion core/user/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestContext(t *testing.T) {
t.Run("should return passed user if exist in context", func(t *testing.T) {
passedUser := user.User{UUID: "uuid", Email: "email"}
passedUser := user.User{Email: "[email protected]"}
userCtx := user.NewContext(context.Background(), passedUser)
actual := user.FromContext(userCtx)
if !cmp.Equal(passedUser, actual) {
Expand Down
12 changes: 6 additions & 6 deletions core/user/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ func TestErrors(t *testing.T) {
testCases := []testCase{
{
Description: "not found error return correct error string",
Err: user.NotFoundError{UUID: "uuid", Email: "email"},
ExpectedString: "could not find user with uuid \"uuid\" with email \"email\"",
Err: user.NotFoundError{Email: "[email protected]"},
ExpectedString: "could not find user with email \"[email protected]\"",
},
{
Description: "duplicate error return correct error string",
Err: user.DuplicateRecordError{UUID: "uuid", Email: "email"},
ExpectedString: "duplicate user with uuid \"uuid\" with email \"email\"",
Err: user.DuplicateRecordError{Email: "[email protected]"},
ExpectedString: "duplicate user with email \"[email protected]\"",
},
{
Description: "invalid error return correct error string",
Err: user.InvalidError{UUID: "uuid"},
ExpectedString: "empty field with uuid \"uuid\"",
Err: user.InvalidError{Email: "[email protected]"},
ExpectedString: "empty field with email \"[email protected]\"",
},
}

Expand Down
60 changes: 15 additions & 45 deletions core/user/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,35 @@ import (
)

func TestValidateUser(t *testing.T) {
userID := "user-id"
type testCase struct {
Description string
UUID string
Email string
Setup func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository)
Setup func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository)
ExpectErr error
}

testCases := []testCase{
{
Description: "should return no user error when uuid is empty and email is optional",
UUID: "",
Description: "should return no user error when email is empty",
ExpectErr: user.ErrNoUserInformation,
},
{
Description: "should return user UUID when successfully found user UUID from DB",
UUID: "a-uuid",
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) {
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{ID: "user-id", UUID: inputUUID}, nil)
Description: "should return user ID when successfully found user Email from DB, or not found but can create the new one",
Email: "[email protected]",
Setup: func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository) {
userRepo.EXPECT().GetOrInsertByEmail(ctx, &user.User{Email: inputEmail}).Return(userID, nil)
},
ExpectErr: nil,
},
{
Description: "should return user error if GetByUUID return nil error and empty ID",
UUID: "a-uuid",
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) {
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, nil)
Description: "should return user error if InsertByEmail return empty ID and error",
Email: "[email protected]",
Setup: func(ctx context.Context, inputEmail string, userRepo *mocks.UserRepository) {
mockErr := errors.New("error get or insert user")
userRepo.EXPECT().GetOrInsertByEmail(ctx, &user.User{Email: inputEmail}).Return("", mockErr)
},
ExpectErr: errors.New("fetched user uuid from DB is empty"),
},
{
Description: "should return user UUID when not found user UUID from DB but can create the new one without email",
UUID: "an-email-success-create",
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) {
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, user.NotFoundError{UUID: inputUUID})
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID}).Return("some-id", nil)
},
ExpectErr: nil,
},
{
Description: "should return user UUID when not found user UUID from DB but can create the new one wit email",
UUID: "an-uuid-error",
Email: "an-email-success-create",
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) {
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, user.NotFoundError{UUID: inputUUID})
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID, Email: inputEmail}).Return("some-id", nil)
},
ExpectErr: nil,
},
{
Description: "should return error when not found user ID from DB but can't create the new one",
UUID: "an-uuid-error",
Email: "an-email",
Setup: func(ctx context.Context, inputUUID, inputEmail string, userRepo *mocks.UserRepository) {
mockErr := errors.New("error upserting user")
userRepo.EXPECT().GetByUUID(ctx, inputUUID).Return(user.User{}, mockErr)
userRepo.EXPECT().UpsertByEmail(ctx, &user.User{UUID: inputUUID, Email: inputEmail}).Return("", mockErr)
},
ExpectErr: errors.New("error upserting user"),
ExpectErr: errors.New("error get or insert user"),
},
}

Expand All @@ -81,12 +51,12 @@ func TestValidateUser(t *testing.T) {
mockUserRepo := new(mocks.UserRepository)

if tc.Setup != nil {
tc.Setup(ctx, tc.UUID, tc.Email, mockUserRepo)
tc.Setup(ctx, tc.Email, mockUserRepo)
}

userSvc := user.NewService(logger, mockUserRepo)

_, err := userSvc.ValidateUser(ctx, tc.UUID, tc.Email)
_, err := userSvc.ValidateUser(ctx, tc.Email)

assert.Equal(t, tc.ExpectErr, err)
})
Expand Down
4 changes: 2 additions & 2 deletions core/user/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ func TestValidate(t *testing.T) {
ExpectError: ErrNoUserInformation,
},
{
Title: "should return error invalid if uuid is empty",
Title: "should return error invalid if email is empty",
User: &User{Provider: "provider"},
ExpectError: InvalidError{},
},
{
Title: "should return nil if user is valid",
User: &User{UUID: "some-uuid", Provider: "provider"},
User: &User{Email: "[email protected]", Provider: "provider"},
ExpectError: nil,
},
}
Expand Down
Loading

0 comments on commit 7a2e267

Please sign in to comment.