get_place_test.go 3.3 KB

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