delete_thing_image_test.go 5.2 KB

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