delete_thing_image_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package image
  2. import (
  3. "context"
  4. "net/http/httptest"
  5. "strconv"
  6. "testing"
  7. "github.com/brianvoe/gofakeit/v6"
  8. "github.com/gofiber/fiber/v2"
  9. "github.com/gojuno/minimock/v3"
  10. "github.com/stretchr/testify/assert"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/image/mocks"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers/test"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  15. )
  16. func TestDeleteThingImageHandler(t *testing.T) {
  17. t.Parallel()
  18. type req struct {
  19. method string
  20. route string
  21. }
  22. var (
  23. imageID = uint64(gofakeit.Number(1, 1000))
  24. imageURL = gofakeit.URL()
  25. testError = gofakeit.Error()
  26. txMockFunc = func(ctx context.Context, f func(ctx context.Context) error) error {
  27. return f(ctx)
  28. }
  29. correctReq = req{
  30. method: fiber.MethodDelete,
  31. route: "/v1/images/thing/" + strconv.FormatUint(imageID, 10),
  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. tmMock func(mc *minimock.Controller) TransactionManager
  43. thingImageRepoMock func(mc *minimock.Controller) ThingImageRepository
  44. fileRepoMock func(mc *minimock.Controller) FileRepository
  45. }{
  46. {
  47. name: "negative case - bad request",
  48. req: req{
  49. method: fiber.MethodDelete,
  50. route: "/v1/images/thing/" + gofakeit.Word(),
  51. },
  52. resCode: fiber.StatusBadRequest,
  53. tmMock: func(mc *minimock.Controller) TransactionManager {
  54. return mocks.NewTransactionManagerMock(mc)
  55. },
  56. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  57. return mocks.NewThingImageRepositoryMock(mc)
  58. },
  59. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  60. return mocks.NewFileRepositoryMock(mc)
  61. },
  62. },
  63. {
  64. name: "negative case - repository error (get)",
  65. req: correctReq,
  66. resCode: fiber.StatusInternalServerError,
  67. tmMock: func(mc *minimock.Controller) TransactionManager {
  68. mock := mocks.NewTransactionManagerMock(mc)
  69. mock.ReadCommittedMock.Set(txMockFunc)
  70. return mock
  71. },
  72. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  73. mock := mocks.NewThingImageRepositoryMock(mc)
  74. mock.GetMock.Inspect(func(ctx context.Context, id uint64) {
  75. assert.Equal(mc, imageID, id)
  76. }).Return(nil, testError)
  77. return mock
  78. },
  79. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  80. return mocks.NewFileRepositoryMock(mc)
  81. },
  82. },
  83. {
  84. name: "negative case - repository error (delete)",
  85. req: correctReq,
  86. resCode: fiber.StatusInternalServerError,
  87. tmMock: func(mc *minimock.Controller) TransactionManager {
  88. mock := mocks.NewTransactionManagerMock(mc)
  89. mock.ReadCommittedMock.Set(txMockFunc)
  90. return mock
  91. },
  92. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  93. mock := mocks.NewThingImageRepositoryMock(mc)
  94. mock.GetMock.Inspect(func(ctx context.Context, id uint64) {
  95. assert.Equal(mc, imageID, id)
  96. }).Return(nil, nil)
  97. mock.DeleteMock.Inspect(func(ctx context.Context, id uint64) {
  98. assert.Equal(mc, imageID, id)
  99. }).Return(testError)
  100. return mock
  101. },
  102. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  103. return mocks.NewFileRepositoryMock(mc)
  104. },
  105. },
  106. {
  107. name: "negative case - file delete error",
  108. req: correctReq,
  109. resCode: fiber.StatusInternalServerError,
  110. tmMock: func(mc *minimock.Controller) TransactionManager {
  111. mock := mocks.NewTransactionManagerMock(mc)
  112. mock.ReadCommittedMock.Set(txMockFunc)
  113. return mock
  114. },
  115. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  116. mock := mocks.NewThingImageRepositoryMock(mc)
  117. mock.GetMock.Inspect(func(ctx context.Context, id uint64) {
  118. assert.Equal(mc, imageID, id)
  119. }).Return(repoRes, nil)
  120. mock.DeleteMock.Inspect(func(ctx context.Context, id uint64) {
  121. assert.Equal(mc, imageID, id)
  122. }).Return(nil)
  123. return mock
  124. },
  125. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  126. mock := mocks.NewFileRepositoryMock(mc)
  127. mock.DeleteMock.Return(testError)
  128. return mock
  129. },
  130. },
  131. {
  132. name: "positive case",
  133. req: correctReq,
  134. resCode: fiber.StatusOK,
  135. resBody: dto.EmptyResponse{},
  136. tmMock: func(mc *minimock.Controller) TransactionManager {
  137. mock := mocks.NewTransactionManagerMock(mc)
  138. mock.ReadCommittedMock.Set(txMockFunc)
  139. return mock
  140. },
  141. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  142. mock := mocks.NewThingImageRepositoryMock(mc)
  143. mock.GetMock.Inspect(func(ctx context.Context, id uint64) {
  144. assert.Equal(mc, imageID, id)
  145. }).Return(repoRes, nil)
  146. mock.DeleteMock.Inspect(func(ctx context.Context, id uint64) {
  147. assert.Equal(mc, imageID, id)
  148. }).Return(nil)
  149. return mock
  150. },
  151. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  152. mock := mocks.NewFileRepositoryMock(mc)
  153. mock.DeleteMock.Return(nil)
  154. return mock
  155. },
  156. },
  157. }
  158. for _, tt := range tests {
  159. t.Run(tt.name, func(t *testing.T) {
  160. t.Parallel()
  161. mc := minimock.NewController(t)
  162. fiberApp := fiber.New()
  163. fiberApp.Delete("/v1/images/thing/:imageId", DeleteThingImageHandler(
  164. tt.tmMock(mc),
  165. tt.fileRepoMock(mc),
  166. tt.thingImageRepoMock(mc),
  167. ))
  168. fiberRes, _ := fiberApp.Test(
  169. httptest.NewRequest(tt.req.method, tt.req.route, nil),
  170. test.TestTimeout,
  171. )
  172. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  173. if tt.resBody != nil {
  174. assert.Equal(t, test.MarshalResponse(tt.resBody), test.ConvertBodyToString(fiberRes.Body))
  175. }
  176. })
  177. }
  178. }