delete_thing_notification_test.go 4.1 KB

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