get_expired_thing_notifications_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package notification
  2. import (
  3. "errors"
  4. "net/http/httptest"
  5. "testing"
  6. "time"
  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. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/notification/mocks"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  16. )
  17. func TestGetExpiredThingNotificationsHandler(t *testing.T) {
  18. t.Parallel()
  19. type req struct {
  20. method string
  21. route string
  22. }
  23. var (
  24. testError = errors.New(gofakeit.Phrase())
  25. layout = "2006-01-02 15:04:05"
  26. correctReq = req{
  27. method: fiber.MethodGet,
  28. route: "/v1/things/notifications/expired",
  29. }
  30. repoRes = []models.ExtThingNotification{
  31. {
  32. ThingID: gofakeit.Number(1, 1000),
  33. PlaceID: gofakeit.Number(1, 1000),
  34. ThingTitle: gofakeit.Phrase(),
  35. PlaceTitle: gofakeit.Phrase(),
  36. NotificationDate: gofakeit.Date().Truncate(time.Second),
  37. CreatedAt: gofakeit.Date(),
  38. UpdatedAt: gofakeit.Date(),
  39. },
  40. }
  41. expectedRes = dto.ThingNotificationsExtResponse{
  42. Notifications: []dto.ThingNotificationExtResponse{
  43. {
  44. ThingID: repoRes[0].ThingID,
  45. PlaceID: repoRes[0].PlaceID,
  46. ThingTitle: repoRes[0].ThingTitle,
  47. PlaceTitle: repoRes[0].PlaceTitle,
  48. NotificationDate: repoRes[0].NotificationDate.Format(layout),
  49. CreatedAt: repoRes[0].CreatedAt.Format(layout),
  50. UpdatedAt: repoRes[0].UpdatedAt.Format(layout),
  51. },
  52. },
  53. }
  54. )
  55. tests := []struct {
  56. name string
  57. req req
  58. resCode int
  59. resBody interface{}
  60. repoMock func(mc *minimock.Controller) ThingNotificationRepository
  61. }{
  62. {
  63. name: "positive case",
  64. req: correctReq,
  65. resCode: fiber.StatusOK,
  66. resBody: expectedRes,
  67. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  68. mock := mocks.NewThingNotificationRepositoryMock(mc)
  69. mock.GetExpiredMock.Return(repoRes, nil)
  70. return mock
  71. },
  72. },
  73. {
  74. name: "negative case - repository error",
  75. req: correctReq,
  76. resCode: fiber.StatusInternalServerError,
  77. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  78. mock := mocks.NewThingNotificationRepositoryMock(mc)
  79. mock.GetExpiredMock.Return(nil, testError)
  80. return mock
  81. },
  82. },
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. t.Parallel()
  87. mc := minimock.NewController(t)
  88. fiberApp := fiber.New()
  89. fiberApp.Get("/v1/things/notifications/expired", GetExpiredThingNotificationsHandler(tt.repoMock(mc)))
  90. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  91. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  92. if tt.resBody != nil {
  93. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  94. }
  95. })
  96. }
  97. }