get_thing_images_test.go 4.1 KB

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