get_thing_test.go 3.7 KB

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