get_place_test.go 3.7 KB

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