add_thing_notification_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package notification
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "testing"
  8. "time"
  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. "git.dmitriygnatenko.ru/dima/homethings/internal/repositories"
  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/lib/pq"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func Test_AddThingNotificationHandler(t *testing.T) {
  24. type req struct {
  25. method string
  26. route string
  27. contentType string
  28. body *dto.AddThingNotificationRequest
  29. }
  30. var (
  31. mc = minimock.NewController(t)
  32. thingID = gofakeit.Number(1, 1000)
  33. notificationDate = gofakeit.Date().Truncate(time.Second)
  34. testError = errors.New(gofakeit.Phrase())
  35. layout = "2006-01-02 15:04:05"
  36. correctReq = req{
  37. method: fiber.MethodPost,
  38. route: "/v1/things/notifications",
  39. body: &dto.AddThingNotificationRequest{
  40. ThingID: thingID,
  41. NotificationDate: notificationDate.Format(time.RFC3339),
  42. },
  43. contentType: fiber.MIMEApplicationJSON,
  44. }
  45. repoRes = models.ThingNotification{
  46. ThingID: thingID,
  47. NotificationDate: notificationDate,
  48. CreatedAt: gofakeit.Date(),
  49. UpdatedAt: gofakeit.Date(),
  50. }
  51. expectedRes = dto.ThingNotificationResponse{
  52. ThingID: thingID,
  53. NotificationDate: notificationDate.Format(layout),
  54. CreatedAt: repoRes.CreatedAt.Format(layout),
  55. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  56. }
  57. )
  58. tests := []struct {
  59. name string
  60. req req
  61. resCode int
  62. resBody interface{}
  63. repoMock func(mc *minimock.Controller) interfaces.ThingNotificationRepository
  64. }{
  65. {
  66. name: "positive case",
  67. req: correctReq,
  68. resCode: fiber.StatusOK,
  69. resBody: expectedRes,
  70. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  71. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  72. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) {
  73. assert.Equal(mc, thingID, req.ThingID)
  74. assert.Equal(mc, notificationDate, req.NotificationDate)
  75. }).Return(nil)
  76. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  77. assert.Equal(mc, thingID, id)
  78. }).Return(&repoRes, nil)
  79. return mock
  80. },
  81. },
  82. {
  83. name: "negative case - body parse error",
  84. req: req{
  85. method: fiber.MethodPost,
  86. route: "/v1/things/notifications",
  87. },
  88. resCode: fiber.StatusBadRequest,
  89. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  90. return repoMocks.NewThingNotificationRepositoryMock(mc)
  91. },
  92. },
  93. {
  94. name: "negative case - thing id is empty",
  95. req: req{
  96. method: fiber.MethodPost,
  97. route: "/v1/things/notifications",
  98. body: &dto.AddThingNotificationRequest{
  99. NotificationDate: notificationDate.Format(time.RFC3339),
  100. },
  101. contentType: fiber.MIMEApplicationJSON,
  102. },
  103. resCode: fiber.StatusBadRequest,
  104. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  105. return repoMocks.NewThingNotificationRepositoryMock(mc)
  106. },
  107. },
  108. {
  109. name: "negative case - incorrect notification date format",
  110. req: req{
  111. method: fiber.MethodPost,
  112. route: "/v1/things/notifications",
  113. body: &dto.AddThingNotificationRequest{
  114. ThingID: thingID,
  115. NotificationDate: notificationDate.String(),
  116. },
  117. contentType: fiber.MIMEApplicationJSON,
  118. },
  119. resCode: fiber.StatusBadRequest,
  120. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  121. return repoMocks.NewThingNotificationRepositoryMock(mc)
  122. },
  123. },
  124. {
  125. name: "negative case - repository error (add)",
  126. req: correctReq,
  127. resCode: fiber.StatusInternalServerError,
  128. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  129. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  130. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) {
  131. assert.Equal(mc, thingID, req.ThingID)
  132. assert.Equal(mc, notificationDate, req.NotificationDate)
  133. }).Return(testError)
  134. return mock
  135. },
  136. },
  137. {
  138. name: "negative case - repository error (duplicate)",
  139. req: correctReq,
  140. resCode: fiber.StatusBadRequest,
  141. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  142. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  143. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) {
  144. assert.Equal(mc, thingID, req.ThingID)
  145. assert.Equal(mc, notificationDate, req.NotificationDate)
  146. }).Return(&pq.Error{Code: repositories.DuplicateKeyErrorCode})
  147. return mock
  148. },
  149. },
  150. {
  151. name: "negative case - repository error (get)",
  152. req: correctReq,
  153. resCode: fiber.StatusInternalServerError,
  154. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  155. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  156. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) {
  157. assert.Equal(mc, thingID, req.ThingID)
  158. assert.Equal(mc, notificationDate, req.NotificationDate)
  159. }).Return(nil)
  160. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  161. assert.Equal(mc, thingID, id)
  162. }).Return(nil, testError)
  163. return mock
  164. },
  165. },
  166. }
  167. for _, tt := range tests {
  168. t.Run(tt.name, func(t *testing.T) {
  169. fiberApp := fiber.New()
  170. serviceProvider := sp.InitMock(tt.repoMock(mc))
  171. fiberApp.Post("/v1/things/notifications", AddThingNotificationHandler(serviceProvider))
  172. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  173. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  174. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  175. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  176. if tt.resBody != nil {
  177. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  178. }
  179. })
  180. }
  181. }