get_places_test.go 3.4 KB

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