get_thing_notification_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package notification
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "time"
  10. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  15. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  16. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  17. "github.com/brianvoe/gofakeit/v6"
  18. "github.com/gofiber/fiber/v2"
  19. "github.com/gojuno/minimock/v3"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func Test_GetThingNotificationHandler(t *testing.T) {
  23. type req struct {
  24. method string
  25. route string
  26. }
  27. var (
  28. mc = minimock.NewController(t)
  29. thingID = gofakeit.Number(1, 1000)
  30. testError = errors.New(gofakeit.Phrase())
  31. layout = "2006-01-02 15:04:05"
  32. correctReq = req{
  33. method: fiber.MethodGet,
  34. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  35. }
  36. repoRes = models.ThingNotification{
  37. ThingID: thingID,
  38. NotificationDate: gofakeit.Date().Truncate(time.Second),
  39. CreatedAt: gofakeit.Date(),
  40. UpdatedAt: gofakeit.Date(),
  41. }
  42. expectedRes = dto.ThingNotificationResponse{
  43. ThingID: thingID,
  44. NotificationDate: repoRes.NotificationDate.Format(layout),
  45. CreatedAt: repoRes.CreatedAt.Format(layout),
  46. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  47. }
  48. )
  49. tests := []struct {
  50. name string
  51. req req
  52. resCode int
  53. resBody interface{}
  54. repoMock func(mc *minimock.Controller) interfaces.ThingNotificationRepository
  55. }{
  56. {
  57. name: "positive case",
  58. req: correctReq,
  59. resCode: fiber.StatusOK,
  60. resBody: expectedRes,
  61. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  62. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  63. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  64. assert.Equal(mc, thingID, id)
  65. }).Return(&repoRes, nil)
  66. return mock
  67. },
  68. },
  69. {
  70. name: "negative case - repository error",
  71. req: correctReq,
  72. resCode: fiber.StatusInternalServerError,
  73. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  74. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  75. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  76. assert.Equal(mc, thingID, id)
  77. }).Return(nil, testError)
  78. return mock
  79. },
  80. },
  81. {
  82. name: "negative case - not found",
  83. req: correctReq,
  84. resCode: fiber.StatusNotFound,
  85. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  86. mock := repoMocks.NewThingNotificationRepositoryMock(mc)
  87. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  88. assert.Equal(mc, thingID, id)
  89. }).Return(nil, sql.ErrNoRows)
  90. return mock
  91. },
  92. },
  93. {
  94. name: "negative case - bad request",
  95. req: req{
  96. method: fiber.MethodGet,
  97. route: "/v1/things/notifications/" + gofakeit.Word(),
  98. },
  99. resCode: fiber.StatusBadRequest,
  100. repoMock: func(mc *minimock.Controller) interfaces.ThingNotificationRepository {
  101. return repoMocks.NewThingNotificationRepositoryMock(mc)
  102. },
  103. },
  104. }
  105. for _, tt := range tests {
  106. t.Run(tt.name, func(t *testing.T) {
  107. fiberApp := fiber.New()
  108. serviceProvider := sp.InitMock(tt.repoMock(mc))
  109. fiberApp.Get("/v1/things/notifications/:thingId", GetThingNotificationHandler(serviceProvider))
  110. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  111. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  112. if tt.resBody != nil {
  113. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  114. }
  115. })
  116. }
  117. }