get_place_nested_test.go 3.1 KB

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