search_thing_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package thing
  2. import (
  3. "context"
  4. "errors"
  5. "net/http/httptest"
  6. "net/url"
  7. "testing"
  8. "github.com/brianvoe/gofakeit/v6"
  9. "github.com/gofiber/fiber/v2"
  10. "github.com/gojuno/minimock/v3"
  11. "github.com/stretchr/testify/assert"
  12. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/thing/mocks"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  17. )
  18. func TestSearchThingHandler(t *testing.T) {
  19. t.Parallel()
  20. type req struct {
  21. method string
  22. route string
  23. }
  24. var (
  25. search = gofakeit.LetterN(10)
  26. testError = errors.New(gofakeit.Phrase())
  27. layout = "2006-01-02 15:04:05"
  28. correctReq = req{
  29. method: fiber.MethodGet,
  30. route: "/v1/things/search/" + url.QueryEscape(search),
  31. }
  32. thingRepoRes = []models.Thing{
  33. {
  34. ID: gofakeit.Number(1, 1000),
  35. PlaceID: gofakeit.Number(1, 1000),
  36. Title: gofakeit.Phrase(),
  37. Description: gofakeit.Phrase(),
  38. CreatedAt: gofakeit.Date(),
  39. UpdatedAt: gofakeit.Date(),
  40. },
  41. {
  42. ID: gofakeit.Number(1, 1000),
  43. PlaceID: gofakeit.Number(1, 1000),
  44. Title: gofakeit.Phrase(),
  45. Description: gofakeit.Phrase(),
  46. CreatedAt: gofakeit.Date(),
  47. UpdatedAt: gofakeit.Date(),
  48. },
  49. }
  50. expectedRes = dto.ThingsResponse{
  51. Things: []dto.ThingResponse{
  52. {
  53. ID: thingRepoRes[0].ID,
  54. PlaceID: thingRepoRes[0].PlaceID,
  55. Title: thingRepoRes[0].Title,
  56. Description: thingRepoRes[0].Description,
  57. CreatedAt: thingRepoRes[0].CreatedAt.Format(layout),
  58. UpdatedAt: thingRepoRes[0].UpdatedAt.Format(layout),
  59. },
  60. {
  61. ID: thingRepoRes[1].ID,
  62. PlaceID: thingRepoRes[1].PlaceID,
  63. Title: thingRepoRes[1].Title,
  64. Description: thingRepoRes[1].Description,
  65. CreatedAt: thingRepoRes[1].CreatedAt.Format(layout),
  66. UpdatedAt: thingRepoRes[1].UpdatedAt.Format(layout),
  67. },
  68. },
  69. }
  70. )
  71. tests := []struct {
  72. name string
  73. req req
  74. resCode int
  75. resBody interface{}
  76. thingRepoMock func(mc *minimock.Controller) ThingRepository
  77. }{
  78. {
  79. name: "negative case - bad request",
  80. req: req{
  81. method: fiber.MethodGet,
  82. route: "/v1/things/search/" + url.QueryEscape(gofakeit.LetterN(2)),
  83. },
  84. resCode: fiber.StatusBadRequest,
  85. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  86. return mocks.NewThingRepositoryMock(mc)
  87. },
  88. },
  89. {
  90. name: "negative case - bad request",
  91. req: req{
  92. method: fiber.MethodGet,
  93. route: "/v1/things/search/" + url.QueryEscape(gofakeit.LetterN(10)+":"),
  94. },
  95. resCode: fiber.StatusBadRequest,
  96. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  97. return mocks.NewThingRepositoryMock(mc)
  98. },
  99. },
  100. {
  101. name: "negative case - repository error",
  102. req: correctReq,
  103. resCode: fiber.StatusInternalServerError,
  104. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  105. mock := mocks.NewThingRepositoryMock(mc)
  106. mock.SearchMock.Inspect(func(ctx context.Context, s string) {
  107. assert.Equal(mc, search, s)
  108. }).Return(nil, testError)
  109. return mock
  110. },
  111. },
  112. {
  113. name: "positive case",
  114. req: correctReq,
  115. resCode: fiber.StatusOK,
  116. resBody: expectedRes,
  117. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  118. mock := mocks.NewThingRepositoryMock(mc)
  119. mock.SearchMock.Inspect(func(ctx context.Context, s string) {
  120. assert.Equal(mc, search, s)
  121. }).Return(thingRepoRes, nil)
  122. return mock
  123. },
  124. },
  125. }
  126. for _, tt := range tests {
  127. t.Run(tt.name, func(t *testing.T) {
  128. t.Parallel()
  129. mc := minimock.NewController(t)
  130. fiberApp := fiber.New()
  131. fiberApp.Get("/v1/things/search/:search", SearchThingHandler(tt.thingRepoMock(mc)))
  132. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  133. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  134. if tt.resBody != nil {
  135. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  136. }
  137. })
  138. }
  139. }