get_thing_images_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package image
  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/image/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 TestGetThingImagesHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. thingID = gofakeit.Number(1, 1000)
  27. testError = errors.New(gofakeit.Phrase())
  28. layout = "2006-01-02 15:04:05"
  29. correctReq = req{
  30. method: fiber.MethodGet,
  31. route: "/v1/images/thing/" + strconv.Itoa(thingID),
  32. }
  33. imageRepoRes = []models.Image{
  34. {
  35. ID: gofakeit.Number(1, 1000),
  36. Image: gofakeit.URL(),
  37. CreatedAt: gofakeit.Date(),
  38. ThingID: sql.NullInt64{Valid: true, Int64: int64(thingID)},
  39. },
  40. {
  41. ID: gofakeit.Number(1, 1000),
  42. Image: gofakeit.URL(),
  43. CreatedAt: gofakeit.Date(),
  44. ThingID: sql.NullInt64{Valid: true, Int64: int64(thingID)},
  45. },
  46. {
  47. ID: gofakeit.Number(1, 1000),
  48. Image: gofakeit.URL(),
  49. CreatedAt: gofakeit.Date(),
  50. ThingID: sql.NullInt64{Valid: true, Int64: int64(thingID)},
  51. },
  52. }
  53. expectedRes = dto.ImagesResponse{
  54. Images: []dto.ImageResponse{
  55. {
  56. ID: imageRepoRes[0].ID,
  57. Image: imageRepoRes[0].Image,
  58. CreatedAt: imageRepoRes[0].CreatedAt.Format(layout),
  59. ThingID: &thingID,
  60. },
  61. {
  62. ID: imageRepoRes[1].ID,
  63. Image: imageRepoRes[1].Image,
  64. CreatedAt: imageRepoRes[1].CreatedAt.Format(layout),
  65. ThingID: &thingID,
  66. },
  67. {
  68. ID: imageRepoRes[2].ID,
  69. Image: imageRepoRes[2].Image,
  70. CreatedAt: imageRepoRes[2].CreatedAt.Format(layout),
  71. ThingID: &thingID,
  72. },
  73. },
  74. }
  75. )
  76. tests := []struct {
  77. name string
  78. req req
  79. resCode int
  80. resBody interface{}
  81. thingImageRepoMock func(mc *minimock.Controller) ThingImageRepository
  82. }{
  83. {
  84. name: "negative case - bad request",
  85. req: req{
  86. method: fiber.MethodGet,
  87. route: "/v1/images/thing/" + gofakeit.Word(),
  88. },
  89. resCode: fiber.StatusBadRequest,
  90. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  91. return mocks.NewThingImageRepositoryMock(mc)
  92. },
  93. },
  94. {
  95. name: "negative case - repository error",
  96. req: correctReq,
  97. resCode: fiber.StatusInternalServerError,
  98. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  99. mock := mocks.NewThingImageRepositoryMock(mc)
  100. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  101. assert.Equal(mc, thingID, id)
  102. }).Return(nil, testError)
  103. return mock
  104. },
  105. },
  106. {
  107. name: "positive case",
  108. req: correctReq,
  109. resCode: fiber.StatusOK,
  110. resBody: expectedRes,
  111. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  112. mock := mocks.NewThingImageRepositoryMock(mc)
  113. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  114. assert.Equal(mc, thingID, id)
  115. }).Return(imageRepoRes, nil)
  116. return mock
  117. },
  118. },
  119. }
  120. for _, tt := range tests {
  121. t.Run(tt.name, func(t *testing.T) {
  122. t.Parallel()
  123. mc := minimock.NewController(t)
  124. fiberApp := fiber.New()
  125. fiberApp.Get("/v1/images/thing/:thingId", GetThingImagesHandler(tt.thingImageRepoMock(mc)))
  126. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  127. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  128. if tt.resBody != nil {
  129. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  130. }
  131. })
  132. }
  133. }