get_place_images_test.go 5.4 KB

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