get_place_things_test.go 6.3 KB

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