|
| 1 | +package courses |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + mooc "github.com/CodelyTV/go-hexagonal_http_api-course/02-04-domain-validations/internal" |
| 7 | + "github.com/CodelyTV/go-hexagonal_http_api-course/02-04-domain-validations/internal/platform/storage/storagemocks" |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + "log" |
| 13 | + "net/http" |
| 14 | + "net/http/httptest" |
| 15 | + "testing" |
| 16 | +) |
| 17 | + |
| 18 | +func TestGetHandler(t *testing.T) { |
| 19 | + tests := map[string]struct { |
| 20 | + mockData []mooc.Course |
| 21 | + mockError error |
| 22 | + expectedResponse []getResponse |
| 23 | + expectedStatus int |
| 24 | + }{ |
| 25 | + "Empty repo return 200 with empty list": { |
| 26 | + mockData: []mooc.Course{}, |
| 27 | + mockError: nil, |
| 28 | + expectedStatus:http.StatusOK, |
| 29 | + expectedResponse: []getResponse{}}, |
| 30 | + "Repo error return 500": { |
| 31 | + mockData: []mooc.Course{}, |
| 32 | + mockError: errors.New("the field Duration can not be empty"), |
| 33 | + expectedStatus:http.StatusInternalServerError, |
| 34 | + expectedResponse: []getResponse{}}, |
| 35 | + "Fully repo return 200 with list of courses":{ |
| 36 | + mockData: []mooc.Course{ mockCourse("8a1c5cdc-ba57-445a-994d-aa412d23723f", "Courses Complete", "123")}, |
| 37 | + mockError: nil, |
| 38 | + expectedStatus:http.StatusOK, |
| 39 | + expectedResponse: []getResponse{{Id: "8a1c5cdc-ba57-445a-994d-aa412d23723f", Name: "Courses Complete", Duration: "123"}}}, |
| 40 | + } |
| 41 | + for key, value := range tests { |
| 42 | + t.Run(key, func(t *testing.T) { |
| 43 | + |
| 44 | + courseRepository := new(storagemocks.CourseRepository) |
| 45 | + courseRepository.On("GetAll", mock.Anything).Return(value.mockData, value.mockError) |
| 46 | + gin.SetMode(gin.TestMode) |
| 47 | + r := gin.New() |
| 48 | + r.GET("/courses", GetHandler(courseRepository)) |
| 49 | + |
| 50 | + req, err := http.NewRequest(http.MethodGet, "/courses", nil) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + rec := httptest.NewRecorder() |
| 54 | + r.ServeHTTP(rec, req) |
| 55 | + |
| 56 | + res := rec.Result() |
| 57 | + defer res.Body.Close() |
| 58 | + |
| 59 | + assert.Equal(t, value.expectedStatus, res.StatusCode) |
| 60 | + var response []getResponse |
| 61 | + if err := json.NewDecoder(res.Body).Decode(&response); err != nil { |
| 62 | + log.Fatalln(err) |
| 63 | + } |
| 64 | + |
| 65 | + assert.Equal(t, value.expectedResponse, response) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func mockCourse(id string, name string, duration string) mooc.Course { |
| 71 | + course, err := mooc.NewCourse(id, name, duration) |
| 72 | + if err != nil{ |
| 73 | + log.Fatalln(err) |
| 74 | + } |
| 75 | + return course |
| 76 | +} |
0 commit comments