get_place_images_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package image
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "time"
  10. "github.com/brianvoe/gofakeit/v6"
  11. "github.com/gofiber/fiber/v2"
  12. "github.com/gojuno/minimock/v3"
  13. "github.com/stretchr/testify/assert"
  14. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/image/mocks"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  18. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  19. )
  20. func TestGetPlaceImagesHandler(t *testing.T) {
  21. t.Parallel()
  22. type req struct {
  23. method string
  24. route string
  25. }
  26. var (
  27. placeID = gofakeit.Number(1, 1000)
  28. thingID = gofakeit.Number(1, 1000)
  29. date1 = gofakeit.Date()
  30. date2 = date1.Add(time.Hour)
  31. date3 = date2.Add(time.Hour)
  32. testError = errors.New(gofakeit.Phrase())
  33. layout = "2006-01-02 15:04:05"
  34. correctReq = req{
  35. method: fiber.MethodGet,
  36. route: "/v1/images/place/" + strconv.Itoa(placeID),
  37. }
  38. placeImageRepoRes = []models.Image{
  39. {
  40. ID: gofakeit.Number(1, 1000),
  41. Image: gofakeit.URL(),
  42. PlaceID: sql.NullInt64{Valid: true, Int64: int64(placeID)},
  43. CreatedAt: date1,
  44. },
  45. {
  46. ID: gofakeit.Number(1, 1000),
  47. Image: gofakeit.URL(),
  48. PlaceID: sql.NullInt64{Valid: true, Int64: int64(placeID)},
  49. CreatedAt: date2,
  50. },
  51. }
  52. thingImageRepoRes = []models.Image{
  53. {
  54. ID: gofakeit.Number(1, 1000),
  55. Image: gofakeit.URL(),
  56. ThingID: sql.NullInt64{Valid: true, Int64: int64(thingID)},
  57. CreatedAt: date3,
  58. },
  59. }
  60. expectedRes = dto.ImagesResponse{
  61. Images: []dto.ImageResponse{
  62. {
  63. ID: thingImageRepoRes[0].ID,
  64. Image: thingImageRepoRes[0].Image,
  65. CreatedAt: thingImageRepoRes[0].CreatedAt.Format(layout),
  66. ThingID: &thingID,
  67. },
  68. {
  69. ID: placeImageRepoRes[1].ID,
  70. Image: placeImageRepoRes[1].Image,
  71. CreatedAt: placeImageRepoRes[1].CreatedAt.Format(layout),
  72. PlaceID: &placeID,
  73. },
  74. {
  75. ID: placeImageRepoRes[0].ID,
  76. Image: placeImageRepoRes[0].Image,
  77. CreatedAt: placeImageRepoRes[0].CreatedAt.Format(layout),
  78. PlaceID: &placeID,
  79. },
  80. },
  81. }
  82. )
  83. tests := []struct {
  84. name string
  85. req req
  86. resCode int
  87. resBody interface{}
  88. thingImageRepoMock func(mc *minimock.Controller) ThingImageRepository
  89. placeImageRepoMock func(mc *minimock.Controller) PlaceImageRepository
  90. }{
  91. {
  92. name: "negative case - bad request",
  93. req: req{
  94. method: fiber.MethodGet,
  95. route: "/v1/images/place/" + gofakeit.Word(),
  96. },
  97. resCode: fiber.StatusBadRequest,
  98. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  99. return mocks.NewPlaceImageRepositoryMock(mc)
  100. },
  101. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  102. return mocks.NewThingImageRepositoryMock(mc)
  103. },
  104. },
  105. {
  106. name: "negative case - place repository error",
  107. req: correctReq,
  108. resCode: fiber.StatusInternalServerError,
  109. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  110. mock := mocks.NewPlaceImageRepositoryMock(mc)
  111. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  112. assert.Equal(mc, placeID, id)
  113. }).Return(nil, testError)
  114. return mock
  115. },
  116. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  117. return mocks.NewThingImageRepositoryMock(mc)
  118. },
  119. },
  120. {
  121. name: "negative case - thing repository error",
  122. req: correctReq,
  123. resCode: fiber.StatusInternalServerError,
  124. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  125. mock := mocks.NewPlaceImageRepositoryMock(mc)
  126. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  127. assert.Equal(mc, placeID, id)
  128. }).Return(nil, nil)
  129. return mock
  130. },
  131. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  132. mock := mocks.NewThingImageRepositoryMock(mc)
  133. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  134. assert.Equal(mc, placeID, id)
  135. }).Return(nil, testError)
  136. return mock
  137. },
  138. },
  139. {
  140. name: "positive case",
  141. req: correctReq,
  142. resCode: fiber.StatusOK,
  143. resBody: expectedRes,
  144. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  145. mock := mocks.NewPlaceImageRepositoryMock(mc)
  146. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  147. assert.Equal(mc, placeID, id)
  148. }).Return(placeImageRepoRes, nil)
  149. return mock
  150. },
  151. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  152. mock := mocks.NewThingImageRepositoryMock(mc)
  153. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  154. assert.Equal(mc, placeID, id)
  155. }).Return(thingImageRepoRes, nil)
  156. return mock
  157. },
  158. },
  159. }
  160. for _, tt := range tests {
  161. t.Run(tt.name, func(t *testing.T) {
  162. t.Parallel()
  163. mc := minimock.NewController(t)
  164. fiberApp := fiber.New()
  165. fiberApp.Get("/v1/images/place/:placeId", GetPlaceImagesHandler(
  166. tt.thingImageRepoMock(mc),
  167. tt.placeImageRepoMock(mc),
  168. ))
  169. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  170. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  171. if tt.resBody != nil {
  172. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  173. }
  174. })
  175. }
  176. }