get_place_images_test.go 5.7 KB

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