get_places_test.go 3.4 KB

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