Untitled
unknown
plain_text
a year ago
4.7 kB
4
Indexable
func TestEmployeeDefault_ReadCases(t *testing.T) { // TEST CASES FOR SPRINT 3 t.Run("Case read 01 -> find_all: Should return a list of employees", func(t *testing.T) { // ARRANGE // -service sv := service.NewEmployeeDefaultMock() // -handler hd := handler.NewEmployeeDefault(sv) handlerFunc := hd.GetAll() // set the find all mock sv.On("FindAll").Return([]internal.Employee{ { ID: 1, CardNumberID: 1001, FirstName: "John", LastName: "Doe", WarehouseID: 1, }, { ID: 2, CardNumberID: 1002, FirstName: "Jane", LastName: "Doe", WarehouseID: 1, }, }, nil) // request // use of http.NewRequest to create a new request for testing req := httptest.NewRequest(http.MethodGet, "/api/v1/employees", nil) //response res := httptest.NewRecorder() //ACT handlerFunc(res, req) // ASSERT // set the expected body expectedBody := `{ "data": [ { "id": 1, "card_number_id": 1001, "first_name": "John", "last_name": "Doe", "warehouse_id": 1 }, { "id": 2, "card_number_id": 1002, "first_name": "Jane", "last_name": "Doe", "warehouse_id": 1 } ] }` // compare status code require.Equal(t, http.StatusOK, res.Code) // compare body require.JSONEq(t, expectedBody, res.Body.String()) }) t.Run("Case read 02 -> find_by_id_non_existent: Should return error message", func(t *testing.T) { // ARRANGE // -service sv := service.NewEmployeeDefaultMock() // -handler hd := handler.NewEmployeeDefault(sv) handlerFunc := hd.GetByID() // set the find by id mock sv.On("FindByID", 1).Return(internal.Employee{}, internal.ErrEmployeeRepositoryNotFound) //request // use of httptest.NewRequest to create a new request for testing req := httptest.NewRequest(http.MethodGet, "/api/v1/employees", nil) // -chi context for route chictx := chi.NewRouteContext() // add params to context chictx.URLParams.Add("id", "1") // create new context from parent context with url params req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chictx)) // response res := httptest.NewRecorder() // ACT handlerFunc(res, req) // ASSERT // set the expected body expectedBody := `{ "status": "Not Found", "message": "Employee not found" }` // compare status code require.Equal(t, http.StatusNotFound, res.Code) // compare body require.JSONEq(t, expectedBody, res.Body.String()) }) t.Run("Case read 03 -> find_by_id_existent: Should return existing employee", func(t *testing.T) { // ARRANGE // -service sv := service.NewEmployeeDefaultMock() // -handler hd := handler.NewEmployeeDefault(sv) handlerFunc := hd.GetByID() // set the find by id mock sv.On("FindByID", 1).Return(internal.Employee{ ID: 1, CardNumberID: 1001, FirstName: "John", LastName: "Doe", WarehouseID: 1, }, nil) // request // use of httptest.NewRequest to create a new request for testing req := httptest.NewRequest(http.MethodGet, "/api/v1/employees", nil) // -chi context for route chictx := chi.NewRouteContext() // add params to context chictx.URLParams.Add("id", "1") // create new context from parent context with url params req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, chictx)) // response res := httptest.NewRecorder() // ACT handlerFunc(res, req) // ASSERT //set the expected body expectedBody := `{ "data": [ { "id": 1, "card_number_id": 1001, "first_name": "John", "last_name": "Doe", "warehouse_id": 1 } ] }` // compare status code require.Equal(t, http.StatusOK, res.Code) // compare body require.JSONEq(t, expectedBody, res.Body.String()) }) // TEST CASES FOR INCRESING COVERAGE t.Run("Case read 04 -> find_all: Should return error message", func(t *testing.T) { // ARRANGE // -service sv := service.NewEmployeeDefaultMock() // -handler hd := handler.NewEmployeeDefault(sv) handlerFunc := hd.GetAll() // set the find all mock sv.On("FindAll").Return([]internal.Employee{}, internal.ErrEmployeesRepositoryNotFound) // request // use of http.NewRequest to create a new request for testing req := httptest.NewRequest(http.MethodGet, "/api/v1/employees", nil) //response res := httptest.NewRecorder() //ACT handlerFunc(res, req) // ASSERT // set the expected body expectedBody := `{ "status": "Not Found", "message": "Employees not found" }` // compare status code require.Equal(t, http.StatusNotFound, res.Code) // compare body require.JSONEq(t, expectedBody, res.Body.String()) }) }
Editor is loading...
Leave a Comment