add_thing_test.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package thing
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  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_AddThingHandler(t *testing.T) {
  21. type req struct {
  22. method string
  23. route string
  24. contentType string
  25. body *dto.AddThingRequest
  26. }
  27. var (
  28. mc = minimock.NewController(t)
  29. placeID = gofakeit.Number(1, 1000)
  30. thingID = gofakeit.Number(1, 1000)
  31. title = gofakeit.Phrase()
  32. description = gofakeit.Phrase()
  33. testError = errors.New(gofakeit.Phrase())
  34. layout = "2006-01-02 15:04:05"
  35. correctReq = req{
  36. method: fiber.MethodPost,
  37. route: "/v1/things",
  38. body: &dto.AddThingRequest{
  39. PlaceID: placeID,
  40. Title: title,
  41. Description: description,
  42. },
  43. contentType: fiber.MIMEApplicationJSON,
  44. }
  45. repoRes = models.Thing{
  46. ID: thingID,
  47. PlaceID: placeID,
  48. Title: title,
  49. Description: description,
  50. CreatedAt: gofakeit.Date(),
  51. UpdatedAt: gofakeit.Date(),
  52. }
  53. expectedRes = dto.ThingResponse{
  54. ID: thingID,
  55. PlaceID: placeID,
  56. Title: title,
  57. Description: description,
  58. CreatedAt: repoRes.CreatedAt.Format(layout),
  59. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  60. }
  61. )
  62. tests := []struct {
  63. name string
  64. req req
  65. resCode int
  66. resBody interface{}
  67. thingRepoMock func(mc *minimock.Controller) interfaces.ThingRepository
  68. placeThingRepoMock func(mc *minimock.Controller) interfaces.PlaceThingRepository
  69. }{
  70. {
  71. name: "positive case",
  72. req: correctReq,
  73. resCode: fiber.StatusOK,
  74. resBody: expectedRes,
  75. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  76. mock := repoMocks.NewThingRepositoryMock(mc)
  77. mock.BeginTxMock.Return(nil, nil)
  78. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) {
  79. assert.Equal(mc, title, req.Title)
  80. assert.Equal(mc, description, req.Description)
  81. }).Return(thingID, nil)
  82. mock.CommitTxMock.Return(nil)
  83. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  84. assert.Equal(mc, thingID, id)
  85. }).Return(&repoRes, nil)
  86. return mock
  87. },
  88. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  89. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  90. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) {
  91. assert.Equal(mc, thingID, req.ThingID)
  92. assert.Equal(mc, placeID, req.PlaceID)
  93. }).Return(nil)
  94. return mock
  95. },
  96. },
  97. {
  98. name: "negative case - body parse error",
  99. req: req{
  100. method: fiber.MethodPost,
  101. route: "/v1/things",
  102. },
  103. resCode: fiber.StatusBadRequest,
  104. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  105. return repoMocks.NewThingRepositoryMock(mc)
  106. },
  107. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  108. return repoMocks.NewPlaceThingRepositoryMock(mc)
  109. },
  110. },
  111. {
  112. name: "negative case - request without place_id",
  113. req: req{
  114. method: fiber.MethodPost,
  115. route: "/v1/things",
  116. contentType: fiber.MIMEApplicationJSON,
  117. body: &dto.AddThingRequest{
  118. Title: title,
  119. Description: description,
  120. },
  121. },
  122. resCode: fiber.StatusBadRequest,
  123. resBody: []*dto.ValidateErrorResponse{
  124. {
  125. Field: "AddThingRequest.PlaceID",
  126. Tag: "required",
  127. },
  128. },
  129. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  130. return repoMocks.NewThingRepositoryMock(mc)
  131. },
  132. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  133. return repoMocks.NewPlaceThingRepositoryMock(mc)
  134. },
  135. },
  136. {
  137. name: "negative case - request without title",
  138. req: req{
  139. method: fiber.MethodPost,
  140. route: "/v1/things",
  141. contentType: fiber.MIMEApplicationJSON,
  142. body: &dto.AddThingRequest{
  143. PlaceID: placeID,
  144. Description: description,
  145. },
  146. },
  147. resCode: fiber.StatusBadRequest,
  148. resBody: []*dto.ValidateErrorResponse{
  149. {
  150. Field: "AddThingRequest.Title",
  151. Tag: "required",
  152. },
  153. },
  154. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  155. return repoMocks.NewThingRepositoryMock(mc)
  156. },
  157. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  158. return repoMocks.NewPlaceThingRepositoryMock(mc)
  159. },
  160. },
  161. {
  162. name: "negative case - repository error (begin tx)",
  163. req: correctReq,
  164. resCode: fiber.StatusInternalServerError,
  165. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  166. mock := repoMocks.NewThingRepositoryMock(mc)
  167. mock.BeginTxMock.Return(nil, testError)
  168. return mock
  169. },
  170. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  171. return repoMocks.NewPlaceThingRepositoryMock(mc)
  172. },
  173. },
  174. {
  175. name: "negative case - repository error (add thing)",
  176. req: correctReq,
  177. resCode: fiber.StatusInternalServerError,
  178. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  179. mock := repoMocks.NewThingRepositoryMock(mc)
  180. mock.BeginTxMock.Return(nil, nil)
  181. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) {
  182. assert.Equal(mc, title, req.Title)
  183. assert.Equal(mc, description, req.Description)
  184. }).Return(0, testError)
  185. return mock
  186. },
  187. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  188. return repoMocks.NewPlaceThingRepositoryMock(mc)
  189. },
  190. },
  191. {
  192. name: "negative case - repository error (add place thing)",
  193. req: correctReq,
  194. resCode: fiber.StatusInternalServerError,
  195. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  196. mock := repoMocks.NewThingRepositoryMock(mc)
  197. mock.BeginTxMock.Return(nil, nil)
  198. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) {
  199. assert.Equal(mc, title, req.Title)
  200. assert.Equal(mc, description, req.Description)
  201. }).Return(thingID, nil)
  202. return mock
  203. },
  204. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  205. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  206. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) {
  207. assert.Equal(mc, thingID, req.ThingID)
  208. assert.Equal(mc, placeID, req.PlaceID)
  209. }).Return(testError)
  210. return mock
  211. },
  212. },
  213. {
  214. name: "negative case - repository error (commit tx)",
  215. req: correctReq,
  216. resCode: fiber.StatusInternalServerError,
  217. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  218. mock := repoMocks.NewThingRepositoryMock(mc)
  219. mock.BeginTxMock.Return(nil, nil)
  220. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) {
  221. assert.Equal(mc, title, req.Title)
  222. assert.Equal(mc, description, req.Description)
  223. }).Return(thingID, nil)
  224. mock.CommitTxMock.Return(testError)
  225. return mock
  226. },
  227. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  228. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  229. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) {
  230. assert.Equal(mc, thingID, req.ThingID)
  231. assert.Equal(mc, placeID, req.PlaceID)
  232. }).Return(nil)
  233. return mock
  234. },
  235. },
  236. {
  237. name: "negative case - repository error (get thing)",
  238. req: correctReq,
  239. resCode: fiber.StatusInternalServerError,
  240. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  241. mock := repoMocks.NewThingRepositoryMock(mc)
  242. mock.BeginTxMock.Return(nil, nil)
  243. mock.AddMock.Inspect(func(ctx context.Context, req models.AddThingRequest, tx *sql.Tx) {
  244. assert.Equal(mc, title, req.Title)
  245. assert.Equal(mc, description, req.Description)
  246. }).Return(thingID, nil)
  247. mock.CommitTxMock.Return(nil)
  248. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  249. assert.Equal(mc, thingID, id)
  250. }).Return(nil, sql.ErrNoRows)
  251. return mock
  252. },
  253. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  254. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  255. mock.AddMock.Inspect(func(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) {
  256. assert.Equal(mc, thingID, req.ThingID)
  257. assert.Equal(mc, placeID, req.PlaceID)
  258. }).Return(nil)
  259. return mock
  260. },
  261. },
  262. }
  263. for _, tt := range tests {
  264. t.Run(tt.name, func(t *testing.T) {
  265. fiberApp := fiber.New()
  266. serviceProvider := sp.InitMock(tt.thingRepoMock(mc), tt.placeThingRepoMock(mc))
  267. fiberApp.Post("/v1/things", AddThingHandler(serviceProvider))
  268. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  269. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  270. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  271. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  272. if tt.resBody != nil {
  273. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  274. }
  275. })
  276. }
  277. }