delete_thing_notification_test.go 4.4 KB

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