get_place_tree_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TestGetPlaceTreeHandler(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/tree",
  32. }
  33. placeRepoRes = []models.Place{
  34. {
  35. ID: id1,
  36. Title: 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: 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: gofakeit.Phrase(),
  51. CreatedAt: gofakeit.Date(),
  52. UpdatedAt: gofakeit.Date(),
  53. },
  54. }
  55. expectedRes = dto.PlaceTreeResponse{
  56. Places: []dto.PlaceTree{
  57. {
  58. Place: dto.PlaceResponse{
  59. ID: id1,
  60. ParentID: nil,
  61. Title: placeRepoRes[0].Title,
  62. CreatedAt: placeRepoRes[0].CreatedAt.Format(layout),
  63. UpdatedAt: placeRepoRes[0].UpdatedAt.Format(layout),
  64. },
  65. NestedPlaces: []dto.PlaceTree{
  66. {
  67. Place: dto.PlaceResponse{
  68. ID: id2,
  69. ParentID: &id1,
  70. Title: placeRepoRes[1].Title,
  71. CreatedAt: placeRepoRes[1].CreatedAt.Format(layout),
  72. UpdatedAt: placeRepoRes[1].UpdatedAt.Format(layout),
  73. },
  74. NestedPlaces: []dto.PlaceTree{
  75. {
  76. Place: dto.PlaceResponse{
  77. ID: id3,
  78. ParentID: &id2,
  79. Title: placeRepoRes[2].Title,
  80. CreatedAt: placeRepoRes[2].CreatedAt.Format(layout),
  81. UpdatedAt: placeRepoRes[2].UpdatedAt.Format(layout),
  82. },
  83. },
  84. },
  85. },
  86. },
  87. },
  88. },
  89. }
  90. )
  91. tests := []struct {
  92. name string
  93. req req
  94. resCode int
  95. resBody interface{}
  96. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  97. }{
  98. {
  99. name: "positive case",
  100. req: correctReq,
  101. resCode: fiber.StatusOK,
  102. resBody: expectedRes,
  103. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  104. mock := mocks.NewPlaceRepositoryMock(mc)
  105. mock.GetAllMock.Return(placeRepoRes, nil)
  106. return mock
  107. },
  108. },
  109. {
  110. name: "negative case - repository error",
  111. req: correctReq,
  112. resCode: fiber.StatusInternalServerError,
  113. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  114. mock := mocks.NewPlaceRepositoryMock(mc)
  115. mock.GetAllMock.Return(nil, testError)
  116. return mock
  117. },
  118. },
  119. }
  120. for _, tt := range tests {
  121. t.Run(tt.name, func(t *testing.T) {
  122. t.Parallel()
  123. mc := minimock.NewController(t)
  124. fiberApp := fiber.New()
  125. fiberApp.Get("/v1/places/tree", GetPlaceTreeHandler(tt.placeRepoMock(mc)))
  126. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  127. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  128. if tt.resBody != nil {
  129. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  130. }
  131. })
  132. }
  133. }