update_thing_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package thing
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  11. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  12. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  13. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  14. repoMocks "git.dmitriygnatenko.ru/dima/homethings/internal/repositories/mocks"
  15. sp "git.dmitriygnatenko.ru/dima/homethings/internal/service_provider"
  16. "github.com/brianvoe/gofakeit/v6"
  17. "github.com/gofiber/fiber/v2"
  18. "github.com/gojuno/minimock/v3"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func Test_UpdateThingHandler(t *testing.T) {
  22. type req struct {
  23. method string
  24. route string
  25. contentType string
  26. body *dto.UpdateThingRequest
  27. }
  28. var (
  29. mc = minimock.NewController(t)
  30. placeID = gofakeit.Number(1, 1000)
  31. thingID = gofakeit.Number(1, 1000)
  32. title = gofakeit.Phrase()
  33. description = gofakeit.Phrase()
  34. testError = errors.New(gofakeit.Phrase())
  35. layout = "2006-01-02 15:04:05"
  36. correctReq = req{
  37. method: fiber.MethodPut,
  38. route: "/v1/things/" + strconv.Itoa(thingID),
  39. body: &dto.UpdateThingRequest{
  40. PlaceID: placeID,
  41. Title: title,
  42. Description: description,
  43. },
  44. contentType: fiber.MIMEApplicationJSON,
  45. }
  46. repoResBeforeUpdate = models.Thing{
  47. ID: thingID,
  48. Title: gofakeit.Phrase(),
  49. Description: gofakeit.Phrase(),
  50. CreatedAt: gofakeit.Date(),
  51. UpdatedAt: gofakeit.Date(),
  52. }
  53. placeThingRepoResBeforeUpdate = models.PlaceThing{
  54. PlaceID: gofakeit.Number(1, 1000),
  55. ThingID: thingID,
  56. CreatedAt: gofakeit.Date(),
  57. }
  58. repoRes = models.Thing{
  59. ID: thingID,
  60. Title: title,
  61. Description: description,
  62. CreatedAt: gofakeit.Date(),
  63. UpdatedAt: gofakeit.Date(),
  64. }
  65. expectedRes = dto.ThingResponse{
  66. ID: thingID,
  67. Title: title,
  68. Description: description,
  69. CreatedAt: repoRes.CreatedAt.Format(layout),
  70. UpdatedAt: repoRes.UpdatedAt.Format(layout),
  71. }
  72. )
  73. tests := []struct {
  74. name string
  75. req req
  76. resCode int
  77. resBody interface{}
  78. thingRepoMock func(mc *minimock.Controller) interfaces.ThingRepository
  79. placeThingRepoMock func(mc *minimock.Controller) interfaces.PlaceThingRepository
  80. }{
  81. {
  82. name: "positive case",
  83. req: correctReq,
  84. resCode: fiber.StatusOK,
  85. resBody: expectedRes,
  86. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  87. mock := repoMocks.NewThingRepositoryMock(mc)
  88. mock.BeginTxMock.Return(nil, nil)
  89. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  90. assert.Equal(mc, title, req.Title)
  91. assert.Equal(mc, description, req.Description)
  92. }).Return(nil)
  93. mock.CommitTxMock.Return(nil)
  94. mock.GetMock.Set(func(ctx context.Context, id int) (*models.Thing, error) {
  95. assert.Equal(mc, thingID, id)
  96. if mock.GetAfterCounter() == 0 {
  97. return &repoResBeforeUpdate, nil
  98. }
  99. return &repoRes, nil
  100. })
  101. return mock
  102. },
  103. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  104. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  105. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  106. assert.Equal(mc, thingID, id)
  107. }).Return(&placeThingRepoResBeforeUpdate, nil)
  108. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  109. assert.Equal(mc, placeID, req.PlaceID)
  110. assert.Equal(mc, thingID, req.ThingID)
  111. }).Return(nil)
  112. return mock
  113. },
  114. },
  115. {
  116. name: "negative case - bad request",
  117. req: req{
  118. method: fiber.MethodPut,
  119. route: "/v1/things/" + gofakeit.Word(),
  120. },
  121. resCode: fiber.StatusBadRequest,
  122. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  123. return repoMocks.NewThingRepositoryMock(mc)
  124. },
  125. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  126. return repoMocks.NewPlaceThingRepositoryMock(mc)
  127. },
  128. },
  129. {
  130. name: "negative case - body parse error",
  131. req: req{
  132. method: fiber.MethodPut,
  133. route: "/v1/things/" + strconv.Itoa(thingID),
  134. },
  135. resCode: fiber.StatusBadRequest,
  136. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  137. return repoMocks.NewThingRepositoryMock(mc)
  138. },
  139. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  140. return repoMocks.NewPlaceThingRepositoryMock(mc)
  141. },
  142. },
  143. {
  144. name: "negative case - request without place_id",
  145. req: req{
  146. method: fiber.MethodPut,
  147. route: "/v1/things/" + strconv.Itoa(thingID),
  148. contentType: fiber.MIMEApplicationJSON,
  149. body: &dto.UpdateThingRequest{
  150. Title: title,
  151. },
  152. },
  153. resCode: fiber.StatusBadRequest,
  154. resBody: []*dto.ValidateErrorResponse{
  155. {
  156. Field: "UpdateThingRequest.PlaceID",
  157. Tag: "required",
  158. },
  159. },
  160. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  161. return repoMocks.NewThingRepositoryMock(mc)
  162. },
  163. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  164. return repoMocks.NewPlaceThingRepositoryMock(mc)
  165. },
  166. },
  167. {
  168. name: "negative case - request without title",
  169. req: req{
  170. method: fiber.MethodPut,
  171. route: "/v1/things/" + strconv.Itoa(thingID),
  172. contentType: fiber.MIMEApplicationJSON,
  173. body: &dto.UpdateThingRequest{
  174. PlaceID: placeID,
  175. },
  176. },
  177. resCode: fiber.StatusBadRequest,
  178. resBody: []*dto.ValidateErrorResponse{
  179. {
  180. Field: "UpdateThingRequest.Title",
  181. Tag: "required",
  182. },
  183. },
  184. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  185. return repoMocks.NewThingRepositoryMock(mc)
  186. },
  187. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  188. return repoMocks.NewPlaceThingRepositoryMock(mc)
  189. },
  190. },
  191. {
  192. name: "negative case - repository error (get thing)",
  193. req: correctReq,
  194. resCode: fiber.StatusBadRequest,
  195. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  196. mock := repoMocks.NewThingRepositoryMock(mc)
  197. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  198. assert.Equal(mc, thingID, id)
  199. }).Return(nil, sql.ErrNoRows)
  200. return mock
  201. },
  202. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  203. return repoMocks.NewPlaceThingRepositoryMock(mc)
  204. },
  205. },
  206. {
  207. name: "negative case - repository error (get place thing)",
  208. req: correctReq,
  209. resCode: fiber.StatusBadRequest,
  210. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  211. mock := repoMocks.NewThingRepositoryMock(mc)
  212. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  213. assert.Equal(mc, thingID, id)
  214. }).Return(&repoResBeforeUpdate, nil)
  215. return mock
  216. },
  217. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  218. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  219. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  220. assert.Equal(mc, thingID, id)
  221. }).Return(nil, sql.ErrNoRows)
  222. return mock
  223. },
  224. },
  225. {
  226. name: "negative case - repository error (begin tx)",
  227. req: correctReq,
  228. resCode: fiber.StatusInternalServerError,
  229. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  230. mock := repoMocks.NewThingRepositoryMock(mc)
  231. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  232. assert.Equal(mc, thingID, id)
  233. }).Return(&repoResBeforeUpdate, nil)
  234. mock.BeginTxMock.Return(nil, testError)
  235. return mock
  236. },
  237. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  238. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  239. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  240. assert.Equal(mc, thingID, id)
  241. }).Return(&placeThingRepoResBeforeUpdate, nil)
  242. return mock
  243. },
  244. },
  245. {
  246. name: "negative case - repository error (update thing)",
  247. req: correctReq,
  248. resCode: fiber.StatusInternalServerError,
  249. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  250. mock := repoMocks.NewThingRepositoryMock(mc)
  251. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  252. assert.Equal(mc, thingID, id)
  253. }).Return(&repoResBeforeUpdate, nil)
  254. mock.BeginTxMock.Return(nil, nil)
  255. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  256. assert.Equal(mc, title, req.Title)
  257. assert.Equal(mc, description, req.Description)
  258. }).Return(testError)
  259. return mock
  260. },
  261. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  262. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  263. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  264. assert.Equal(mc, thingID, id)
  265. }).Return(&placeThingRepoResBeforeUpdate, nil)
  266. return mock
  267. },
  268. },
  269. {
  270. name: "negative case - repository error (update place)",
  271. req: correctReq,
  272. resCode: fiber.StatusInternalServerError,
  273. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  274. mock := repoMocks.NewThingRepositoryMock(mc)
  275. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  276. assert.Equal(mc, thingID, id)
  277. }).Return(&repoResBeforeUpdate, nil)
  278. mock.BeginTxMock.Return(nil, nil)
  279. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  280. assert.Equal(mc, title, req.Title)
  281. assert.Equal(mc, description, req.Description)
  282. }).Return(nil)
  283. return mock
  284. },
  285. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  286. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  287. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  288. assert.Equal(mc, thingID, id)
  289. }).Return(&placeThingRepoResBeforeUpdate, nil)
  290. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  291. assert.Equal(mc, placeID, req.PlaceID)
  292. assert.Equal(mc, thingID, req.ThingID)
  293. }).Return(testError)
  294. return mock
  295. },
  296. },
  297. {
  298. name: "negative case - repository error (commit tx)",
  299. req: correctReq,
  300. resCode: fiber.StatusInternalServerError,
  301. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  302. mock := repoMocks.NewThingRepositoryMock(mc)
  303. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  304. assert.Equal(mc, thingID, id)
  305. }).Return(&repoResBeforeUpdate, nil)
  306. mock.BeginTxMock.Return(nil, nil)
  307. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  308. assert.Equal(mc, title, req.Title)
  309. assert.Equal(mc, description, req.Description)
  310. }).Return(nil)
  311. mock.CommitTxMock.Return(testError)
  312. return mock
  313. },
  314. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  315. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  316. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  317. assert.Equal(mc, thingID, id)
  318. }).Return(&placeThingRepoResBeforeUpdate, nil)
  319. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  320. assert.Equal(mc, placeID, req.PlaceID)
  321. assert.Equal(mc, thingID, req.ThingID)
  322. }).Return(nil)
  323. return mock
  324. },
  325. },
  326. {
  327. name: "negative case - repository error (get thing)",
  328. req: correctReq,
  329. resCode: fiber.StatusInternalServerError,
  330. thingRepoMock: func(mc *minimock.Controller) interfaces.ThingRepository {
  331. mock := repoMocks.NewThingRepositoryMock(mc)
  332. mock.GetMock.Set(func(ctx context.Context, thingID int) (*models.Thing, error) {
  333. if mock.GetAfterCounter() == 0 {
  334. return &repoResBeforeUpdate, nil
  335. }
  336. return nil, sql.ErrNoRows
  337. })
  338. mock.BeginTxMock.Return(nil, nil)
  339. mock.UpdateMock.Inspect(func(ctx context.Context, req models.UpdateThingRequest, tx *sql.Tx) {
  340. assert.Equal(mc, title, req.Title)
  341. assert.Equal(mc, description, req.Description)
  342. }).Return(nil)
  343. mock.CommitTxMock.Return(nil)
  344. return mock
  345. },
  346. placeThingRepoMock: func(mc *minimock.Controller) interfaces.PlaceThingRepository {
  347. mock := repoMocks.NewPlaceThingRepositoryMock(mc)
  348. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  349. assert.Equal(mc, thingID, id)
  350. }).Return(&placeThingRepoResBeforeUpdate, nil)
  351. mock.UpdatePlaceMock.Inspect(func(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) {
  352. assert.Equal(mc, placeID, req.PlaceID)
  353. assert.Equal(mc, thingID, req.ThingID)
  354. }).Return(nil)
  355. return mock
  356. },
  357. },
  358. }
  359. for _, tt := range tests {
  360. t.Run(tt.name, func(t *testing.T) {
  361. fiberApp := fiber.New()
  362. serviceProvider := sp.InitMock(tt.thingRepoMock(mc), tt.placeThingRepoMock(mc))
  363. fiberApp.Put("/v1/things/:thingId", UpdateThingHandler(serviceProvider))
  364. fiberReq := httptest.NewRequest(tt.req.method, tt.req.route, helpers.ConvertDataToIOReader(tt.req.body))
  365. fiberReq.Header.Add(fiber.HeaderContentType, tt.req.contentType)
  366. fiberRes, _ := fiberApp.Test(fiberReq, API.DefaultTestTimeOut)
  367. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  368. if tt.resBody != nil {
  369. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  370. }
  371. })
  372. }
  373. }