get_expired_thing_notifications_test.go 3.2 KB

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