get_place_things_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package thing
  2. import (
  3. "context"
  4. "errors"
  5. "net/http/httptest"
  6. "strconv"
  7. "testing"
  8. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  13. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  14. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  15. "github.com/brianvoe/gofakeit/v6"
  16. "github.com/gofiber/fiber/v2"
  17. "github.com/gojuno/minimock/v3"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. func Test_GetPlaceThingsHandler(t *testing.T) {
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. mc = minimock.NewController(t)
  27. placeID = gofakeit.Number(1, 1000)
  28. testError = errors.New(gofakeit.Phrase())
  29. layout = "2006-01-02 15:04:05"
  30. correctReq = req{
  31. method: fiber.MethodGet,
  32. route: "/v1/things/place/" + strconv.Itoa(placeID),
  33. }
  34. thingRepoRes = []models.Thing{
  35. {
  36. ID: gofakeit.Number(1, 1000),
  37. PlaceID: placeID,
  38. Title: gofakeit.Phrase(),
  39. Description: gofakeit.Phrase(),
  40. CreatedAt: gofakeit.Date(),
  41. UpdatedAt: gofakeit.Date(),
  42. },
  43. {
  44. ID: gofakeit.Number(1, 1000),
  45. PlaceID: placeID,
  46. Title: gofakeit.Phrase(),
  47. Description: gofakeit.Phrase(),
  48. CreatedAt: gofakeit.Date(),
  49. UpdatedAt: gofakeit.Date(),
  50. },
  51. }
  52. thingTagRepoRes = []models.ThingTag{
  53. {
  54. ThingID: thingRepoRes[0].ID,
  55. Tag: models.Tag{
  56. ID: gofakeit.Number(1, 1000),
  57. Title: gofakeit.Phrase(),
  58. Style: gofakeit.Phrase(),
  59. CreatedAt: gofakeit.Date(),
  60. UpdatedAt: gofakeit.Date(),
  61. },
  62. },
  63. {
  64. ThingID: thingRepoRes[1].ID,
  65. Tag: models.Tag{
  66. ID: gofakeit.Number(1, 1000),
  67. Title: gofakeit.Phrase(),
  68. Style: gofakeit.Phrase(),
  69. CreatedAt: gofakeit.Date(),
  70. UpdatedAt: gofakeit.Date(),
  71. },
  72. },
  73. }
  74. expectedRes = dto.ThingsExtResponse{
  75. Things: []dto.ThingExtResponse{
  76. {
  77. ThingResponse: dto.ThingResponse{
  78. ID: thingRepoRes[0].ID,
  79. PlaceID: thingRepoRes[0].PlaceID,
  80. Title: thingRepoRes[0].Title,
  81. Description: thingRepoRes[0].Description,
  82. CreatedAt: thingRepoRes[0].CreatedAt.Format(layout),
  83. UpdatedAt: thingRepoRes[0].UpdatedAt.Format(layout),
  84. },
  85. Tags: []dto.TagResponse{
  86. {
  87. ID: thingTagRepoRes[0].ID,
  88. Title: thingTagRepoRes[0].Title,
  89. Style: thingTagRepoRes[0].Style,
  90. CreatedAt: thingTagRepoRes[0].CreatedAt.Format(layout),
  91. UpdatedAt: thingTagRepoRes[0].UpdatedAt.Format(layout),
  92. },
  93. },
  94. },
  95. {
  96. ThingResponse: dto.ThingResponse{
  97. ID: thingRepoRes[1].ID,
  98. PlaceID: thingRepoRes[1].PlaceID,
  99. Title: thingRepoRes[1].Title,
  100. Description: thingRepoRes[1].Description,
  101. CreatedAt: thingRepoRes[1].CreatedAt.Format(layout),
  102. UpdatedAt: thingRepoRes[1].UpdatedAt.Format(layout),
  103. },
  104. Tags: []dto.TagResponse{
  105. {
  106. ID: thingTagRepoRes[1].ID,
  107. Title: thingTagRepoRes[1].Title,
  108. Style: thingTagRepoRes[1].Style,
  109. CreatedAt: thingTagRepoRes[1].CreatedAt.Format(layout),
  110. UpdatedAt: thingTagRepoRes[1].UpdatedAt.Format(layout),
  111. },
  112. },
  113. },
  114. },
  115. }
  116. )
  117. tests := []struct {
  118. name string
  119. req req
  120. resCode int
  121. resBody interface{}
  122. thingRepoMock func(mc *minimock.Controller) interfaces.ThingRepository
  123. thingTagRepoMock func(mc *minimock.Controller) interfaces.ThingTagRepository
  124. }{
  125. {
  126. name: "positive case",
  127. req: correctReq,
  128. resCode: fiber.StatusOK,
  129. resBody: expectedRes,
  130. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  131. mock := repoMocks.NewThingRepositoryMock(mc)
  132. mock.GetAllByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  133. assert.Equal(mc, placeID, id)
  134. }).Return(thingRepoRes, nil)
  135. return mock
  136. },
  137. thingTagRepoMock: func(mc *minimock.Controller) interfaces.ThingTagRepository {
  138. mock := repoMocks.NewThingTagRepositoryMock(mc)
  139. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  140. assert.Equal(mc, placeID, id)
  141. }).Return(thingTagRepoRes, nil)
  142. return mock
  143. },
  144. },
  145. {
  146. name: "negative case - thing repository error",
  147. req: correctReq,
  148. resCode: fiber.StatusInternalServerError,
  149. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  150. mock := repoMocks.NewThingRepositoryMock(mc)
  151. mock.GetAllByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  152. assert.Equal(mc, placeID, id)
  153. }).Return(nil, testError)
  154. return mock
  155. },
  156. thingTagRepoMock: func(mc *minimock.Controller) interfaces.ThingTagRepository {
  157. return repoMocks.NewThingTagRepositoryMock(mc)
  158. },
  159. },
  160. {
  161. name: "negative case - thing tag repository error",
  162. req: correctReq,
  163. resCode: fiber.StatusInternalServerError,
  164. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  165. mock := repoMocks.NewThingRepositoryMock(mc)
  166. mock.GetAllByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  167. assert.Equal(mc, placeID, id)
  168. }).Return(thingRepoRes, nil)
  169. return mock
  170. },
  171. thingTagRepoMock: func(mc *minimock.Controller) interfaces.ThingTagRepository {
  172. mock := repoMocks.NewThingTagRepositoryMock(mc)
  173. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  174. assert.Equal(mc, placeID, id)
  175. }).Return(nil, testError)
  176. return mock
  177. },
  178. },
  179. {
  180. name: "negative case - bad request",
  181. req: req{
  182. method: fiber.MethodGet,
  183. route: "/v1/things/place/" + gofakeit.Word(),
  184. },
  185. resCode: fiber.StatusBadRequest,
  186. resBody: nil,
  187. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  188. return repoMocks.NewThingRepositoryMock(mc)
  189. },
  190. thingTagRepoMock: func(mc *minimock.Controller) interfaces.ThingTagRepository {
  191. return repoMocks.NewThingTagRepositoryMock(mc)
  192. },
  193. },
  194. }
  195. for _, tt := range tests {
  196. t.Run(tt.name, func(t *testing.T) {
  197. fiberApp := fiber.New()
  198. serviceProvider := sp.InitMock(tt.thingRepoMock(mc), tt.thingTagRepoMock(mc))
  199. fiberApp.Get("/v1/things/place/:placeId", GetPlaceThingsHandler(serviceProvider))
  200. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  201. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  202. if tt.resBody != nil {
  203. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  204. }
  205. })
  206. }
  207. }