delete_thing_test.go 23 KB

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