delete_thing_test.go 20 KB

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