update_place_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package place
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "github.com/brianvoe/gofakeit/v6"
  10. "github.com/gofiber/fiber/v2"
  11. "github.com/gojuno/minimock/v3"
  12. "github.com/stretchr/testify/assert"
  13. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/place/mocks"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. func TestUpdatePlaceHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. contentType string
  25. body *dto.UpdatePlaceRequest
  26. }
  27. var (
  28. placeID = gofakeit.Number(1, 1000)
  29. parentID = gofakeit.Number(1, 1000)
  30. title = gofakeit.Phrase()
  31. testError = errors.New(gofakeit.Phrase())
  32. layout = "2006-01-02 15:04:05"
  33. correctReq = req{
  34. method: fiber.MethodPut,
  35. route: "/v1/places/" + strconv.Itoa(placeID),
  36. body: &dto.UpdatePlaceRequest{
  37. Title: title,
  38. ParentID: &parentID,
  39. },
  40. contentType: fiber.MIMEApplicationJSON,
  41. }
  42. repoRes = models.Place{
  43. ID: placeID,
  44. Title: title,
  45. ParentID: sql.NullInt64{Int64: int64(parentID), Valid: true},
  46. CreatedAt: gofakeit.Date(),
  47. UpdatedAt: gofakeit.Date(),
  48. }
  49. expectedRes = dto.PlaceResponse{
  50. ID: placeID,
  51. ParentID: &parentID,
  52. Title: repoRes.Title,
  53. CreatedAt: repoRes.CreatedAt.Format(layout),
  54. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  55. }
  56. )
  57. tests := []struct {
  58. name string
  59. req req
  60. resCode int
  61. resBody interface{}
  62. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  63. }{
  64. {
  65. name: "positive case",
  66. req: correctReq,
  67. resCode: fiber.StatusOK,
  68. resBody: expectedRes,
  69. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  70. mock := mocks.NewPlaceRepositoryMock(mc)
  71. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdatePlaceRequest, tx *sql.Tx) {
  72. assert.Equal(mc, title, req.Title)
  73. assert.Equal(mc, parentID, int(req.ParentID.Int64))
  74. }).Return(nil)
  75. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  76. assert.Equal(mc, placeID, id)
  77. }).Return(&repoRes, nil)
  78. return mock
  79. },
  80. },
  81. {
  82. name: "negative case - bad request",
  83. req: req{
  84. method: fiber.MethodPut,
  85. route: "/v1/places/" + gofakeit.Word(),
  86. },
  87. resCode: fiber.StatusBadRequest,
  88. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  89. return mocks.NewPlaceRepositoryMock(mc)
  90. },
  91. },
  92. {
  93. name: "negative case - body parse error",
  94. req: req{
  95. method: fiber.MethodPut,
  96. route: "/v1/places/" + strconv.Itoa(placeID),
  97. },
  98. resCode: fiber.StatusBadRequest,
  99. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  100. return mocks.NewPlaceRepositoryMock(mc)
  101. },
  102. },
  103. {
  104. name: "negative case - request without title",
  105. req: req{
  106. method: fiber.MethodPut,
  107. route: "/v1/places/" + strconv.Itoa(placeID),
  108. contentType: fiber.MIMEApplicationJSON,
  109. body: &dto.UpdatePlaceRequest{},
  110. },
  111. resCode: fiber.StatusBadRequest,
  112. resBody: []*dto.ValidateErrorResponse{
  113. {
  114. Field: "UpdatePlaceRequest.Title",
  115. Tag: "required",
  116. },
  117. },
  118. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  119. return mocks.NewPlaceRepositoryMock(mc)
  120. },
  121. },
  122. {
  123. name: "negative case - repository error (update place)",
  124. req: correctReq,
  125. resCode: fiber.StatusInternalServerError,
  126. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  127. mock := mocks.NewPlaceRepositoryMock(mc)
  128. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdatePlaceRequest, tx *sql.Tx) {
  129. assert.Equal(mc, title, req.Title)
  130. assert.Equal(mc, parentID, int(req.ParentID.Int64))
  131. }).Return(testError)
  132. return mock
  133. },
  134. },
  135. {
  136. name: "negative case - repository error (get place)",
  137. req: correctReq,
  138. resCode: fiber.StatusInternalServerError,
  139. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  140. mock := mocks.NewPlaceRepositoryMock(mc)
  141. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdatePlaceRequest, tx *sql.Tx) {
  142. assert.Equal(mc, title, req.Title)
  143. assert.Equal(mc, parentID, int(req.ParentID.Int64))
  144. }).Return(nil)
  145. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  146. assert.Equal(mc, placeID, id)
  147. }).Return(nil, testError)
  148. return mock
  149. },
  150. },
  151. }
  152. for _, tt := range tests {
  153. t.Run(tt.name, func(t *testing.T) {
  154. t.Parallel()
  155. mc := minimock.NewController(t)
  156. fiberApp := fiber.New()
  157. fiberApp.Put("/v1/places/:placeId", UpdatePlaceHandler(tt.placeRepoMock(mc)))
  158. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  159. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  160. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  161. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  162. if tt.resBody != nil {
  163. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  164. }
  165. })
  166. }
  167. }