get_place_tree_test.go 3.6 KB

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