update_place_test.go 5.1 KB

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