get_place_nested_test.go 3.4 KB

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