get_expired_thing_notifications_test.go 3.0 KB

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