get_thing_notification_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 TestGetThingNotificationHandler(t *testing.T) {
  21. t.Parallel()
  22. type req struct {
  23. method string
  24. route string
  25. }
  26. var (
  27. thingID = gofakeit.Number(1, 1000)
  28. testError = errors.New(gofakeit.Phrase())
  29. layout = "2006-01-02 15:04:05"
  30. correctReq = req{
  31. method: fiber.MethodGet,
  32. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  33. }
  34. repoRes = models.ThingNotification{
  35. ThingID: thingID,
  36. NotificationDate: gofakeit.Date().Truncate(time.Second),
  37. CreatedAt: gofakeit.Date(),
  38. UpdatedAt: gofakeit.Date(),
  39. }
  40. expectedRes = dto.ThingNotificationResponse{
  41. ThingID: thingID,
  42. NotificationDate: repoRes.NotificationDate.Format(layout),
  43. CreatedAt: repoRes.CreatedAt.Format(layout),
  44. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  45. }
  46. )
  47. tests := []struct {
  48. name string
  49. req req
  50. resCode int
  51. resBody interface{}
  52. repoMock func(mc *minimock.Controller) ThingNotificationRepository
  53. }{
  54. {
  55. name: "positive case",
  56. req: correctReq,
  57. resCode: fiber.StatusOK,
  58. resBody: expectedRes,
  59. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  60. mock := mocks.NewThingNotificationRepositoryMock(mc)
  61. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  62. assert.Equal(mc, thingID, id)
  63. }).Return(&repoRes, nil)
  64. return mock
  65. },
  66. },
  67. {
  68. name: "negative case - repository error",
  69. req: correctReq,
  70. resCode: fiber.StatusInternalServerError,
  71. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  72. mock := mocks.NewThingNotificationRepositoryMock(mc)
  73. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  74. assert.Equal(mc, thingID, id)
  75. }).Return(nil, testError)
  76. return mock
  77. },
  78. },
  79. {
  80. name: "negative case - not found",
  81. req: correctReq,
  82. resCode: fiber.StatusNotFound,
  83. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  84. mock := mocks.NewThingNotificationRepositoryMock(mc)
  85. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  86. assert.Equal(mc, thingID, id)
  87. }).Return(nil, sql.ErrNoRows)
  88. return mock
  89. },
  90. },
  91. {
  92. name: "negative case - bad request",
  93. req: req{
  94. method: fiber.MethodGet,
  95. route: "/v1/things/notifications/" + gofakeit.Word(),
  96. },
  97. resCode: fiber.StatusBadRequest,
  98. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  99. return mocks.NewThingNotificationRepositoryMock(mc)
  100. },
  101. },
  102. }
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. t.Parallel()
  106. mc := minimock.NewController(t)
  107. fiberApp := fiber.New()
  108. fiberApp.Get("/v1/things/notifications/:thingId", GetThingNotificationHandler(tt.repoMock(mc)))
  109. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  110. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  111. if tt.resBody != nil {
  112. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  113. }
  114. })
  115. }
  116. }