delete_thing_image_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_DeleteThingImageHandler(t *testing.T) {
  22. type req struct {
  23. method string
  24. route string
  25. }
  26. var (
  27. mc = minimock.NewController(t)
  28. imageID = gofakeit.Number(1, 1000)
  29. imageURL = gofakeit.URL()
  30. testError = errors.New(gofakeit.Phrase())
  31. correctReq = req{
  32. method: fiber.MethodDelete,
  33. route: "/v1/images/thing/" + strconv.Itoa(imageID),
  34. }
  35. repoRes = &models.Image{
  36. Image: imageURL,
  37. }
  38. )
  39. tests := []struct {
  40. name string
  41. req req
  42. resCode int
  43. resBody interface{}
  44. thingImageRepoMock func(mc *minimock.Controller) interfaces.ThingImageRepository
  45. fileRepoMock func(mc *minimock.Controller) interfaces.FileRepository
  46. }{
  47. {
  48. name: "negative case - bad request",
  49. req: req{
  50. method: fiber.MethodDelete,
  51. route: "/v1/images/thing/" + gofakeit.Word(),
  52. },
  53. resCode: fiber.StatusBadRequest,
  54. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  55. return repoMocks.NewThingImageRepositoryMock(mc)
  56. },
  57. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  58. return repoMocks.NewFileRepositoryMock(mc)
  59. },
  60. },
  61. {
  62. name: "negative case - bad request (image not exists)",
  63. req: correctReq,
  64. resCode: fiber.StatusBadRequest,
  65. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  66. mock := repoMocks.NewThingImageRepositoryMock(mc)
  67. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  68. assert.Equal(mc, imageID, id)
  69. }).Return(nil, sql.ErrNoRows)
  70. return mock
  71. },
  72. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  73. return repoMocks.NewFileRepositoryMock(mc)
  74. },
  75. },
  76. {
  77. name: "negative case - repository error (get)",
  78. req: correctReq,
  79. resCode: fiber.StatusInternalServerError,
  80. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  81. mock := repoMocks.NewThingImageRepositoryMock(mc)
  82. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  83. assert.Equal(mc, imageID, id)
  84. }).Return(nil, testError)
  85. return mock
  86. },
  87. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  88. return repoMocks.NewFileRepositoryMock(mc)
  89. },
  90. },
  91. {
  92. name: "negative case - repository error (delete)",
  93. req: correctReq,
  94. resCode: fiber.StatusInternalServerError,
  95. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  96. mock := repoMocks.NewThingImageRepositoryMock(mc)
  97. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  98. assert.Equal(mc, imageID, id)
  99. }).Return(nil, nil)
  100. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  101. assert.Equal(mc, imageID, id)
  102. }).Return(testError)
  103. return mock
  104. },
  105. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  106. return repoMocks.NewFileRepositoryMock(mc)
  107. },
  108. },
  109. {
  110. name: "negative case - file delete error",
  111. req: correctReq,
  112. resCode: fiber.StatusInternalServerError,
  113. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  114. mock := repoMocks.NewThingImageRepositoryMock(mc)
  115. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  116. assert.Equal(mc, imageID, id)
  117. }).Return(repoRes, nil)
  118. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  119. assert.Equal(mc, imageID, id)
  120. }).Return(nil)
  121. return mock
  122. },
  123. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  124. mock := repoMocks.NewFileRepositoryMock(mc)
  125. mock.DeleteMock.Return(testError)
  126. return mock
  127. },
  128. },
  129. {
  130. name: "positive case",
  131. req: correctReq,
  132. resCode: fiber.StatusOK,
  133. resBody: dto.EmptyResponse{},
  134. thingImageRepoMock: func(mc *minimock.Controller) interfaces.ThingImageRepository {
  135. mock := repoMocks.NewThingImageRepositoryMock(mc)
  136. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  137. assert.Equal(mc, imageID, id)
  138. }).Return(repoRes, nil)
  139. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  140. assert.Equal(mc, imageID, id)
  141. }).Return(nil)
  142. return mock
  143. },
  144. fileRepoMock: func(mc *minimock.Controller) interfaces.FileRepository {
  145. mock := repoMocks.NewFileRepositoryMock(mc)
  146. mock.DeleteMock.Return(nil)
  147. return mock
  148. },
  149. },
  150. }
  151. for _, tt := range tests {
  152. t.Run(tt.name, func(t *testing.T) {
  153. fiberApp := fiber.New()
  154. serviceProvider := sp.InitMock(tt.thingImageRepoMock(mc), tt.fileRepoMock(mc))
  155. fiberApp.Delete("/v1/images/thing/:imageId", DeleteThingImageHandler(serviceProvider))
  156. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  157. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  158. if tt.resBody != nil {
  159. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  160. }
  161. })
  162. }
  163. }