diff --git a/features/trash_exchange/service/cover.out b/features/trash_exchange/service/cover.out new file mode 100644 index 0000000..2a4768c --- /dev/null +++ b/features/trash_exchange/service/cover.out @@ -0,0 +1,39 @@ +mode: set +recything/features/trash_exchange/service/service.go:26.284,33.2 1 1 +recything/features/trash_exchange/service/service.go:36.133,40.21 3 1 +recything/features/trash_exchange/service/service.go:40.21,42.3 1 1 +recything/features/trash_exchange/service/service.go:44.2,45.16 2 1 +recything/features/trash_exchange/service/service.go:45.16,47.3 1 1 +recything/features/trash_exchange/service/service.go:49.2,50.16 2 1 +recything/features/trash_exchange/service/service.go:50.16,52.3 1 1 +recything/features/trash_exchange/service/service.go:53.2,58.51 5 1 +recything/features/trash_exchange/service/service.go:58.51,61.28 2 1 +recything/features/trash_exchange/service/service.go:61.28,63.4 1 1 +recything/features/trash_exchange/service/service.go:65.3,68.17 4 1 +recything/features/trash_exchange/service/service.go:68.17,70.4 1 1 +recything/features/trash_exchange/service/service.go:72.3,78.36 5 1 +recything/features/trash_exchange/service/service.go:81.2,92.16 9 1 +recything/features/trash_exchange/service/service.go:92.16,94.3 1 1 +recything/features/trash_exchange/service/service.go:95.2,96.16 2 1 +recything/features/trash_exchange/service/service.go:96.16,98.3 1 0 +recything/features/trash_exchange/service/service.go:100.2,102.16 3 1 +recything/features/trash_exchange/service/service.go:102.16,104.3 1 1 +recything/features/trash_exchange/service/service.go:106.2,106.33 1 1 +recything/features/trash_exchange/service/service.go:106.33,109.17 3 1 +recything/features/trash_exchange/service/service.go:109.17,111.4 1 0 +recything/features/trash_exchange/service/service.go:114.2,114.20 1 1 +recything/features/trash_exchange/service/service.go:118.75,119.14 1 1 +recything/features/trash_exchange/service/service.go:119.14,121.3 1 1 +recything/features/trash_exchange/service/service.go:123.2,124.16 2 1 +recything/features/trash_exchange/service/service.go:124.16,126.3 1 1 +recything/features/trash_exchange/service/service.go:128.2,128.12 1 1 +recything/features/trash_exchange/service/service.go:132.151,134.16 2 1 +recything/features/trash_exchange/service/service.go:134.16,136.3 1 1 +recything/features/trash_exchange/service/service.go:138.2,141.16 3 1 +recything/features/trash_exchange/service/service.go:141.16,143.3 1 1 +recything/features/trash_exchange/service/service.go:145.2,145.45 1 1 +recything/features/trash_exchange/service/service.go:149.107,150.14 1 1 +recything/features/trash_exchange/service/service.go:150.14,152.3 1 1 +recything/features/trash_exchange/service/service.go:154.2,155.16 2 1 +recything/features/trash_exchange/service/service.go:155.16,157.3 1 1 +recything/features/trash_exchange/service/service.go:159.2,159.29 1 1 diff --git a/features/trash_exchange/service/service_test.go b/features/trash_exchange/service/service_test.go new file mode 100644 index 0000000..cf2cd1e --- /dev/null +++ b/features/trash_exchange/service/service_test.go @@ -0,0 +1,727 @@ +package service + +// Import paket-paket yang dibutuhkan untuk unit test +import ( + "errors" + "testing" + + de "recything/features/drop-point/entity" + tce "recything/features/trash_category/entity" + tee "recything/features/trash_exchange/entity" + ue "recything/features/user/entity" + "recything/mocks" + "recything/utils/constanta" + "recything/utils/pagination" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/mock" +) + +func TestCreateTrashExchangeSuccess(t *testing.T) { + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + mockData := tee.TrashExchangeCore{ + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(mockData, nil) + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(nil) + + result, err := trashExchangeService.CreateTrashExchange(mockData) + + assert.NoError(t, err) + assert.NotNil(t, result) +} + +func TestCreateTrashExchangeEmptyError(t *testing.T) { + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + mockData := tee.TrashExchangeCore{ + Name: "", + EmailUser: "", + DropPointName: "", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + // Simulate an error in CreateTrashExchange + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(tee.TrashExchangeCore{}, errors.New("harap lengkapi data dengan benar")) + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(nil) + + result, err := trashExchangeService.CreateTrashExchange(mockData) + + assert.Error(t, err) + assert.Equal(t, "", result.Id) + assert.EqualError(t, err, "error : harap lengkapi data dengan benar") +} + +func TestCreateTrashExchangeInvalidEmailError(t *testing.T) { + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + mockData := tee.TrashExchangeCore{ + Name: "juwan", + EmailUser: "juwan@gmail.com", + DropPointName: "wasebutentuya", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + // Simulate an error in CreateTrashExchange + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(tee.TrashExchangeCore{}, errors.New("harap lengkapi data dengan benar")) + userRepo.On("FindByEmail", "juwan@gmail.com").Return(ue.UsersCore{}, errors.New("pengguna dengan email tersebut tidak ditemukan")) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(nil) + + result, err := trashExchangeService.CreateTrashExchange(mockData) + + assert.Error(t, err) + assert.Equal(t, "", result.Id) + assert.EqualError(t, err, "pengguna dengan email tersebut tidak ditemukan") +} + +func TestCreateTrashExchangeInvalidDropPointError(t *testing.T) { + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + mockData := tee.TrashExchangeCore{ + Name: "juwan", + EmailUser: "juwan@gmail.com", + DropPointName: "wasebutentuya", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "juwan@gmail.com", + Point: 100, + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + // Simulate an error in CreateTrashExchange + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(tee.TrashExchangeCore{}, errors.New("harap lengkapi data dengan benar")) + userRepo.On("FindByEmail", "juwan@gmail.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "wasebutentuya").Return(de.DropPointsCore{}, errors.New("nama drop point tidak ditemukan")) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(nil) + + result, err := trashExchangeService.CreateTrashExchange(mockData) + + assert.Error(t, err) + assert.Equal(t, "", result.Id) + assert.EqualError(t, err, "nama drop point tidak ditemukan") +} + +func TestCreateTrashExchangeEmptyDetailError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + // Mock data with empty TrashType and Amount + mockData := tee.TrashExchangeCore{ + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "", Amount: 10.5}, // Empty TrashType + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + // Expectations + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + + // Mock repository response for the first detail + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + + // Mock repository response for the second detail + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + + // Call the method to be tested + result, err := trashExchangeService.CreateTrashExchange(mockData) + + // Assertions + assert.Error(t, err) + assert.Equal(t, errors.New("error : harap lengkapi data dengan benar"), err) + assert.Equal(t, tee.TrashExchangeCore{}, result) +} + +func TestCreateTrashExchangeTrashCategoryNotFoundError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + // Mock data with empty TrashType and Amount + mockData := tee.TrashExchangeCore{ + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + // Expectations + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + + // Mock repository response for the first detail + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory1, errors.New("kategori sampah tidak ditemukan")) + + // Call the method to be tested + result, err := trashExchangeService.CreateTrashExchange(mockData) + + // Assertions + assert.Error(t, err) + assert.Equal(t, errors.New("kategori sampah tidak ditemukan"), err) + assert.Equal(t, tee.TrashExchangeCore{}, result) +} + +func TestCreateTrashExchangeUpdateUserError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + // Mock data + mockData := tee.TrashExchangeCore{ + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(mockData, nil) + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + + // Simulate an error while updating user points + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(errors.New("gagal memperbarui nilai point pengguna")) + + // Call the method to be tested + result, err := trashExchangeService.CreateTrashExchange(mockData) + + // Assertions + assert.Error(t, err) + assert.Equal(t, "", result.Id) + assert.Equal(t, errors.New("gagal memperbarui nilai point pengguna"), err) +} + +func TestCreateTrashExchangeError(t *testing.T) { + repoData := new(mocks.TrashExchangeRepositoryInterface) + userRepo := new(mocks.UsersRepositoryInterface) + dropPointRepo := new(mocks.DropPointRepositoryInterface) + trashCategoryRepo := new(mocks.TrashCategoryRepositoryInterface) + + trashExchangeService := NewTrashExchangeService(repoData, dropPointRepo, userRepo, trashCategoryRepo) + + mockData := tee.TrashExchangeCore{ + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + TrashExchangeDetails: []tee.TrashExchangeDetailCore{ + {TrashType: "Plastic", Amount: 10.5}, + {TrashType: "Paper", Amount: 5.0}, + }, + } + + mockUser := ue.UsersCore{ + Id: "user123", + Email: "john@example.com", + Point: 100, + } + + mockDropPoint := de.DropPointsCore{ + Id: "dropPoint123", + Name: "Recycling Center", + } + + mockTrashCategory1 := tce.TrashCategoryCore{ + ID: "category1", + TrashType: "Plastic", + Point: 5, + Unit: "kg", + } + + mockTrashCategory2 := tce.TrashCategoryCore{ + ID: "category2", + TrashType: "Paper", + Point: 3, + Unit: "kg", + } + + for _, detail := range mockData.TrashExchangeDetails { + repoData.On("CreateTrashExchangeDetails", mock.AnythingOfType("entity.TrashExchangeDetailCore")). + Return(detail, nil) + } + + // Simulate an error in CreateTrashExchange + repoData.On("CreateTrashExchange", mock.AnythingOfType("entity.TrashExchangeCore")).Return(tee.TrashExchangeCore{}, errors.New("harap lengkapi data dengan benar")) + userRepo.On("FindByEmail", "john@example.com").Return(mockUser, nil) + dropPointRepo.On("GetDropPointByName", "Recycling Center").Return(mockDropPoint, nil) + trashCategoryRepo.On("GetByType", "Plastic").Return(mockTrashCategory1, nil) + trashCategoryRepo.On("GetByType", "Paper").Return(mockTrashCategory2, nil) + userRepo.On("UpdateById", "user123", mock.AnythingOfType("entity.UsersCore")).Return(nil) + + result, err := trashExchangeService.CreateTrashExchange(mockData) + + assert.Error(t, err) + assert.Equal(t, "", result.Id) + assert.EqualError(t, err, "gagal menyimpan data trash exchange") +} + +func TestDeleteTrashExchangeByIdSuccess(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + trashExchangeId := "trashExchange123" + + // Expectations + repoData.On("DeleteTrashExchangeById", trashExchangeId).Return(nil) + + // Call the method to be tested + err := trashExchangeService.DeleteTrashExchangeById(trashExchangeId) + + // Assertions + assert.NoError(t, err) +} + +func TestDeleteTrashExchangeByIdEmptyIdError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + trashExchangeId := "" + + // Expectations + repoData.On("DeleteTrashExchangeById", trashExchangeId).Return(errors.New("error")) + + // Call the method to be tested + err := trashExchangeService.DeleteTrashExchangeById(trashExchangeId) + + // Assertions + assert.Error(t, err) +} + +func TestDeleteTrashExchangeByIdError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + trashExchangeId := "idbaru" + + // Expectations + repoData.On("DeleteTrashExchangeById", trashExchangeId).Return(errors.New("error")) + + // Call the method to be tested + err := trashExchangeService.DeleteTrashExchangeById(trashExchangeId) + + // Assertions + assert.Error(t, err) +} + +func TestGetAllTrashExchangeSuccess(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + page := "1" + limit := "10" + search := "example" + + // Mock repository response + mockTrashExchanges := []tee.TrashExchangeCore{ + { + Id: "1", + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + // ... other fields + }, + // Add more mock data as needed + } + + mockPageInfo := pagination.PageInfo{ + CurrentPage: 1, + Limit: 10, + LastPage: 5, + } + + mockCount := 20 + + // Expectations + repoData.On("GetAllTrashExchange", 1, 10, search).Return(mockTrashExchanges, mockPageInfo, mockCount, nil) + + // Call the method to be tested + trashExchanges, pageInfo, count, err := trashExchangeService.GetAllTrashExchange(page, limit, search) + + // Assertions + assert.NoError(t, err) + assert.NotNil(t, trashExchanges) + assert.Equal(t, mockTrashExchanges, trashExchanges) + assert.Equal(t, mockPageInfo, pageInfo) + assert.Equal(t, mockCount, count) + + // Assert that the expected method was called with the correct arguments + repoData.AssertExpectations(t) +} +func TestGetAllTrashExchangeError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + page := "1" + limit := "10" + search := "example" + + // Mock repository error response + mockError := errors.New("repository error") + + // Expectations for the error case + repoData.On("GetAllTrashExchange", 1, 10, search).Return(nil, pagination.PageInfo{}, 0, mockError) + + // Call the method to be tested + trashExchanges, _, _, err := trashExchangeService.GetAllTrashExchange(page, limit, search) + + // Assertions for the error case + assert.Error(t, err) + assert.Nil(t, trashExchanges) + assert.EqualError(t, err, mockError.Error()) +} + +func TestGetAllTrashExchangeInvalidPageParamsError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + page := "1" + limit := "unlimited" + search := "example" + + // Mock repository error response + mockError := errors.New("limit harus berupa angka") + + // Expectations for the error case + repoData.On("GetAllTrashExchange", 1, 10, search).Return(nil, pagination.PageInfo{}, 0, mockError) + + // Call the method to be tested + trashExchanges, _, _, err := trashExchangeService.GetAllTrashExchange(page, limit, search) + + // Assertions for the error case + assert.Error(t, err) + assert.Nil(t, trashExchanges) + assert.EqualError(t, err, mockError.Error()) +} + +func TestGetTrashExchangeByIdSuccess(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + trashExchangeID := "123" + + // Mock repository response + mockTrashExchange := tee.TrashExchangeCore{ + Id: "123", + Name: "John Doe", + EmailUser: "john@example.com", + DropPointName: "Recycling Center", + // ... other fields + } + + // Expectations + repoData.On("GetTrashExchangeById", trashExchangeID).Return(mockTrashExchange, nil) + + // Call the method to be tested + result, err := trashExchangeService.GetTrashExchangeById(trashExchangeID) + + // Assertions + assert.NoError(t, err) + assert.NotNil(t, result) + assert.Equal(t, mockTrashExchange, result) + + // Assert that the expected method was called with the correct arguments + repoData.AssertExpectations(t) +} + +func TestGetTrashExchangeByIdInvalidIDError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + invalidTrashExchangeID := "" + + // Call the method to be tested + result, err := trashExchangeService.GetTrashExchangeById(invalidTrashExchangeID) + + // Assertions for invalid ID case + assert.Error(t, err) + assert.Equal(t, constanta.ERROR_ID_INVALID, err.Error()) + assert.Equal(t, tee.TrashExchangeCore{}, result) + + // Assert that the repository method was not called + repoData.AssertNotCalled(t, "GetTrashExchangeById", mock.Anything) +} + +func TestGetTrashExchangeByIdRepositoryError(t *testing.T) { + // Create mock repository + repoData := new(mocks.TrashExchangeRepositoryInterface) + trashExchangeService := NewTrashExchangeService(repoData, nil, nil, nil) + + // Mock data + trashExchangeID := "123" + + // Expectations for repository error case + repoData.On("GetTrashExchangeById", trashExchangeID).Return(tee.TrashExchangeCore{}, errors.New("repository error")) + + // Call the method to be tested + result, err := trashExchangeService.GetTrashExchangeById(trashExchangeID) + + // Assertions for repository error case + assert.Error(t, err) + assert.Equal(t, "repository error", err.Error()) + assert.Equal(t, tee.TrashExchangeCore{}, result) + + // Assert that the repository method was called with the correct arguments + repoData.AssertExpectations(t) +}