get_place_nested_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 TestGetNestedPlacesHandler(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. testError = errors.New(gofakeit.Phrase())
  28. layout = "2006-01-02 15:04:05"
  29. correctReq = req{
  30. method: fiber.MethodGet,
  31. route: "/v1/places/" + strconv.Itoa(placeID) + "/nested",
  32. }
  33. repoRes = []models.Place{
  34. {
  35. ID: placeID,
  36. ParentID: sql.NullInt64{Int64: int64(placeID), Valid: true},
  37. Title: gofakeit.Phrase(),
  38. CreatedAt: gofakeit.Date(),
  39. UpdatedAt: gofakeit.Date(),
  40. },
  41. }
  42. expectedRes = dto.PlacesResponse{
  43. Places: []dto.PlaceResponse{
  44. {
  45. ID: repoRes[0].ID,
  46. ParentID: &placeID,
  47. Title: repoRes[0].Title,
  48. CreatedAt: repoRes[0].CreatedAt.Format(layout),
  49. UpdatedAt: repoRes[0].UpdatedAt.Format(layout),
  50. },
  51. },
  52. }
  53. )
  54. tests := []struct {
  55. name string
  56. req req
  57. resCode int
  58. resBody interface{}
  59. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  60. }{
  61. {
  62. name: "positive case",
  63. req: correctReq,
  64. resCode: fiber.StatusOK,
  65. resBody: expectedRes,
  66. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  67. mock := mocks.NewPlaceRepositoryMock(mc)
  68. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  69. assert.Equal(mc, placeID, id)
  70. }).Return(repoRes, nil)
  71. return mock
  72. },
  73. },
  74. {
  75. name: "negative case - repository error",
  76. req: correctReq,
  77. resCode: fiber.StatusInternalServerError,
  78. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  79. mock := mocks.NewPlaceRepositoryMock(mc)
  80. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  81. assert.Equal(mc, placeID, id)
  82. }).Return(nil, testError)
  83. return mock
  84. },
  85. },
  86. {
  87. name: "negative case - bad request",
  88. req: req{
  89. method: fiber.MethodGet,
  90. route: "/v1/places/" + gofakeit.Word() + "/nested",
  91. },
  92. resCode: fiber.StatusBadRequest,
  93. resBody: nil,
  94. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  95. return mocks.NewPlaceRepositoryMock(mc)
  96. },
  97. },
  98. }
  99. for _, tt := range tests {
  100. t.Run(tt.name, func(t *testing.T) {
  101. t.Parallel()
  102. mc := minimock.NewController(t)
  103. fiberApp := fiber.New()
  104. fiberApp.Get("/v1/places/:parentPlaceId/nested", GetNestedPlacesHandler(tt.placeRepoMock(mc)))
  105. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  106. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  107. if tt.resBody != nil {
  108. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  109. }
  110. })
  111. }
  112. }