update_thing_notification_test.go 7.1 KB

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