Skip to content

Commit

Permalink
new test mission
Browse files Browse the repository at this point in the history
  • Loading branch information
tiarajuliarsita committed Dec 16, 2023
2 parents 85f740e + 8172fda commit 2d90ba2
Show file tree
Hide file tree
Showing 18 changed files with 3,704 additions and 126 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: test cover coveralls

test:
# running on directory
go test -v -cover

cover:
# running on directory with view out
go test -coverprofile=cover.out && go tool cover -html=cover.out

coveralls:
# running on features directory
go list ./... | grep service | xargs -n1 go test -cover
180 changes: 180 additions & 0 deletions features/achievement/service/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package service

import (
"errors"
"log"
"recything/features/achievement/entity"
"recything/mocks"
"recything/utils/constanta"

"testing"

"github.com/stretchr/testify/assert"
)

var dataAchievements = []entity.AchievementCore{
{Id: 1, Name: "platinum", TargetPoint: 250000},
{Id: 2, Name: "gold", TargetPoint: 100000},
{Id: 3, Name: "silver", TargetPoint: 50000},
{Id: 4, Name: "bronze", TargetPoint: 0},
}

var dataAchievement = entity.AchievementCore{
Id: 1, Name: "platinum", TargetPoint: 250000,
}

func TestGetAllAchievement(t *testing.T) {

t.Run("Succes GetAll", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

mockRepo.On("GetAllAchievement").Return(dataAchievements, nil)

result, err := achievementService.GetAllAchievement()

assert.NoError(t, err)
assert.NotNil(t, result)
mockRepo.AssertExpectations(t)

})

t.Run("Error", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

mockRepo.On("GetAllAchievement").Return(nil, errors.New("failed"))

result, err := achievementService.GetAllAchievement()

assert.Error(t, err)
assert.Empty(t, result)
mockRepo.AssertExpectations(t)
})
}

func TestUpdateById(t *testing.T) {
t.Run("Sukses Update", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 1
point := 249000

expectedAchievement := dataAchievements[achivementID-1]
mockRepo.On("FindById", achivementID).Return(expectedAchievement, nil)
mockRepo.On("UpdateById", achivementID, point).Return(nil)

err := achievementService.UpdateById(achivementID, point)

assert.NoError(t, err)
assert.Equal(t, achivementID, expectedAchievement.Id)
mockRepo.AssertExpectations(t)
})

t.Run("Invalid ID", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 0
point := 249000

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

t.Run("Data Not Found", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 9
point := 249000

mockRepo.On("FindById", achivementID).Return(dataAchievement, errors.New(constanta.ERROR_RECORD_NOT_FOUND))

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
assert.NotEqual(t, achivementID, dataAchievement.Id)
mockRepo.AssertExpectations(t)
})

t.Run("Invalid Point for Bronze", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 4
point := 100

mockRepo.On("FindById", achivementID).Return(dataAchievements[achivementID-1], nil)

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

t.Run("Invalid Point for Silver", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 3
point := 70000

mockRepo.On("FindById", achivementID).Return(dataAchievements[achivementID-1], nil)

err := achievementService.UpdateById(achivementID, point)
log.Println("er", err)
assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

t.Run("Invalid Point for Gold", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 2
point := 100

mockRepo.On("FindById", achivementID).Return(dataAchievements[achivementID-1], nil)

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

t.Run("Invalid Point for Platinum", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 1
point := 500000

mockRepo.On("FindById", achivementID).Return(dataAchievements[achivementID-1], nil)

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

t.Run("Error Repository", func(t *testing.T) {
mockRepo := new(mocks.AchievementRepositoryInterface)
achievementService := NewAchievementService(mockRepo)

achivementID := 1
point := 249000

mockRepo.On("FindById", achivementID).Return(dataAchievements[achivementID-1],nil)
mockRepo.On("UpdateById", achivementID, point).Return(errors.New("failed"))

err := achievementService.UpdateById(achivementID, point)

assert.Error(t, err)
mockRepo.AssertExpectations(t)
})

}
1 change: 1 addition & 0 deletions features/admin/dto/response/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func AdminCoreToAdminResponseLogin(admin entity.AdminCore, token string) AdminRe
ID: admin.Id,
Fullname: admin.Fullname,
Email: admin.Email,
Image: admin.Image,
Token: token,
}
}
1 change: 1 addition & 0 deletions features/admin/dto/response/respon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ type AdminResponseLogin struct {
ID string `json:"id"`
Fullname string `json:"fullname"`
Email string `json:"email"`
Image string `json:"image"`
Token string `json:"token"`
}
2 changes: 1 addition & 1 deletion features/admin/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (ar *AdminRepository) Create(image *multipart.FileHeader, data entity.Admin
}
dataAdmins.Image = imageURL
} else {
dataAdmins.Image = "https://ui-avatars.com/api/?background=56cc33&color=fff&name=" + data.Fullname
dataAdmins.Image = constanta.IMAGE_ADMIN + data.Fullname
}

tx := ar.db.Create(&dataAdmins)
Expand Down
Loading

0 comments on commit 2d90ba2

Please sign in to comment.