get_thing_notification_test.go 3.5 KB

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