get_thing_images_test.go 3.8 KB

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