add_thing_notification_test.go 6.1 KB

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