add_place_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package place
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  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. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/place/mocks"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  17. )
  18. func TestAddPlaceHandler(t *testing.T) {
  19. t.Parallel()
  20. type req struct {
  21. method string
  22. route string
  23. contentType string
  24. body *dto.AddPlaceRequest
  25. }
  26. var (
  27. placeID = gofakeit.Number(1, 1000)
  28. parentID = gofakeit.Number(1, 1000)
  29. title = gofakeit.Phrase()
  30. testError = errors.New(gofakeit.Phrase())
  31. layout = "2006-01-02 15:04:05"
  32. correctReq = req{
  33. method: fiber.MethodPost,
  34. route: "/v1/places",
  35. body: &dto.AddPlaceRequest{
  36. Title: title,
  37. ParentID: &parentID,
  38. },
  39. contentType: fiber.MIMEApplicationJSON,
  40. }
  41. repoRes = models.Place{
  42. ID: placeID,
  43. Title: title,
  44. ParentID: sql.NullInt64{Int64: int64(parentID), Valid: true},
  45. CreatedAt: gofakeit.Date(),
  46. UpdatedAt: gofakeit.Date(),
  47. }
  48. expectedRes = dto.PlaceResponse{
  49. ID: placeID,
  50. ParentID: &parentID,
  51. Title: repoRes.Title,
  52. CreatedAt: repoRes.CreatedAt.Format(layout),
  53. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  54. }
  55. )
  56. tests := []struct {
  57. name string
  58. req req
  59. resCode int
  60. resBody interface{}
  61. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  62. }{
  63. {
  64. name: "positive case",
  65. req: correctReq,
  66. resCode: fiber.StatusOK,
  67. resBody: expectedRes,
  68. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  69. mock := mocks.NewPlaceRepositoryMock(mc)
  70. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceRequest, tx *sql.Tx) {
  71. assert.Equal(mc, title, req.Title)
  72. assert.Equal(mc, int64(parentID), req.ParentID.Int64)
  73. }).Return(placeID, nil)
  74. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  75. assert.Equal(mc, placeID, id)
  76. }).Return(&repoRes, nil)
  77. return mock
  78. },
  79. },
  80. {
  81. name: "negative case - body parse error",
  82. req: req{
  83. method: fiber.MethodPost,
  84. route: "/v1/places",
  85. },
  86. resCode: fiber.StatusBadRequest,
  87. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  88. return mocks.NewPlaceRepositoryMock(mc)
  89. },
  90. },
  91. {
  92. name: "negative case - request without title",
  93. req: req{
  94. method: fiber.MethodPost,
  95. route: "/v1/places",
  96. contentType: fiber.MIMEApplicationJSON,
  97. body: &dto.AddPlaceRequest{},
  98. },
  99. resCode: fiber.StatusBadRequest,
  100. resBody: []*dto.ValidateErrorResponse{
  101. {
  102. Field: "AddPlaceRequest.Title",
  103. Tag: "required",
  104. },
  105. },
  106. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  107. return mocks.NewPlaceRepositoryMock(mc)
  108. },
  109. },
  110. {
  111. name: "negative case - repository error (add place)",
  112. req: correctReq,
  113. resCode: fiber.StatusInternalServerError,
  114. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  115. mock := mocks.NewPlaceRepositoryMock(mc)
  116. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceRequest, tx *sql.Tx) {
  117. assert.Equal(mc, title, req.Title)
  118. assert.Equal(mc, int64(parentID), req.ParentID.Int64)
  119. }).Return(0, testError)
  120. return mock
  121. },
  122. },
  123. {
  124. name: "negative case - repository error (get place)",
  125. req: correctReq,
  126. resCode: fiber.StatusInternalServerError,
  127. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  128. mock := mocks.NewPlaceRepositoryMock(mc)
  129. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceRequest, tx *sql.Tx) {
  130. assert.Equal(mc, title, req.Title)
  131. assert.Equal(mc, int64(parentID), req.ParentID.Int64)
  132. }).Return(placeID, nil)
  133. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  134. assert.Equal(mc, placeID, id)
  135. }).Return(nil, testError)
  136. return mock
  137. },
  138. },
  139. }
  140. for _, tt := range tests {
  141. t.Run(tt.name, func(t *testing.T) {
  142. t.Parallel()
  143. mc := minimock.NewController(t)
  144. fiberApp := fiber.New()
  145. fiberApp.Post("/v1/places", AddPlaceHandler(tt.placeRepoMock(mc)))
  146. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  147. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  148. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  149. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  150. if tt.resBody != nil {
  151. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  152. }
  153. })
  154. }
  155. }