delete_thing_test.go 22 KB

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