add_place_test.go 4.6 KB

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