Skip to content

Commit

Permalink
fix: update tests vor vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
Dorien Grönwald committed Jan 31, 2025
1 parent 3f87d39 commit f082145
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/server/http/handler/v1/vehicle/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var (
func GetAllVehicles(svc service.VehicleService) fiber.Handler {
return func(c *fiber.Ctx) error {
ctx := c.Context()

var domainData []*domain.Vehicle
var totalCount int64
var err error
Expand Down
101 changes: 97 additions & 4 deletions internal/server/http/handler/v1/vehicle/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/green-ecolution/green-ecolution-backend/internal/entities"
serverEntities "github.com/green-ecolution/green-ecolution-backend/internal/server/http/entities"
"github.com/green-ecolution/green-ecolution-backend/internal/server/http/handler/v1/vehicle"
"github.com/green-ecolution/green-ecolution-backend/internal/server/http/middleware"
"github.com/green-ecolution/green-ecolution-backend/internal/service"
serviceMock "github.com/green-ecolution/green-ecolution-backend/internal/service/_mock"
"github.com/green-ecolution/green-ecolution-backend/internal/utils"
Expand All @@ -19,15 +20,16 @@ import (
)

func TestGetAllVehicles(t *testing.T) {
t.Run("should return all vehicles successfully", func(t *testing.T) {
t.Run("should return all vehicles successfully with default pagination values", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

mockVehicleService.EXPECT().GetAll(
mock.Anything,
).Return(TestVehicles, nil)
).Return(TestVehicles, int64(len(TestVehicles)), nil)

// when
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle", nil)
Expand All @@ -41,14 +43,92 @@ func TestGetAllVehicles(t *testing.T) {
var response serverEntities.VehicleListResponse
err = utils.ParseJSONResponse(resp, &response)
assert.NoError(t, err)

// assert data
assert.Equal(t, 2, len(response.Data))
assert.Equal(t, TestVehicles[0].ID, response.Data[0].ID)

// assert pagination
assert.Empty(t, response.Pagination)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return all vehicles successfully with limit 1 and offset 0", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

mockVehicleService.EXPECT().GetAll(
mock.Anything,
).Return(TestVehicles, int64(len(TestVehicles)), nil)

// when
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle?page=1&limit=1", nil)
resp, err := app.Test(req, -1)
defer resp.Body.Close()

// then
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)

var response serverEntities.VehicleListResponse
err = utils.ParseJSONResponse(resp, &response)
assert.NoError(t, err)

// assert data
assert.Equal(t, 2, len(response.Data))
assert.Equal(t, TestVehicles[0].ID, response.Data[0].ID)

// assert pagination
assert.Equal(t, int32(1), response.Pagination.CurrentPage)
assert.Equal(t, int64(len(TestVehicles)), response.Pagination.Total)
assert.Equal(t, int32(2), *response.Pagination.NextPage)
assert.Empty(t, response.Pagination.PrevPage)
assert.Equal(t, int32((len(TestVehicles))/1), response.Pagination.TotalPages)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return error when page is invalid", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle?page=0&limit=1", nil)
resp, err := app.Test(req, -1)
defer resp.Body.Close()

assert.Nil(t, err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return error when limit is invalid", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle?page=1&limit=0", nil)
resp, err := app.Test(req, -1)
defer resp.Body.Close()

assert.Nil(t, err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return all vehicles by one type successfully", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)
Expand All @@ -70,20 +150,26 @@ func TestGetAllVehicles(t *testing.T) {
var response serverEntities.VehicleListResponse
err = utils.ParseJSONResponse(resp, &response)
assert.NoError(t, err)

// assert data
assert.Equal(t, 1, len(response.Data))

// assert pagination
assert.Empty(t, response.Pagination)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return an empty list when no vehicles are available", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

mockVehicleService.EXPECT().GetAll(
mock.Anything,
).Return([]*entities.Vehicle{}, nil)
).Return([]*entities.Vehicle{}, int64(0), nil)

// when
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle", nil)
Expand All @@ -98,20 +184,26 @@ func TestGetAllVehicles(t *testing.T) {
err = utils.ParseJSONResponse(resp, &response)
assert.NoError(t, err)
assert.NoError(t, err)

// assert data
assert.Equal(t, 0, len(response.Data))

// assert pagination
assert.Empty(t, response.Pagination)

mockVehicleService.AssertExpectations(t)
})

t.Run("should return 500 Internal Server Error when service fails", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)

mockVehicleService.EXPECT().GetAll(
mock.Anything,
).Return(nil, fiber.NewError(fiber.StatusInternalServerError, "service error"))
).Return(nil, int64(0), fiber.NewError(fiber.StatusInternalServerError, "service error"))

// when
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/v1/vehicle", nil)
Expand All @@ -127,6 +219,7 @@ func TestGetAllVehicles(t *testing.T) {

t.Run("should return 400 Bad Request Error when service fails due to invalid type parameter", func(t *testing.T) {
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
mockVehicleService := serviceMock.NewMockVehicleService(t)
handler := vehicle.GetAllVehicles(mockVehicleService)
app.Get("/v1/vehicle", handler)
Expand Down
9 changes: 7 additions & 2 deletions internal/server/http/handler/v1/vehicle/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/green-ecolution/green-ecolution-backend/internal/entities"
"github.com/green-ecolution/green-ecolution-backend/internal/server/http/handler/v1/vehicle"
"github.com/green-ecolution/green-ecolution-backend/internal/server/http/middleware"
serviceMock "github.com/green-ecolution/green-ecolution-backend/internal/service/_mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -20,14 +21,18 @@ func TestRegisterRoutes(t *testing.T) {
t.Run("should call GET handler", func(t *testing.T) {
mockVehicleService := serviceMock.NewMockVehicleService(t)
app := fiber.New()
app.Use(middleware.PaginationMiddleware())
vehicle.RegisterRoutes(app, mockVehicleService)

ctx := context.WithValue(context.Background(), "page", int32(1))
ctx = context.WithValue(ctx, "limit", int32(-1))

mockVehicleService.EXPECT().GetAll(
mock.Anything,
).Return(TestVehicles, nil)
).Return(TestVehicles, int64(len(TestVehicles)), nil)

// when
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "/", nil)
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/", nil)

// then
resp, err := app.Test(req)
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/postgres/vehicle/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *VehicleRepository) GetAll(ctx context.Context) ([]*entities.Vehicle, in
limit = int32(totalCount)
page = 1
}

rows, err := r.store.GetAllVehicles(ctx, &sqlc.GetAllVehiclesParams{
Limit: limit,
Offset: (page - 1) * limit,
Expand Down
1 change: 0 additions & 1 deletion internal/storage/postgres/vehicle/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func TestVehicleRepository_GetAll(t *testing.T) {
}
})


t.Run("should return error on invalid page value", func(t *testing.T) {
// given
suite.ResetDB(t)
Expand Down
6 changes: 5 additions & 1 deletion internal/storage/postgres/watering_plan/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func TestWateringPlanRepository_Create(t *testing.T) {
suite.ResetDB(t)
suite.InsertSeed(t, "internal/storage/postgres/seed/test/watering_plan")

testVehicles, err := suite.Store.GetAllVehicles(context.Background())
testVehicles, err := suite.Store.GetAllVehicles(context.Background(), &sqlc.GetAllVehiclesParams{
Limit: 2,
Offset: 0,
})

if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion internal/storage/postgres/watering_plan/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ func TestWateringPlanRepository_Update(t *testing.T) {
suite.ResetDB(t)
suite.InsertSeed(t, "internal/storage/postgres/seed/test/watering_plan")

testVehicles, err := suite.Store.GetAllVehicles(context.Background())
testVehicles, err := suite.Store.GetAllVehicles(context.Background(), &sqlc.GetAllVehiclesParams{
Limit: 2,
Offset: 0,
})
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit f082145

Please sign in to comment.