Skip to content

Commit

Permalink
testing: improvement-testing-voucher (#112)
Browse files Browse the repository at this point in the history
testing: improvement-testing-voucher
  • Loading branch information
masnann authored Dec 18, 2023
2 parents a202721 + 01d88ba commit 1a25ca6
Show file tree
Hide file tree
Showing 3 changed files with 431 additions and 149 deletions.
53 changes: 46 additions & 7 deletions module/feature/auth/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package service

import (
"errors"
"testing"
"time"

"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/auth/dto"
"github.com/capstone-kelompok-7/backend-disappear/module/feature/auth/mocks"
Expand All @@ -10,8 +13,6 @@ import (
utils "github.com/capstone-kelompok-7/backend-disappear/utils/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"testing"
"time"
)

func setupTestService(t *testing.T) (
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestAuthService_RegisterSocial(t *testing.T) {

assert.Error(t, err)
assert.Nil(t, result)
assert.EqualError(t, err, "social ID sudah terdaftar")
assert.EqualError(t, err, "Social ID sudah terdaftar")

authRepo.AssertExpectations(t)
})
Expand Down Expand Up @@ -154,16 +155,29 @@ func TestAuthService_LoginSocial(t *testing.T) {
jwtService.AssertNotCalled(t, "GenerateJWT")
})

t.Run("Failed Case - Error Finding User by Social ID", func(t *testing.T) {
// t.Run("Failed Case - Error Finding User by Social ID", func(t *testing.T) {
// authService, authRepo, _, _, _, _ := setupTestService(t)
// expectedErr := errors.New("some error here")
// authRepo.On("FindUserBySocialID", socialID).Return(nil, expectedErr)

// foundUser, _, err := authService.LoginSocial(socialID)

// assert.Error(t, err)
// assert.Nil(t, foundUser)
// assert.Equal(t, err, expectedErr)

// authRepo.AssertExpectations(t)
// })

t.Run("Failed Case - User is Nil After Finding", func(t *testing.T) {
authService, authRepo, _, _, _, _ := setupTestService(t)
expectedErr := errors.New("some error here")
authRepo.On("FindUserBySocialID", socialID).Return(nil, expectedErr)
authRepo.On("FindUserBySocialID", socialID).Return(nil, nil)

foundUser, _, err := authService.LoginSocial(socialID)

assert.Error(t, err)
assert.Nil(t, foundUser)
assert.Equal(t, err, expectedErr)
assert.Equal(t, "pengguna tidak ditemukan", err.Error())

authRepo.AssertExpectations(t)
})
Expand All @@ -183,4 +197,29 @@ func TestAuthService_LoginSocial(t *testing.T) {
authRepo.AssertExpectations(t)
jwtService.AssertNotCalled(t, "GenerateJWT")
})

t.Run("Failed Case - Error Generating JWT", func(t *testing.T) {
authService, authRepo, jwtService, _, _, _ := setupTestService(t)
user := &entities.UserModels{
ID: 1,
SocialID: socialID,
Email: "test@example.com",
Role: "customer",
LastLogin: time.Now(),
}

authRepo.On("FindUserBySocialID", socialID).Return(user, nil)
authRepo.On("UpdateLastLogin", user.ID, mock.AnythingOfType("time.Time")).Return(nil)
expectedErr := errors.New("JWT generation failed")
jwtService.On("GenerateJWT", user.ID, user.Email, user.Role).Return("", expectedErr)

foundUser, _, err := authService.LoginSocial(socialID)

assert.Error(t, err)
assert.Nil(t, foundUser)
assert.Equal(t, expectedErr, err)

authRepo.AssertExpectations(t)
jwtService.AssertExpectations(t)
})
}
28 changes: 14 additions & 14 deletions module/feature/order/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func TestGetFilterDateRange(t *testing.T) {
}{
{
filterType: "minggu ini",
expectedStart: time.Now().AddDate(0, 0, -int(time.Now().Weekday())).Truncate(24 * time.Hour),
expectedEnd: time.Now().AddDate(0, 0, 7-int(time.Now().Weekday())).Truncate(24 * time.Hour),
expectedStart: time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour),
expectedEnd: time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour),
expectedError: nil,
},
{
Expand Down Expand Up @@ -531,8 +531,8 @@ func TestOrderService_GetOrderByDateRange(t *testing.T) {
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedTotalItems := int64(len(expectedOrders))
orderRepo.On("GetOrderByDateRange", startOfWeek, endOfWeek, 0, perPage).Return(expectedOrders, nil).Once()
Expand Down Expand Up @@ -727,8 +727,8 @@ func TestOrderService_GetOrderByDateRangeAndStatus(t *testing.T) {
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedOrders := []*entities.OrderModels{
{ID: "order123", OrderStatus: "Pending"},
Expand Down Expand Up @@ -803,8 +803,8 @@ func TestOrderService_GetOrderByDateRangeAndStatusAndSearch(t *testing.T) {
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedOrders := []*entities.OrderModels{
{ID: "order123", OrderStatus: "Pending"},
Expand Down Expand Up @@ -878,8 +878,8 @@ func TestOrderService_GetOrderBySearchAndDateRange(t *testing.T) {
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedOrders := []*entities.OrderModels{
{ID: "order123", OrderStatus: "Pending"},
Expand Down Expand Up @@ -1068,8 +1068,8 @@ func TestOrderService_GetOrderByDateRangeAndPaymentStatus(t *testing.T) {
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedOrders := []*entities.OrderModels{
{ID: "order123", PaymentStatus: "Pending"},
Expand Down Expand Up @@ -1144,8 +1144,8 @@ func TestOrderService_GetOrderByDateRangeAndPaymentStatusAndSearch(t *testing.T)
page := 1
perPage := 8

startOfWeek := time.Date(2023, time.December, 10, 0, 0, 0, 0, time.UTC)
endOfWeek := time.Date(2023, time.December, 17, 0, 0, 0, 0, time.UTC)
startOfWeek := time.Now().In(time.UTC).AddDate(0, 0, -int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)
endOfWeek := time.Now().In(time.UTC).AddDate(0, 0, 7-int(time.Now().In(time.UTC).Weekday())).Truncate(24 * time.Hour)

expectedOrders := []*entities.OrderModels{
{ID: "order123", PaymentStatus: "Pending"},
Expand Down
Loading

0 comments on commit 1a25ca6

Please sign in to comment.