Untitled
unknown
plain_text
2 years ago
5.5 kB
9
Indexable
func TestGetSectionByID(t *testing.T) {
// Case 1 : Success get section by id, return 200 and the section
t.Run("Success get section by id", func(t *testing.T) {
// Arrange
// - Create the mock of the service
service_mock := service.NewSectionServiceMock()
// - Set the function to return the section
service_mock.FindByIDFunc = func(id int) (internal.Section, error) {
// Business logic
// ...
return internal.Section{
ID: 1,
SectionNumber: 1,
CurrentTemperature: 0,
MinimumTemperature: -5,
CurrentCapacity: 50,
MinimumCapacity: 20,
MaximumCapacity: 100,
WarehouseID: 1,
ProductTypeID: 1,
}, nil
}
// - Create the handler
hd := handler.NewSectionDefault(service_mock)
// - Create the handler function (http.HandlerFunc) GetSection
handlerFunc := hd.GetByID()
// - Set the router
router := chi.NewRouter()
router.Get("/api/v1/sections/{id}", handlerFunc)
// - Create the Request using httptest package
request := httptest.NewRequest("GET", "/api/v1/sections/1", nil)
// - Create the ResponseRecorder
response := httptest.NewRecorder()
// Act
router.ServeHTTP(response, request)
// Assert
// Expected results
expectedCode := 200
expectedBody := `{
"data": {
"id": 1,
"section_number": 1,
"current_temperature": 0,
"minimum_temperature": -5,
"current_capacity": 50,
"minimum_capacity": 20,
"maximum_capacity": 100,
"warehouse_id": 1,
"product_type_id": 1
}
}`
expectedHeaders := "application/json"
// - Check the status code
require.Equal(t, expectedCode, response.Code)
// - Check the response body
require.JSONEq(t, expectedBody, response.Body.String())
// - Check HTTP headers
require.Equal(t, expectedHeaders, response.Header().Get("Content-Type"))
// - Check the function was called once
require.Equal(t, 1, service_mock.Calls.FindByID)
// - Check the function was called with the correct parameter
require.Equal(t, 1, service_mock.Args.FindByID[0])
// - Check Error
require.Nil(t, nil)
})
// Case 2 : Error get section by id not found, return 404 and the message section not found
t.Run("Error get section by id not found", func(t *testing.T) {
// Arrange
// - Create a new section service mock
service_mock := service.NewSectionServiceMock()
// - Set the function to return an error
service_mock.FindByIDFunc = func(id int) (internal.Section, error) {
// Business logic
// ...
return internal.Section{}, internal.ErrSectionNotFound
}
// - Create the handler
hd := handler.NewSectionDefault(service_mock)
// - Create the handler function (http.HandlerFunc) GetSection
handlerFunc := hd.GetByID()
// - Set the router
router := chi.NewRouter()
router.Get("/api/v1/sections/{id}", handlerFunc)
// - Create the Request using httptest package
request := httptest.NewRequest("GET", "/api/v1/sections/1", nil)
// - Create the ResponseRecorder
response := httptest.NewRecorder()
// Act
// - Execute the handler function (http.HandlerFunc) GetSection
router.ServeHTTP(response, request)
// Assert
// Expected results
expectedCode := 404
expectedBody := `{
"status": "Not Found",
"message": "Section not found"
}`
expectedHeaders := "application/json"
expectedCalls := 1
expectedArgumentsService := 1
expectedError := internal.ErrSectionNotFound
// - Check the status code
require.Equal(t, expectedCode, response.Code)
// - Check the response body
require.JSONEq(t, expectedBody, response.Body.String())
// - Check HTTP headers
require.Equal(t, expectedHeaders, response.Header().Get("Content-Type"))
// - Check the function was called once
require.Equal(t, expectedCalls, service_mock.Calls.FindByID)
// - Check the function was called with the correct parameter
require.Equal(t, expectedArgumentsService, service_mock.Args.FindByID[0])
// - Check the error
require.Error(t, expectedError)
})
// Case 3 : Error get section by id (Invalid ID), return 500 and the message error internal server
t.Run("Error get section by id (Invalid ID)", func(t *testing.T) {
// Arrange
// - Create a new section service mock
service_mock := service.NewSectionServiceMock()
// - Create the handler
hd := handler.NewSectionDefault(service_mock)
// - Create the handler function (http.HandlerFunc) GetSection
handlerFunc := hd.GetByID()
// - Set the router
router := chi.NewRouter()
router.Get("/api/v1/sections/{id}", handlerFunc)
// - Create the Request using httptest package
request := httptest.NewRequest("GET", "/api/v1/sections/asd1", nil)
// - Create the ResponseRecorder
response := httptest.NewRecorder()
// Act
// - Execute the handler function (http.HandlerFunc) GetSection
router.ServeHTTP(response, request)
// Assert
// Expected results
expectedCode := 400
expectedBody := `{"message":"Error input request", "status":"Bad Request"}`
expectedHeaders := "application/json"
expectedCalls := 0
expectedError := internal.ErrSectionNotFound
// - Check the status code
require.Equal(t, expectedCode, response.Code)
// - Check the response body
require.JSONEq(t, expectedBody, response.Body.String())
// - Check HTTP headers
require.Equal(t, expectedHeaders, response.Header().Get("Content-Type"))
// - Check the function was called once
require.Equal(t, expectedCalls, service_mock.Calls.FindByID)
// - Check the error
require.Error(t, expectedError)
})
}
Editor is loading...
Leave a Comment