get_places_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package place
  2. import (
  3. "database/sql"
  4. "errors"
  5. "net/http/httptest"
  6. "testing"
  7. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  12. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  13. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  14. "github.com/brianvoe/gofakeit/v6"
  15. "github.com/gofiber/fiber/v2"
  16. "github.com/gojuno/minimock/v3"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func Test_GetPlacesHandler(t *testing.T) {
  20. type req struct {
  21. method string
  22. route string
  23. }
  24. var (
  25. mc = minimock.NewController(t)
  26. testError = errors.New(gofakeit.Phrase())
  27. layout = "2006-01-02 15:04:05"
  28. id1 = gofakeit.Number(1, 1000)
  29. id2 = gofakeit.Number(1, 1000)
  30. id3 = gofakeit.Number(1, 1000)
  31. correctReq = req{
  32. method: fiber.MethodGet,
  33. route: "/v1/places",
  34. }
  35. placeRepoRes = []models.Place{
  36. {
  37. ID: id1,
  38. Title: "A " + gofakeit.Phrase(),
  39. CreatedAt: gofakeit.Date(),
  40. UpdatedAt: gofakeit.Date(),
  41. },
  42. {
  43. ID: id2,
  44. ParentID: sql.NullInt64{Valid: true, Int64: int64(id1)},
  45. Title: "B " + gofakeit.Phrase(),
  46. CreatedAt: gofakeit.Date(),
  47. UpdatedAt: gofakeit.Date(),
  48. },
  49. {
  50. ID: id3,
  51. ParentID: sql.NullInt64{Valid: true, Int64: int64(id2)},
  52. Title: "C " + gofakeit.Phrase(),
  53. CreatedAt: gofakeit.Date(),
  54. UpdatedAt: gofakeit.Date(),
  55. },
  56. }
  57. expectedRes = dto.PlacesResponse{
  58. Places: []dto.PlaceResponse{
  59. {
  60. ID: id1,
  61. ParentID: nil,
  62. Title: placeRepoRes[0].Title,
  63. CreatedAt: placeRepoRes[0].CreatedAt.Format(layout),
  64. UpdatedAt: placeRepoRes[0].UpdatedAt.Format(layout),
  65. },
  66. {
  67. ID: id2,
  68. ParentID: &id1,
  69. Title: placeRepoRes[1].Title,
  70. CreatedAt: placeRepoRes[1].CreatedAt.Format(layout),
  71. UpdatedAt: placeRepoRes[1].UpdatedAt.Format(layout),
  72. },
  73. {
  74. ID: id3,
  75. ParentID: &id2,
  76. Title: placeRepoRes[2].Title,
  77. CreatedAt: placeRepoRes[2].CreatedAt.Format(layout),
  78. UpdatedAt: placeRepoRes[2].UpdatedAt.Format(layout),
  79. },
  80. },
  81. }
  82. )
  83. tests := []struct {
  84. name string
  85. req req
  86. resCode int
  87. resBody interface{}
  88. placeRepoMock func(mc *minimock.Controller) interfaces.PlaceRepository
  89. }{
  90. {
  91. name: "positive case",
  92. req: correctReq,
  93. resCode: fiber.StatusOK,
  94. resBody: expectedRes,
  95. placeRepoMock: func(mc *minimock.Controller) interfaces.PlaceRepository {
  96. mock := repoMocks.NewPlaceRepositoryMock(mc)
  97. mock.GetAllMock.Return(placeRepoRes, nil)
  98. return mock
  99. },
  100. },
  101. {
  102. name: "negative case - repository error",
  103. req: correctReq,
  104. resCode: fiber.StatusInternalServerError,
  105. placeRepoMock: func(mc *minimock.Controller) interfaces.PlaceRepository {
  106. mock := repoMocks.NewPlaceRepositoryMock(mc)
  107. mock.GetAllMock.Return(nil, testError)
  108. return mock
  109. },
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. fiberApp := fiber.New()
  115. serviceProvider := sp.InitMock(tt.placeRepoMock(mc))
  116. fiberApp.Get("/v1/places", GetPlacesHandler(serviceProvider))
  117. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  118. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  119. if tt.resBody != nil {
  120. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  121. }
  122. })
  123. }
  124. }