get_place_tree_test.go 3.9 KB

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