get_place_things_test.go 6.3 KB

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