delete_thing_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package thing
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "net/http/httptest"
  7. "strconv"
  8. "testing"
  9. "github.com/brianvoe/gofakeit/v6"
  10. "github.com/gofiber/fiber/v2"
  11. "github.com/gojuno/minimock/v3"
  12. "github.com/stretchr/testify/assert"
  13. API "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1"
  14. "git.dmitriygnatenko.ru/dima/homethings/internal/api/v1/thing/mocks"
  15. "git.dmitriygnatenko.ru/dima/homethings/internal/dto"
  16. "git.dmitriygnatenko.ru/dima/homethings/internal/helpers"
  17. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  18. )
  19. func TestDeleteThingHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. thingID = gofakeit.Number(1, 1000)
  27. imageID = gofakeit.Number(1, 1000)
  28. imageURL = gofakeit.URL()
  29. testError = errors.New(gofakeit.Phrase())
  30. correctReq = req{
  31. method: fiber.MethodDelete,
  32. route: "/v1/things/" + strconv.Itoa(thingID),
  33. }
  34. repoImagesRes = []models.Image{
  35. {
  36. ID: imageID,
  37. Image: imageURL,
  38. },
  39. }
  40. )
  41. tests := []struct {
  42. name string
  43. req req
  44. resCode int
  45. resBody interface{}
  46. thingRepoMock func(mc *minimock.Controller) ThingRepository
  47. placeThingRepoMock func(mc *minimock.Controller) PlaceThingRepository
  48. thingImageRepoMock func(mc *minimock.Controller) ThingImageRepository
  49. thingTagRepoMock func(mc *minimock.Controller) ThingTagRepository
  50. thingNotificationRepoMock func(mc *minimock.Controller) ThingNotificationRepository
  51. fileRepoMock func(mc *minimock.Controller) FileRepository
  52. }{
  53. {
  54. name: "negative case - bad request",
  55. req: req{
  56. method: fiber.MethodDelete,
  57. route: "/v1/things/" + gofakeit.Word(),
  58. },
  59. resCode: fiber.StatusBadRequest,
  60. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  61. return mocks.NewThingRepositoryMock(mc)
  62. },
  63. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  64. return mocks.NewPlaceThingRepositoryMock(mc)
  65. },
  66. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  67. return mocks.NewThingImageRepositoryMock(mc)
  68. },
  69. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  70. return mocks.NewThingTagRepositoryMock(mc)
  71. },
  72. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  73. return mocks.NewThingNotificationRepositoryMock(mc)
  74. },
  75. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  76. return mocks.NewFileRepositoryMock(mc)
  77. },
  78. },
  79. {
  80. name: "negative case - bad request (thing not found)",
  81. req: correctReq,
  82. resCode: fiber.StatusBadRequest,
  83. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  84. mock := mocks.NewThingRepositoryMock(mc)
  85. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  86. assert.Equal(mc, thingID, id)
  87. }).Return(nil, sql.ErrNoRows)
  88. return mock
  89. },
  90. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  91. return mocks.NewPlaceThingRepositoryMock(mc)
  92. },
  93. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  94. return mocks.NewThingImageRepositoryMock(mc)
  95. },
  96. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  97. return mocks.NewThingTagRepositoryMock(mc)
  98. },
  99. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  100. return mocks.NewThingNotificationRepositoryMock(mc)
  101. },
  102. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  103. return mocks.NewFileRepositoryMock(mc)
  104. },
  105. },
  106. {
  107. name: "negative case - repository error (get thing)",
  108. req: correctReq,
  109. resCode: fiber.StatusInternalServerError,
  110. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  111. mock := mocks.NewThingRepositoryMock(mc)
  112. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  113. assert.Equal(mc, thingID, id)
  114. }).Return(nil, testError)
  115. return mock
  116. },
  117. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  118. return mocks.NewPlaceThingRepositoryMock(mc)
  119. },
  120. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  121. return mocks.NewThingImageRepositoryMock(mc)
  122. },
  123. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  124. return mocks.NewThingTagRepositoryMock(mc)
  125. },
  126. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  127. return mocks.NewThingNotificationRepositoryMock(mc)
  128. },
  129. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  130. return mocks.NewFileRepositoryMock(mc)
  131. },
  132. },
  133. {
  134. name: "negative case - repository error (begin tx)",
  135. req: correctReq,
  136. resCode: fiber.StatusInternalServerError,
  137. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  138. mock := mocks.NewThingRepositoryMock(mc)
  139. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  140. assert.Equal(mc, thingID, id)
  141. }).Return(nil, nil)
  142. mock.BeginTxMock.Return(nil, testError)
  143. return mock
  144. },
  145. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  146. return mocks.NewPlaceThingRepositoryMock(mc)
  147. },
  148. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  149. return mocks.NewThingImageRepositoryMock(mc)
  150. },
  151. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  152. return mocks.NewThingTagRepositoryMock(mc)
  153. },
  154. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  155. return mocks.NewThingNotificationRepositoryMock(mc)
  156. },
  157. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  158. return mocks.NewFileRepositoryMock(mc)
  159. },
  160. },
  161. {
  162. name: "negative case - repository error (delete place thing)",
  163. req: correctReq,
  164. resCode: fiber.StatusInternalServerError,
  165. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  166. mock := mocks.NewThingRepositoryMock(mc)
  167. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  168. assert.Equal(mc, thingID, id)
  169. }).Return(nil, nil)
  170. mock.BeginTxMock.Return(nil, nil)
  171. return mock
  172. },
  173. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  174. mock := mocks.NewPlaceThingRepositoryMock(mc)
  175. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  176. assert.Equal(mc, thingID, id)
  177. }).Return(testError)
  178. return mock
  179. },
  180. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  181. return mocks.NewThingImageRepositoryMock(mc)
  182. },
  183. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  184. return mocks.NewThingTagRepositoryMock(mc)
  185. },
  186. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  187. return mocks.NewThingNotificationRepositoryMock(mc)
  188. },
  189. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  190. return mocks.NewFileRepositoryMock(mc)
  191. },
  192. },
  193. {
  194. name: "negative case - repository error (get images)",
  195. req: correctReq,
  196. resCode: fiber.StatusInternalServerError,
  197. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  198. mock := mocks.NewThingRepositoryMock(mc)
  199. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  200. assert.Equal(mc, thingID, id)
  201. }).Return(nil, nil)
  202. mock.BeginTxMock.Return(nil, nil)
  203. return mock
  204. },
  205. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  206. mock := mocks.NewPlaceThingRepositoryMock(mc)
  207. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  208. assert.Equal(mc, thingID, id)
  209. }).Return(nil)
  210. return mock
  211. },
  212. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  213. mock := mocks.NewThingImageRepositoryMock(mc)
  214. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  215. assert.Equal(mc, thingID, id)
  216. }).Return(nil, testError)
  217. return mock
  218. },
  219. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  220. return mocks.NewThingTagRepositoryMock(mc)
  221. },
  222. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  223. return mocks.NewThingNotificationRepositoryMock(mc)
  224. },
  225. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  226. return mocks.NewFileRepositoryMock(mc)
  227. },
  228. },
  229. {
  230. name: "negative case - repository error (delete images)",
  231. req: correctReq,
  232. resCode: fiber.StatusInternalServerError,
  233. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  234. mock := mocks.NewThingRepositoryMock(mc)
  235. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  236. assert.Equal(mc, thingID, id)
  237. }).Return(nil, nil)
  238. mock.BeginTxMock.Return(nil, nil)
  239. return mock
  240. },
  241. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  242. mock := mocks.NewPlaceThingRepositoryMock(mc)
  243. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  244. assert.Equal(mc, thingID, id)
  245. }).Return(nil)
  246. return mock
  247. },
  248. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  249. mock := mocks.NewThingImageRepositoryMock(mc)
  250. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  251. assert.Equal(mc, thingID, id)
  252. }).Return(repoImagesRes, nil)
  253. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  254. assert.Equal(mc, imageID, id)
  255. }).Return(testError)
  256. return mock
  257. },
  258. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  259. return mocks.NewThingTagRepositoryMock(mc)
  260. },
  261. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  262. return mocks.NewThingNotificationRepositoryMock(mc)
  263. },
  264. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  265. return mocks.NewFileRepositoryMock(mc)
  266. },
  267. },
  268. {
  269. name: "negative case - repository error (delete thing tags)",
  270. req: correctReq,
  271. resCode: fiber.StatusInternalServerError,
  272. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  273. mock := mocks.NewThingRepositoryMock(mc)
  274. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  275. assert.Equal(mc, thingID, id)
  276. }).Return(nil, nil)
  277. mock.BeginTxMock.Return(nil, nil)
  278. return mock
  279. },
  280. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  281. mock := mocks.NewPlaceThingRepositoryMock(mc)
  282. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  283. assert.Equal(mc, thingID, id)
  284. }).Return(nil)
  285. return mock
  286. },
  287. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  288. mock := mocks.NewThingImageRepositoryMock(mc)
  289. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  290. assert.Equal(mc, thingID, id)
  291. }).Return(repoImagesRes, nil)
  292. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  293. assert.Equal(mc, imageID, id)
  294. }).Return(nil)
  295. return mock
  296. },
  297. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  298. mock := mocks.NewThingTagRepositoryMock(mc)
  299. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  300. assert.Equal(mc, thingID, id)
  301. }).Return(testError)
  302. return mock
  303. },
  304. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  305. return mocks.NewThingNotificationRepositoryMock(mc)
  306. },
  307. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  308. return mocks.NewFileRepositoryMock(mc)
  309. },
  310. },
  311. {
  312. name: "negative case - repository error (delete notification)",
  313. req: correctReq,
  314. resCode: fiber.StatusInternalServerError,
  315. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  316. mock := mocks.NewThingRepositoryMock(mc)
  317. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  318. assert.Equal(mc, thingID, id)
  319. }).Return(nil, nil)
  320. mock.BeginTxMock.Return(nil, nil)
  321. return mock
  322. },
  323. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  324. mock := mocks.NewPlaceThingRepositoryMock(mc)
  325. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  326. assert.Equal(mc, thingID, id)
  327. }).Return(nil)
  328. return mock
  329. },
  330. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  331. mock := mocks.NewThingImageRepositoryMock(mc)
  332. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  333. assert.Equal(mc, thingID, id)
  334. }).Return(repoImagesRes, nil)
  335. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  336. assert.Equal(mc, imageID, id)
  337. }).Return(nil)
  338. return mock
  339. },
  340. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  341. mock := mocks.NewThingTagRepositoryMock(mc)
  342. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  343. assert.Equal(mc, thingID, id)
  344. }).Return(nil)
  345. return mock
  346. },
  347. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  348. mock := mocks.NewThingNotificationRepositoryMock(mc)
  349. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  350. assert.Equal(mc, thingID, id)
  351. }).Return(testError)
  352. return mock
  353. },
  354. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  355. return mocks.NewFileRepositoryMock(mc)
  356. },
  357. },
  358. {
  359. name: "negative case - repository error (delete thing)",
  360. req: correctReq,
  361. resCode: fiber.StatusInternalServerError,
  362. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  363. mock := mocks.NewThingRepositoryMock(mc)
  364. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  365. assert.Equal(mc, thingID, id)
  366. }).Return(nil, nil)
  367. mock.BeginTxMock.Return(nil, nil)
  368. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  369. assert.Equal(mc, thingID, id)
  370. }).Return(testError)
  371. return mock
  372. },
  373. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  374. mock := mocks.NewPlaceThingRepositoryMock(mc)
  375. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  376. assert.Equal(mc, thingID, id)
  377. }).Return(nil)
  378. return mock
  379. },
  380. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  381. mock := mocks.NewThingImageRepositoryMock(mc)
  382. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  383. assert.Equal(mc, thingID, id)
  384. }).Return(repoImagesRes, nil)
  385. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  386. assert.Equal(mc, imageID, id)
  387. }).Return(nil)
  388. return mock
  389. },
  390. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  391. mock := mocks.NewThingTagRepositoryMock(mc)
  392. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  393. assert.Equal(mc, thingID, id)
  394. }).Return(nil)
  395. return mock
  396. },
  397. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  398. mock := mocks.NewThingNotificationRepositoryMock(mc)
  399. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  400. assert.Equal(mc, thingID, id)
  401. }).Return(nil)
  402. return mock
  403. },
  404. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  405. return mocks.NewFileRepositoryMock(mc)
  406. },
  407. },
  408. {
  409. name: "negative case - repository error (commit tx)",
  410. req: correctReq,
  411. resCode: fiber.StatusInternalServerError,
  412. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  413. mock := mocks.NewThingRepositoryMock(mc)
  414. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  415. assert.Equal(mc, thingID, id)
  416. }).Return(nil, nil)
  417. mock.BeginTxMock.Return(nil, nil)
  418. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  419. assert.Equal(mc, thingID, id)
  420. }).Return(nil)
  421. mock.CommitTxMock.Return(testError)
  422. return mock
  423. },
  424. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  425. mock := mocks.NewPlaceThingRepositoryMock(mc)
  426. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  427. assert.Equal(mc, thingID, id)
  428. }).Return(nil)
  429. return mock
  430. },
  431. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  432. mock := mocks.NewThingImageRepositoryMock(mc)
  433. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  434. assert.Equal(mc, thingID, id)
  435. }).Return(repoImagesRes, nil)
  436. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  437. assert.Equal(mc, imageID, id)
  438. }).Return(nil)
  439. return mock
  440. },
  441. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  442. mock := mocks.NewThingTagRepositoryMock(mc)
  443. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  444. assert.Equal(mc, thingID, id)
  445. }).Return(nil)
  446. return mock
  447. },
  448. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  449. mock := mocks.NewThingNotificationRepositoryMock(mc)
  450. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  451. assert.Equal(mc, thingID, id)
  452. }).Return(nil)
  453. return mock
  454. },
  455. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  456. return mocks.NewFileRepositoryMock(mc)
  457. },
  458. },
  459. {
  460. name: "negative case - file delete error",
  461. req: correctReq,
  462. resCode: fiber.StatusInternalServerError,
  463. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  464. mock := mocks.NewThingRepositoryMock(mc)
  465. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  466. assert.Equal(mc, thingID, id)
  467. }).Return(nil, nil)
  468. mock.BeginTxMock.Return(nil, nil)
  469. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  470. assert.Equal(mc, thingID, id)
  471. }).Return(nil)
  472. mock.CommitTxMock.Return(nil)
  473. return mock
  474. },
  475. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  476. mock := mocks.NewPlaceThingRepositoryMock(mc)
  477. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  478. assert.Equal(mc, thingID, id)
  479. }).Return(nil)
  480. return mock
  481. },
  482. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  483. mock := mocks.NewThingImageRepositoryMock(mc)
  484. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  485. assert.Equal(mc, thingID, id)
  486. }).Return(repoImagesRes, nil)
  487. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  488. assert.Equal(mc, imageID, id)
  489. }).Return(nil)
  490. return mock
  491. },
  492. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  493. mock := mocks.NewThingTagRepositoryMock(mc)
  494. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  495. assert.Equal(mc, thingID, id)
  496. }).Return(nil)
  497. return mock
  498. },
  499. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  500. mock := mocks.NewThingNotificationRepositoryMock(mc)
  501. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  502. assert.Equal(mc, thingID, id)
  503. }).Return(nil)
  504. return mock
  505. },
  506. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  507. mock := mocks.NewFileRepositoryMock(mc)
  508. mock.DeleteMock.Return(testError)
  509. return mock
  510. },
  511. },
  512. {
  513. name: "positive case",
  514. req: correctReq,
  515. resCode: fiber.StatusOK,
  516. resBody: dto.EmptyResponse{},
  517. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  518. mock := mocks.NewThingRepositoryMock(mc)
  519. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  520. assert.Equal(mc, thingID, id)
  521. }).Return(nil, nil)
  522. mock.BeginTxMock.Return(nil, nil)
  523. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  524. assert.Equal(mc, thingID, id)
  525. }).Return(nil)
  526. mock.CommitTxMock.Return(nil)
  527. return mock
  528. },
  529. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  530. mock := mocks.NewPlaceThingRepositoryMock(mc)
  531. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  532. assert.Equal(mc, thingID, id)
  533. }).Return(nil)
  534. return mock
  535. },
  536. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  537. mock := mocks.NewThingImageRepositoryMock(mc)
  538. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  539. assert.Equal(mc, thingID, id)
  540. }).Return(repoImagesRes, nil)
  541. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  542. assert.Equal(mc, imageID, id)
  543. }).Return(nil)
  544. return mock
  545. },
  546. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  547. mock := mocks.NewThingTagRepositoryMock(mc)
  548. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  549. assert.Equal(mc, thingID, id)
  550. }).Return(nil)
  551. return mock
  552. },
  553. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  554. mock := mocks.NewThingNotificationRepositoryMock(mc)
  555. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  556. assert.Equal(mc, thingID, id)
  557. }).Return(nil)
  558. return mock
  559. },
  560. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  561. mock := mocks.NewFileRepositoryMock(mc)
  562. mock.DeleteMock.Return(nil)
  563. return mock
  564. },
  565. },
  566. }
  567. for _, tt := range tests {
  568. t.Run(tt.name, func(t *testing.T) {
  569. t.Parallel()
  570. mc := minimock.NewController(t)
  571. fiberApp := fiber.New()
  572. fiberApp.Delete("/v1/things/:thingId", DeleteThingHandler(
  573. tt.thingRepoMock(mc),
  574. tt.thingTagRepoMock(mc),
  575. tt.placeThingRepoMock(mc),
  576. tt.thingImageRepoMock(mc),
  577. tt.thingNotificationRepoMock(mc),
  578. tt.fileRepoMock(mc),
  579. ))
  580. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  581. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  582. if tt.resBody != nil {
  583. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  584. }
  585. })
  586. }
  587. }