get_thing_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package thing
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  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. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/thing/mocks"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. func TestGetThingHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. thingID = gofakeit.Number(1, 1000)
  27. placeID = gofakeit.Number(1, 1000)
  28. testError = errors.New(gofakeit.Phrase())
  29. layout = "2006-01-02 15:04:05"
  30. correctReq = req{
  31. method: fiber.MethodGet,
  32. route: "/v1/things/" + strconv.Itoa(thingID),
  33. }
  34. thingRepoRes = models.Thing{
  35. ID: thingID,
  36. PlaceID: placeID,
  37. Title: gofakeit.Phrase(),
  38. Description: gofakeit.Phrase(),
  39. CreatedAt: gofakeit.Date(),
  40. UpdatedAt: gofakeit.Date(),
  41. }
  42. expectedRes = dto.ThingResponse{
  43. ID: thingID,
  44. PlaceID: placeID,
  45. Title: thingRepoRes.Title,
  46. Description: thingRepoRes.Description,
  47. CreatedAt: thingRepoRes.CreatedAt.Format(layout),
  48. UpdatedAt: thingRepoRes.UpdatedAt.Format(layout),
  49. }
  50. )
  51. tests := []struct {
  52. name string
  53. req req
  54. resCode int
  55. resBody interface{}
  56. thingRepoMock func(mc *minimock.Controller) ThingRepository
  57. }{
  58. {
  59. name: "positive case",
  60. req: correctReq,
  61. resCode: fiber.StatusOK,
  62. resBody: expectedRes,
  63. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  64. mock := mocks.NewThingRepositoryMock(mc)
  65. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  66. assert.Equal(mc, thingID, id)
  67. }).Return(&thingRepoRes, nil)
  68. return mock
  69. },
  70. },
  71. {
  72. name: "negative case - not found",
  73. req: correctReq,
  74. resCode: fiber.StatusNotFound,
  75. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  76. mock := mocks.NewThingRepositoryMock(mc)
  77. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  78. assert.Equal(mc, thingID, id)
  79. }).Return(nil, sql.ErrNoRows)
  80. return mock
  81. },
  82. },
  83. {
  84. name: "negative case - repository error",
  85. req: correctReq,
  86. resCode: fiber.StatusInternalServerError,
  87. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  88. mock := mocks.NewThingRepositoryMock(mc)
  89. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  90. assert.Equal(mc, thingID, id)
  91. }).Return(nil, testError)
  92. return mock
  93. },
  94. },
  95. {
  96. name: "negative case - bad request",
  97. req: req{
  98. method: fiber.MethodGet,
  99. route: "/v1/things/" + gofakeit.Word(),
  100. },
  101. resCode: fiber.StatusBadRequest,
  102. resBody: nil,
  103. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  104. return mocks.NewThingRepositoryMock(mc)
  105. },
  106. },
  107. }
  108. for _, tt := range tests {
  109. t.Run(tt.name, func(t *testing.T) {
  110. t.Parallel()
  111. mc := minimock.NewController(t)
  112. fiberApp := fiber.New()
  113. fiberApp.Get("/v1/things/:thingId", GetThingHandler(tt.thingRepoMock(mc)))
  114. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  115. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  116. if tt.resBody != nil {
  117. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  118. }
  119. })
  120. }
  121. }