update_thing_notification_test.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package notification
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "time"
  10. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  15. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  16. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  17. "github.com/brianvoe/gofakeit/v6"
  18. "github.com/gofiber/fiber/v2"
  19. "github.com/gojuno/minimock/v3"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func Test_UpdateThingNotificationHandler(t *testing.T) {
  23. type req struct {
  24. method string
  25. route string
  26. contentType string
  27. body *dto.UpdateThingNotificationRequest
  28. }
  29. var (
  30. mc = minimock.NewController(t)
  31. thingID = gofakeit.Number(1, 1000)
  32. notificationDate = gofakeit.Date().Truncate(time.Second)
  33. testError = errors.New(gofakeit.Phrase())
  34. layout = "2006-01-02 15:04:05"
  35. correctReq = req{
  36. method: fiber.MethodPut,
  37. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  38. body: &dto.UpdateThingNotificationRequest{
  39. NotificationDate: notificationDate.Format(time.RFC3339),
  40. },
  41. contentType: fiber.MIMEApplicationJSON,
  42. }
  43. repoRes = models.ThingNotification{
  44. ThingID: thingID,
  45. NotificationDate: notificationDate,
  46. CreatedAt: gofakeit.Date(),
  47. UpdatedAt: gofakeit.Date(),
  48. }
  49. expectedRes = dto.ThingNotificationResponse{
  50. ThingID: thingID,
  51. NotificationDate: notificationDate.Format(layout),
  52. CreatedAt: repoRes.CreatedAt.Format(layout),
  53. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  54. }
  55. )
  56. tests := []struct {
  57. name string
  58. req req
  59. resCode int
  60. resBody interface{}
  61. repoMock func(mc *minimock.Controller) interfaces.ThingNotificationRepository
  62. }{
  63. {
  64. name: "positive case",
  65. req: correctReq,
  66. resCode: fiber.StatusOK,
  67. resBody: expectedRes,
  68. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  69. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  70. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) {
  71. assert.Equal(mc, thingID, req.ThingID)
  72. assert.Equal(mc, notificationDate, req.NotificationDate)
  73. }).Return(nil)
  74. mock.GetMock.Set(func(ctx context.Context, id int) (*models.ThingNotification, error) {
  75. assert.Equal(mc, thingID, id)
  76. if mock.GetAfterCounter() == 0 {
  77. return nil, nil
  78. }
  79. return &repoRes, nil
  80. })
  81. return mock
  82. },
  83. },
  84. {
  85. name: "negative case - bad request",
  86. req: req{
  87. method: fiber.MethodPut,
  88. route: "/v1/things/notifications/" + gofakeit.Word(),
  89. },
  90. resCode: fiber.StatusBadRequest,
  91. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  92. return repoMocks.NewThingNotificationRepositoryMock(mc)
  93. },
  94. },
  95. {
  96. name: "negative case - body parse error",
  97. req: req{
  98. method: fiber.MethodPut,
  99. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  100. },
  101. resCode: fiber.StatusBadRequest,
  102. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  103. return repoMocks.NewThingNotificationRepositoryMock(mc)
  104. },
  105. },
  106. {
  107. name: "negative case - validate request error",
  108. req: req{
  109. method: fiber.MethodPut,
  110. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  111. contentType: fiber.MIMEApplicationJSON,
  112. },
  113. resCode: fiber.StatusBadRequest,
  114. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  115. return repoMocks.NewThingNotificationRepositoryMock(mc)
  116. },
  117. },
  118. {
  119. name: "negative case - repository error (get)",
  120. req: correctReq,
  121. resCode: fiber.StatusInternalServerError,
  122. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  123. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  124. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  125. assert.Equal(mc, thingID, id)
  126. }).Return(nil, testError)
  127. return mock
  128. },
  129. },
  130. {
  131. name: "negative case - bad request (notification not found)",
  132. req: correctReq,
  133. resCode: fiber.StatusBadRequest,
  134. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  135. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  136. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  137. assert.Equal(mc, thingID, id)
  138. }).Return(nil, sql.ErrNoRows)
  139. return mock
  140. },
  141. },
  142. {
  143. name: "negative case - bad request (notification not found)",
  144. req: req{
  145. method: fiber.MethodPut,
  146. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  147. body: &dto.UpdateThingNotificationRequest{
  148. NotificationDate: notificationDate.String(),
  149. },
  150. contentType: fiber.MIMEApplicationJSON,
  151. },
  152. resCode: fiber.StatusBadRequest,
  153. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  154. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  155. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  156. assert.Equal(mc, thingID, id)
  157. }).Return(nil, nil)
  158. return mock
  159. },
  160. },
  161. {
  162. name: "negative case - repository error (update)",
  163. req: correctReq,
  164. resCode: fiber.StatusInternalServerError,
  165. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  166. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  167. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  168. assert.Equal(mc, thingID, id)
  169. }).Return(nil, nil)
  170. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) {
  171. assert.Equal(mc, thingID, req.ThingID)
  172. assert.Equal(mc, notificationDate, req.NotificationDate)
  173. }).Return(testError)
  174. return mock
  175. },
  176. },
  177. {
  178. name: "negative case - repository error (get)",
  179. req: correctReq,
  180. resCode: fiber.StatusInternalServerError,
  181. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  182. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  183. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) {
  184. assert.Equal(mc, thingID, req.ThingID)
  185. assert.Equal(mc, notificationDate, req.NotificationDate)
  186. }).Return(nil)
  187. mock.GetMock.Set(func(ctx context.Context, id int) (*models.ThingNotification, error) {
  188. assert.Equal(mc, thingID, id)
  189. if mock.GetAfterCounter() == 0 {
  190. return nil, nil
  191. }
  192. return nil, testError
  193. })
  194. return mock
  195. },
  196. },
  197. }
  198. for _, tt := range tests {
  199. t.Run(tt.name, func(t *testing.T) {
  200. fiberApp := fiber.New()
  201. serviceProvider := sp.InitMock(tt.repoMock(mc))
  202. fiberApp.Put("/v1/things/notifications/:thingId", UpdateThingNotificationHandler(serviceProvider))
  203. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  204. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  205. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  206. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  207. if tt.resBody != nil {
  208. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  209. }
  210. })
  211. }
  212. }