delete_thing_notification_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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/helpers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. func Test_DeleteThingNotificationHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. contentType string
  25. }
  26. var (
  27. thingID = gofakeit.Number(1, 1000)
  28. testError = errors.New(gofakeit.Phrase())
  29. correctReq = req{
  30. method: fiber.MethodDelete,
  31. route: "/v1/things/notifications/" + strconv.Itoa(thingID),
  32. contentType: fiber.MIMEApplicationJSON,
  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. )
  41. tests := []struct {
  42. name string
  43. req req
  44. resCode int
  45. resBody interface{}
  46. repoMock func(mc *minimock.Controller) ThingNotificationRepository
  47. }{
  48. {
  49. name: "positive case",
  50. req: correctReq,
  51. resCode: fiber.StatusOK,
  52. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  53. mock := mocks.NewThingNotificationRepositoryMock(mc)
  54. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  55. assert.Equal(mc, thingID, id)
  56. }).Return(&repoRes, nil)
  57. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  58. assert.Equal(mc, thingID, id)
  59. }).Return(nil)
  60. return mock
  61. },
  62. },
  63. {
  64. name: "negative case - bad request",
  65. req: req{
  66. method: fiber.MethodDelete,
  67. route: "/v1/things/notifications/" + gofakeit.Word(),
  68. contentType: fiber.MIMEApplicationJSON,
  69. },
  70. resCode: fiber.StatusBadRequest,
  71. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  72. return mocks.NewThingNotificationRepositoryMock(mc)
  73. },
  74. },
  75. {
  76. name: "negative case - repository error (get)",
  77. req: correctReq,
  78. resCode: fiber.StatusInternalServerError,
  79. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  80. mock := mocks.NewThingNotificationRepositoryMock(mc)
  81. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  82. assert.Equal(mc, thingID, id)
  83. }).Return(nil, testError)
  84. return mock
  85. },
  86. },
  87. {
  88. name: "negative case - bad request (notification not found)",
  89. req: correctReq,
  90. resCode: fiber.StatusBadRequest,
  91. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  92. mock := mocks.NewThingNotificationRepositoryMock(mc)
  93. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  94. assert.Equal(mc, thingID, id)
  95. }).Return(nil, sql.ErrNoRows)
  96. return mock
  97. },
  98. },
  99. {
  100. name: "negative case - repository error (delete)",
  101. req: correctReq,
  102. resCode: fiber.StatusInternalServerError,
  103. repoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  104. mock := mocks.NewThingNotificationRepositoryMock(mc)
  105. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  106. assert.Equal(mc, thingID, id)
  107. }).Return(&repoRes, nil)
  108. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  109. assert.Equal(mc, thingID, id)
  110. }).Return(testError)
  111. return mock
  112. },
  113. },
  114. }
  115. for _, tt := range tests {
  116. t.Run(tt.name, func(t *testing.T) {
  117. t.Parallel()
  118. mc := minimock.NewController(t)
  119. fiberApp := fiber.New()
  120. fiberApp.Delete("/v1/things/notifications/:thingId", DeleteThingNotificationHandler(tt.repoMock(mc)))
  121. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, nil)
  122. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  123. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  124. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  125. if tt.resBody != nil {
  126. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  127. }
  128. })
  129. }
  130. }