get_place_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package place
  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/place/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 TestGetPlaceHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. placeID = gofakeit.Number(1, 1000)
  27. parentID = 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/places/" + strconv.Itoa(placeID),
  33. }
  34. repoRes = models.Place{
  35. ID: placeID,
  36. ParentID: sql.NullInt64{Int64: int64(parentID), Valid: true},
  37. Title: gofakeit.Phrase(),
  38. CreatedAt: gofakeit.Date(),
  39. UpdatedAt: gofakeit.Date(),
  40. }
  41. expectedRes = dto.PlaceResponse{
  42. ID: placeID,
  43. Title: repoRes.Title,
  44. ParentID: &parentID,
  45. CreatedAt: repoRes.CreatedAt.Format(layout),
  46. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  47. }
  48. )
  49. tests := []struct {
  50. name string
  51. req req
  52. resCode int
  53. resBody interface{}
  54. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  55. }{
  56. {
  57. name: "positive case",
  58. req: correctReq,
  59. resCode: fiber.StatusOK,
  60. resBody: expectedRes,
  61. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  62. mock := mocks.NewPlaceRepositoryMock(mc)
  63. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  64. assert.Equal(mc, placeID, id)
  65. }).Return(&repoRes, nil)
  66. return mock
  67. },
  68. },
  69. {
  70. name: "negative case - not found",
  71. req: correctReq,
  72. resCode: fiber.StatusNotFound,
  73. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  74. mock := mocks.NewPlaceRepositoryMock(mc)
  75. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  76. assert.Equal(mc, placeID, 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. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  86. mock := mocks.NewPlaceRepositoryMock(mc)
  87. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  88. assert.Equal(mc, placeID, 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/places/" + gofakeit.Word(),
  98. },
  99. resCode: fiber.StatusBadRequest,
  100. resBody: nil,
  101. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  102. return mocks.NewPlaceRepositoryMock(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/places/:placeId", GetPlaceHandler(tt.placeRepoMock(mc)))
  112. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  113. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  114. if tt.resBody != nil {
  115. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  116. }
  117. })
  118. }
  119. }