get_thing_test.go 3.5 KB

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