delete_place_test.go 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. package place
  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/place/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 TestDeletePlaceHandler(t *testing.T) {
  20. t.Parallel()
  21. type req struct {
  22. method string
  23. route string
  24. }
  25. var (
  26. placeID = gofakeit.Number(1, 1000)
  27. thingID = gofakeit.Number(1, 1000)
  28. placeImageID = gofakeit.Number(1, 1000)
  29. placeImageURL = gofakeit.URL()
  30. thingImageID = gofakeit.Number(1, 1000)
  31. thingImageURL = gofakeit.URL()
  32. testError = errors.New(gofakeit.Phrase())
  33. correctReq = req{
  34. method: fiber.MethodDelete,
  35. route: "/v1/places/" + strconv.Itoa(placeID),
  36. }
  37. thingRepoRes = []models.Thing{
  38. {
  39. ID: thingID,
  40. PlaceID: placeID,
  41. },
  42. }
  43. placeImageRepoRes = []models.Image{
  44. {
  45. ID: placeImageID,
  46. Image: placeImageURL,
  47. },
  48. }
  49. thingImageRepoRes = []models.Image{
  50. {
  51. ID: thingImageID,
  52. Image: thingImageURL,
  53. },
  54. }
  55. )
  56. tests := []struct {
  57. name string
  58. req req
  59. resCode int
  60. resBody interface{}
  61. placeRepoMock func(mc *minimock.Controller) PlaceRepository
  62. thingRepoMock func(mc *minimock.Controller) ThingRepository
  63. placeThingRepoMock func(mc *minimock.Controller) PlaceThingRepository
  64. thingImageRepoMock func(mc *minimock.Controller) ThingImageRepository
  65. placeImageRepoMock func(mc *minimock.Controller) PlaceImageRepository
  66. thingTagRepoMock func(mc *minimock.Controller) ThingTagRepository
  67. thingNotificationRepoMock func(mc *minimock.Controller) ThingNotificationRepository
  68. fileRepoMock func(mc *minimock.Controller) FileRepository
  69. }{
  70. {
  71. name: "negative case - bad request",
  72. req: req{
  73. method: fiber.MethodDelete,
  74. route: "/v1/places/" + gofakeit.Word(),
  75. },
  76. resCode: fiber.StatusBadRequest,
  77. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  78. return mocks.NewPlaceRepositoryMock(mc)
  79. },
  80. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  81. return mocks.NewThingRepositoryMock(mc)
  82. },
  83. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  84. return mocks.NewPlaceThingRepositoryMock(mc)
  85. },
  86. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  87. return mocks.NewThingImageRepositoryMock(mc)
  88. },
  89. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  90. return mocks.NewPlaceImageRepositoryMock(mc)
  91. },
  92. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  93. return mocks.NewThingTagRepositoryMock(mc)
  94. },
  95. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  96. return mocks.NewThingNotificationRepositoryMock(mc)
  97. },
  98. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  99. return mocks.NewFileRepositoryMock(mc)
  100. },
  101. },
  102. {
  103. name: "negative case - bad request (place not found)",
  104. req: correctReq,
  105. resCode: fiber.StatusBadRequest,
  106. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  107. mock := mocks.NewPlaceRepositoryMock(mc)
  108. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  109. assert.Equal(mc, placeID, id)
  110. }).Return(nil, sql.ErrNoRows)
  111. return mock
  112. },
  113. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  114. return mocks.NewThingRepositoryMock(mc)
  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. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  123. return mocks.NewPlaceImageRepositoryMock(mc)
  124. },
  125. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  126. return mocks.NewThingTagRepositoryMock(mc)
  127. },
  128. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  129. return mocks.NewThingNotificationRepositoryMock(mc)
  130. },
  131. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  132. return mocks.NewFileRepositoryMock(mc)
  133. },
  134. },
  135. {
  136. name: "negative case - repository error (get place)",
  137. req: correctReq,
  138. resCode: fiber.StatusInternalServerError,
  139. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  140. mock := mocks.NewPlaceRepositoryMock(mc)
  141. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  142. assert.Equal(mc, placeID, id)
  143. }).Return(nil, testError)
  144. return mock
  145. },
  146. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  147. return mocks.NewThingRepositoryMock(mc)
  148. },
  149. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  150. return mocks.NewPlaceThingRepositoryMock(mc)
  151. },
  152. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  153. return mocks.NewThingImageRepositoryMock(mc)
  154. },
  155. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  156. return mocks.NewPlaceImageRepositoryMock(mc)
  157. },
  158. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  159. return mocks.NewThingTagRepositoryMock(mc)
  160. },
  161. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  162. return mocks.NewThingNotificationRepositoryMock(mc)
  163. },
  164. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  165. return mocks.NewFileRepositoryMock(mc)
  166. },
  167. },
  168. {
  169. name: "negative case - repository error (get nested places)",
  170. req: correctReq,
  171. resCode: fiber.StatusInternalServerError,
  172. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  173. mock := mocks.NewPlaceRepositoryMock(mc)
  174. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  175. assert.Equal(mc, placeID, id)
  176. }).Return(nil, nil)
  177. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  178. assert.Equal(mc, placeID, id)
  179. }).Return(nil, testError)
  180. return mock
  181. },
  182. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  183. return mocks.NewThingRepositoryMock(mc)
  184. },
  185. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  186. return mocks.NewPlaceThingRepositoryMock(mc)
  187. },
  188. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  189. return mocks.NewThingImageRepositoryMock(mc)
  190. },
  191. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  192. return mocks.NewPlaceImageRepositoryMock(mc)
  193. },
  194. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  195. return mocks.NewThingTagRepositoryMock(mc)
  196. },
  197. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  198. return mocks.NewThingNotificationRepositoryMock(mc)
  199. },
  200. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  201. return mocks.NewFileRepositoryMock(mc)
  202. },
  203. },
  204. {
  205. name: "negative case - bad request (nested places exists)",
  206. req: correctReq,
  207. resCode: fiber.StatusBadRequest,
  208. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  209. mock := mocks.NewPlaceRepositoryMock(mc)
  210. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  211. assert.Equal(mc, placeID, id)
  212. }).Return(nil, nil)
  213. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  214. assert.Equal(mc, placeID, id)
  215. }).Return([]models.Place{{}}, nil)
  216. return mock
  217. },
  218. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  219. return mocks.NewThingRepositoryMock(mc)
  220. },
  221. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  222. return mocks.NewPlaceThingRepositoryMock(mc)
  223. },
  224. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  225. return mocks.NewThingImageRepositoryMock(mc)
  226. },
  227. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  228. return mocks.NewPlaceImageRepositoryMock(mc)
  229. },
  230. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  231. return mocks.NewThingTagRepositoryMock(mc)
  232. },
  233. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  234. return mocks.NewThingNotificationRepositoryMock(mc)
  235. },
  236. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  237. return mocks.NewFileRepositoryMock(mc)
  238. },
  239. },
  240. {
  241. name: "negative case - repository error (get place images)",
  242. req: correctReq,
  243. resCode: fiber.StatusInternalServerError,
  244. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  245. mock := mocks.NewPlaceRepositoryMock(mc)
  246. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  247. assert.Equal(mc, placeID, id)
  248. }).Return(nil, nil)
  249. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  250. assert.Equal(mc, placeID, id)
  251. }).Return(nil, nil)
  252. return mock
  253. },
  254. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  255. return mocks.NewThingRepositoryMock(mc)
  256. },
  257. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  258. return mocks.NewPlaceThingRepositoryMock(mc)
  259. },
  260. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  261. return mocks.NewThingImageRepositoryMock(mc)
  262. },
  263. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  264. mock := mocks.NewPlaceImageRepositoryMock(mc)
  265. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  266. assert.Equal(mc, placeID, id)
  267. }).Return(nil, testError)
  268. return mock
  269. },
  270. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  271. return mocks.NewThingTagRepositoryMock(mc)
  272. },
  273. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  274. return mocks.NewThingNotificationRepositoryMock(mc)
  275. },
  276. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  277. return mocks.NewFileRepositoryMock(mc)
  278. },
  279. },
  280. {
  281. name: "negative case - repository error (get things)",
  282. req: correctReq,
  283. resCode: fiber.StatusInternalServerError,
  284. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  285. mock := mocks.NewPlaceRepositoryMock(mc)
  286. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  287. assert.Equal(mc, placeID, id)
  288. }).Return(nil, nil)
  289. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  290. assert.Equal(mc, placeID, id)
  291. }).Return(nil, nil)
  292. return mock
  293. },
  294. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  295. mock := mocks.NewThingRepositoryMock(mc)
  296. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  297. assert.Equal(mc, placeID, id)
  298. }).Return(nil, testError)
  299. return mock
  300. },
  301. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  302. return mocks.NewPlaceThingRepositoryMock(mc)
  303. },
  304. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  305. return mocks.NewThingImageRepositoryMock(mc)
  306. },
  307. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  308. mock := mocks.NewPlaceImageRepositoryMock(mc)
  309. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  310. assert.Equal(mc, placeID, id)
  311. }).Return(nil, nil)
  312. return mock
  313. },
  314. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  315. return mocks.NewThingTagRepositoryMock(mc)
  316. },
  317. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  318. return mocks.NewThingNotificationRepositoryMock(mc)
  319. },
  320. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  321. return mocks.NewFileRepositoryMock(mc)
  322. },
  323. },
  324. {
  325. name: "negative case - repository error (get things images)",
  326. req: correctReq,
  327. resCode: fiber.StatusInternalServerError,
  328. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  329. mock := mocks.NewPlaceRepositoryMock(mc)
  330. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  331. assert.Equal(mc, placeID, id)
  332. }).Return(nil, nil)
  333. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  334. assert.Equal(mc, placeID, id)
  335. }).Return(nil, nil)
  336. return mock
  337. },
  338. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  339. mock := mocks.NewThingRepositoryMock(mc)
  340. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  341. assert.Equal(mc, placeID, id)
  342. }).Return(thingRepoRes, nil)
  343. return mock
  344. },
  345. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  346. return mocks.NewPlaceThingRepositoryMock(mc)
  347. },
  348. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  349. mock := mocks.NewThingImageRepositoryMock(mc)
  350. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  351. assert.Equal(mc, thingID, id)
  352. }).Return(nil, testError)
  353. return mock
  354. },
  355. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  356. mock := mocks.NewPlaceImageRepositoryMock(mc)
  357. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  358. assert.Equal(mc, placeID, id)
  359. }).Return(nil, nil)
  360. return mock
  361. },
  362. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  363. return mocks.NewThingTagRepositoryMock(mc)
  364. },
  365. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  366. return mocks.NewThingNotificationRepositoryMock(mc)
  367. },
  368. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  369. return mocks.NewFileRepositoryMock(mc)
  370. },
  371. },
  372. {
  373. name: "negative case - repository error (begin tx)",
  374. req: correctReq,
  375. resCode: fiber.StatusInternalServerError,
  376. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  377. mock := mocks.NewPlaceRepositoryMock(mc)
  378. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  379. assert.Equal(mc, placeID, id)
  380. }).Return(nil, nil)
  381. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  382. assert.Equal(mc, placeID, id)
  383. }).Return(nil, nil)
  384. mock.BeginTxMock.Return(nil, testError)
  385. return mock
  386. },
  387. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  388. mock := mocks.NewThingRepositoryMock(mc)
  389. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  390. assert.Equal(mc, placeID, id)
  391. }).Return(thingRepoRes, nil)
  392. return mock
  393. },
  394. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  395. return mocks.NewPlaceThingRepositoryMock(mc)
  396. },
  397. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  398. mock := mocks.NewThingImageRepositoryMock(mc)
  399. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  400. assert.Equal(mc, thingID, id)
  401. }).Return(thingImageRepoRes, nil)
  402. return mock
  403. },
  404. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  405. mock := mocks.NewPlaceImageRepositoryMock(mc)
  406. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  407. assert.Equal(mc, placeID, id)
  408. }).Return(nil, nil)
  409. return mock
  410. },
  411. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  412. return mocks.NewThingTagRepositoryMock(mc)
  413. },
  414. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  415. return mocks.NewThingNotificationRepositoryMock(mc)
  416. },
  417. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  418. return mocks.NewFileRepositoryMock(mc)
  419. },
  420. },
  421. {
  422. name: "negative case - repository error (delete place image)",
  423. req: correctReq,
  424. resCode: fiber.StatusInternalServerError,
  425. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  426. mock := mocks.NewPlaceRepositoryMock(mc)
  427. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  428. assert.Equal(mc, placeID, id)
  429. }).Return(nil, nil)
  430. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  431. assert.Equal(mc, placeID, id)
  432. }).Return(nil, nil)
  433. mock.BeginTxMock.Return(nil, nil)
  434. return mock
  435. },
  436. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  437. mock := mocks.NewThingRepositoryMock(mc)
  438. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  439. assert.Equal(mc, placeID, id)
  440. }).Return(thingRepoRes, nil)
  441. return mock
  442. },
  443. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  444. return mocks.NewPlaceThingRepositoryMock(mc)
  445. },
  446. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  447. mock := mocks.NewThingImageRepositoryMock(mc)
  448. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  449. assert.Equal(mc, thingID, id)
  450. }).Return(thingImageRepoRes, nil)
  451. return mock
  452. },
  453. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  454. mock := mocks.NewPlaceImageRepositoryMock(mc)
  455. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  456. assert.Equal(mc, placeID, id)
  457. }).Return(placeImageRepoRes, nil)
  458. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  459. assert.Equal(mc, placeImageID, id)
  460. }).Return(testError)
  461. return mock
  462. },
  463. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  464. return mocks.NewThingTagRepositoryMock(mc)
  465. },
  466. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  467. return mocks.NewThingNotificationRepositoryMock(mc)
  468. },
  469. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  470. return mocks.NewFileRepositoryMock(mc)
  471. },
  472. },
  473. {
  474. name: "negative case - repository error (delete thing image)",
  475. req: correctReq,
  476. resCode: fiber.StatusInternalServerError,
  477. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  478. mock := mocks.NewPlaceRepositoryMock(mc)
  479. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  480. assert.Equal(mc, placeID, id)
  481. }).Return(nil, nil)
  482. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  483. assert.Equal(mc, placeID, id)
  484. }).Return(nil, nil)
  485. mock.BeginTxMock.Return(nil, nil)
  486. return mock
  487. },
  488. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  489. mock := mocks.NewThingRepositoryMock(mc)
  490. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  491. assert.Equal(mc, placeID, id)
  492. }).Return(thingRepoRes, nil)
  493. return mock
  494. },
  495. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  496. return mocks.NewPlaceThingRepositoryMock(mc)
  497. },
  498. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  499. mock := mocks.NewThingImageRepositoryMock(mc)
  500. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  501. assert.Equal(mc, thingID, id)
  502. }).Return(thingImageRepoRes, nil)
  503. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  504. assert.Equal(mc, thingImageID, id)
  505. }).Return(testError)
  506. return mock
  507. },
  508. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  509. mock := mocks.NewPlaceImageRepositoryMock(mc)
  510. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  511. assert.Equal(mc, placeID, id)
  512. }).Return(placeImageRepoRes, nil)
  513. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  514. assert.Equal(mc, placeImageID, id)
  515. }).Return(nil)
  516. return mock
  517. },
  518. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  519. return mocks.NewThingTagRepositoryMock(mc)
  520. },
  521. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  522. return mocks.NewThingNotificationRepositoryMock(mc)
  523. },
  524. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  525. return mocks.NewFileRepositoryMock(mc)
  526. },
  527. },
  528. {
  529. name: "negative case - repository error (delete place thing)",
  530. req: correctReq,
  531. resCode: fiber.StatusInternalServerError,
  532. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  533. mock := mocks.NewPlaceRepositoryMock(mc)
  534. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  535. assert.Equal(mc, placeID, id)
  536. }).Return(nil, nil)
  537. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  538. assert.Equal(mc, placeID, id)
  539. }).Return(nil, nil)
  540. mock.BeginTxMock.Return(nil, nil)
  541. return mock
  542. },
  543. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  544. mock := mocks.NewThingRepositoryMock(mc)
  545. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  546. assert.Equal(mc, placeID, id)
  547. }).Return(thingRepoRes, nil)
  548. return mock
  549. },
  550. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  551. mock := mocks.NewPlaceThingRepositoryMock(mc)
  552. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  553. assert.Equal(mc, thingID, id)
  554. }).Return(testError)
  555. return mock
  556. },
  557. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  558. mock := mocks.NewThingImageRepositoryMock(mc)
  559. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  560. assert.Equal(mc, thingID, id)
  561. }).Return(thingImageRepoRes, nil)
  562. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  563. assert.Equal(mc, thingImageID, id)
  564. }).Return(nil)
  565. return mock
  566. },
  567. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  568. mock := mocks.NewPlaceImageRepositoryMock(mc)
  569. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  570. assert.Equal(mc, placeID, id)
  571. }).Return(placeImageRepoRes, nil)
  572. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  573. assert.Equal(mc, placeImageID, id)
  574. }).Return(nil)
  575. return mock
  576. },
  577. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  578. return mocks.NewThingTagRepositoryMock(mc)
  579. },
  580. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  581. return mocks.NewThingNotificationRepositoryMock(mc)
  582. },
  583. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  584. return mocks.NewFileRepositoryMock(mc)
  585. },
  586. },
  587. {
  588. name: "negative case - repository error (delete thing tags)",
  589. req: correctReq,
  590. resCode: fiber.StatusInternalServerError,
  591. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  592. mock := mocks.NewPlaceRepositoryMock(mc)
  593. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  594. assert.Equal(mc, placeID, id)
  595. }).Return(nil, nil)
  596. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  597. assert.Equal(mc, placeID, id)
  598. }).Return(nil, nil)
  599. mock.BeginTxMock.Return(nil, nil)
  600. return mock
  601. },
  602. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  603. mock := mocks.NewThingRepositoryMock(mc)
  604. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  605. assert.Equal(mc, placeID, id)
  606. }).Return(thingRepoRes, nil)
  607. return mock
  608. },
  609. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  610. mock := mocks.NewPlaceThingRepositoryMock(mc)
  611. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  612. assert.Equal(mc, thingID, id)
  613. }).Return(nil)
  614. return mock
  615. },
  616. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  617. mock := mocks.NewThingImageRepositoryMock(mc)
  618. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  619. assert.Equal(mc, thingID, id)
  620. }).Return(thingImageRepoRes, nil)
  621. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  622. assert.Equal(mc, thingImageID, id)
  623. }).Return(nil)
  624. return mock
  625. },
  626. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  627. mock := mocks.NewPlaceImageRepositoryMock(mc)
  628. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  629. assert.Equal(mc, placeID, id)
  630. }).Return(placeImageRepoRes, nil)
  631. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  632. assert.Equal(mc, placeImageID, id)
  633. }).Return(nil)
  634. return mock
  635. },
  636. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  637. mock := mocks.NewThingTagRepositoryMock(mc)
  638. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  639. assert.Equal(mc, thingID, id)
  640. }).Return(testError)
  641. return mock
  642. },
  643. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  644. return mocks.NewThingNotificationRepositoryMock(mc)
  645. },
  646. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  647. return mocks.NewFileRepositoryMock(mc)
  648. },
  649. },
  650. {
  651. name: "negative case - repository error (delete thing notifications)",
  652. req: correctReq,
  653. resCode: fiber.StatusInternalServerError,
  654. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  655. mock := mocks.NewPlaceRepositoryMock(mc)
  656. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  657. assert.Equal(mc, placeID, id)
  658. }).Return(nil, nil)
  659. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  660. assert.Equal(mc, placeID, id)
  661. }).Return(nil, nil)
  662. mock.BeginTxMock.Return(nil, nil)
  663. return mock
  664. },
  665. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  666. mock := mocks.NewThingRepositoryMock(mc)
  667. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  668. assert.Equal(mc, placeID, id)
  669. }).Return(thingRepoRes, nil)
  670. return mock
  671. },
  672. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  673. mock := mocks.NewPlaceThingRepositoryMock(mc)
  674. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  675. assert.Equal(mc, thingID, id)
  676. }).Return(nil)
  677. return mock
  678. },
  679. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  680. mock := mocks.NewThingImageRepositoryMock(mc)
  681. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  682. assert.Equal(mc, thingID, id)
  683. }).Return(thingImageRepoRes, nil)
  684. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  685. assert.Equal(mc, thingImageID, id)
  686. }).Return(nil)
  687. return mock
  688. },
  689. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  690. mock := mocks.NewPlaceImageRepositoryMock(mc)
  691. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  692. assert.Equal(mc, placeID, id)
  693. }).Return(placeImageRepoRes, nil)
  694. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  695. assert.Equal(mc, placeImageID, id)
  696. }).Return(nil)
  697. return mock
  698. },
  699. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  700. mock := mocks.NewThingTagRepositoryMock(mc)
  701. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  702. assert.Equal(mc, thingID, id)
  703. }).Return(nil)
  704. return mock
  705. },
  706. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  707. mock := mocks.NewThingNotificationRepositoryMock(mc)
  708. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  709. assert.Equal(mc, thingID, id)
  710. }).Return(testError)
  711. return mock
  712. },
  713. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  714. return mocks.NewFileRepositoryMock(mc)
  715. },
  716. },
  717. {
  718. name: "negative case - repository error (delete thing)",
  719. req: correctReq,
  720. resCode: fiber.StatusInternalServerError,
  721. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  722. mock := mocks.NewPlaceRepositoryMock(mc)
  723. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  724. assert.Equal(mc, placeID, id)
  725. }).Return(nil, nil)
  726. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  727. assert.Equal(mc, placeID, id)
  728. }).Return(nil, nil)
  729. mock.BeginTxMock.Return(nil, nil)
  730. return mock
  731. },
  732. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  733. mock := mocks.NewThingRepositoryMock(mc)
  734. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  735. assert.Equal(mc, placeID, id)
  736. }).Return(thingRepoRes, nil)
  737. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  738. assert.Equal(mc, thingID, id)
  739. }).Return(testError)
  740. return mock
  741. },
  742. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  743. mock := mocks.NewPlaceThingRepositoryMock(mc)
  744. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  745. assert.Equal(mc, thingID, id)
  746. }).Return(nil)
  747. return mock
  748. },
  749. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  750. mock := mocks.NewThingImageRepositoryMock(mc)
  751. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  752. assert.Equal(mc, thingID, id)
  753. }).Return(thingImageRepoRes, nil)
  754. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  755. assert.Equal(mc, thingImageID, id)
  756. }).Return(nil)
  757. return mock
  758. },
  759. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  760. mock := mocks.NewPlaceImageRepositoryMock(mc)
  761. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  762. assert.Equal(mc, placeID, id)
  763. }).Return(placeImageRepoRes, nil)
  764. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  765. assert.Equal(mc, placeImageID, id)
  766. }).Return(nil)
  767. return mock
  768. },
  769. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  770. mock := mocks.NewThingTagRepositoryMock(mc)
  771. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  772. assert.Equal(mc, thingID, id)
  773. }).Return(nil)
  774. return mock
  775. },
  776. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  777. mock := mocks.NewThingNotificationRepositoryMock(mc)
  778. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  779. assert.Equal(mc, thingID, id)
  780. }).Return(nil)
  781. return mock
  782. },
  783. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  784. return mocks.NewFileRepositoryMock(mc)
  785. },
  786. },
  787. {
  788. name: "negative case - repository error (delete place)",
  789. req: correctReq,
  790. resCode: fiber.StatusInternalServerError,
  791. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  792. mock := mocks.NewPlaceRepositoryMock(mc)
  793. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  794. assert.Equal(mc, placeID, id)
  795. }).Return(nil, nil)
  796. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  797. assert.Equal(mc, placeID, id)
  798. }).Return(nil, nil)
  799. mock.BeginTxMock.Return(nil, nil)
  800. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  801. assert.Equal(mc, placeID, id)
  802. }).Return(testError)
  803. return mock
  804. },
  805. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  806. mock := mocks.NewThingRepositoryMock(mc)
  807. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  808. assert.Equal(mc, placeID, id)
  809. }).Return(thingRepoRes, nil)
  810. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  811. assert.Equal(mc, thingID, id)
  812. }).Return(nil)
  813. return mock
  814. },
  815. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  816. mock := mocks.NewPlaceThingRepositoryMock(mc)
  817. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  818. assert.Equal(mc, thingID, id)
  819. }).Return(nil)
  820. return mock
  821. },
  822. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  823. mock := mocks.NewThingImageRepositoryMock(mc)
  824. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  825. assert.Equal(mc, thingID, id)
  826. }).Return(thingImageRepoRes, nil)
  827. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  828. assert.Equal(mc, thingImageID, id)
  829. }).Return(nil)
  830. return mock
  831. },
  832. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  833. mock := mocks.NewPlaceImageRepositoryMock(mc)
  834. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  835. assert.Equal(mc, placeID, id)
  836. }).Return(placeImageRepoRes, nil)
  837. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  838. assert.Equal(mc, placeImageID, id)
  839. }).Return(nil)
  840. return mock
  841. },
  842. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  843. mock := mocks.NewThingTagRepositoryMock(mc)
  844. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  845. assert.Equal(mc, thingID, id)
  846. }).Return(nil)
  847. return mock
  848. },
  849. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  850. mock := mocks.NewThingNotificationRepositoryMock(mc)
  851. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  852. assert.Equal(mc, thingID, id)
  853. }).Return(nil)
  854. return mock
  855. },
  856. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  857. return mocks.NewFileRepositoryMock(mc)
  858. },
  859. },
  860. {
  861. name: "negative case - repository error (commit tx)",
  862. req: correctReq,
  863. resCode: fiber.StatusInternalServerError,
  864. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  865. mock := mocks.NewPlaceRepositoryMock(mc)
  866. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  867. assert.Equal(mc, placeID, id)
  868. }).Return(nil, nil)
  869. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  870. assert.Equal(mc, placeID, id)
  871. }).Return(nil, nil)
  872. mock.BeginTxMock.Return(nil, nil)
  873. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  874. assert.Equal(mc, placeID, id)
  875. }).Return(nil)
  876. mock.CommitTxMock.Return(testError)
  877. return mock
  878. },
  879. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  880. mock := mocks.NewThingRepositoryMock(mc)
  881. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  882. assert.Equal(mc, placeID, id)
  883. }).Return(thingRepoRes, nil)
  884. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  885. assert.Equal(mc, thingID, id)
  886. }).Return(nil)
  887. return mock
  888. },
  889. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  890. mock := mocks.NewPlaceThingRepositoryMock(mc)
  891. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  892. assert.Equal(mc, thingID, id)
  893. }).Return(nil)
  894. return mock
  895. },
  896. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  897. mock := mocks.NewThingImageRepositoryMock(mc)
  898. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  899. assert.Equal(mc, thingID, id)
  900. }).Return(thingImageRepoRes, nil)
  901. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  902. assert.Equal(mc, thingImageID, id)
  903. }).Return(nil)
  904. return mock
  905. },
  906. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  907. mock := mocks.NewPlaceImageRepositoryMock(mc)
  908. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  909. assert.Equal(mc, placeID, id)
  910. }).Return(placeImageRepoRes, nil)
  911. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  912. assert.Equal(mc, placeImageID, id)
  913. }).Return(nil)
  914. return mock
  915. },
  916. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  917. mock := mocks.NewThingTagRepositoryMock(mc)
  918. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  919. assert.Equal(mc, thingID, id)
  920. }).Return(nil)
  921. return mock
  922. },
  923. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  924. mock := mocks.NewThingNotificationRepositoryMock(mc)
  925. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  926. assert.Equal(mc, thingID, id)
  927. }).Return(nil)
  928. return mock
  929. },
  930. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  931. return mocks.NewFileRepositoryMock(mc)
  932. },
  933. },
  934. {
  935. name: "negative case - delete place image error",
  936. req: correctReq,
  937. resCode: fiber.StatusInternalServerError,
  938. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  939. mock := mocks.NewPlaceRepositoryMock(mc)
  940. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  941. assert.Equal(mc, placeID, id)
  942. }).Return(nil, nil)
  943. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  944. assert.Equal(mc, placeID, id)
  945. }).Return(nil, nil)
  946. mock.BeginTxMock.Return(nil, nil)
  947. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  948. assert.Equal(mc, placeID, id)
  949. }).Return(nil)
  950. mock.CommitTxMock.Return(nil)
  951. return mock
  952. },
  953. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  954. mock := mocks.NewThingRepositoryMock(mc)
  955. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  956. assert.Equal(mc, placeID, id)
  957. }).Return(thingRepoRes, nil)
  958. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  959. assert.Equal(mc, thingID, id)
  960. }).Return(nil)
  961. return mock
  962. },
  963. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  964. mock := mocks.NewPlaceThingRepositoryMock(mc)
  965. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  966. assert.Equal(mc, thingID, id)
  967. }).Return(nil)
  968. return mock
  969. },
  970. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  971. mock := mocks.NewThingImageRepositoryMock(mc)
  972. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  973. assert.Equal(mc, thingID, id)
  974. }).Return(thingImageRepoRes, nil)
  975. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  976. assert.Equal(mc, thingImageID, id)
  977. }).Return(nil)
  978. return mock
  979. },
  980. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  981. mock := mocks.NewPlaceImageRepositoryMock(mc)
  982. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  983. assert.Equal(mc, placeID, id)
  984. }).Return(placeImageRepoRes, nil)
  985. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  986. assert.Equal(mc, placeImageID, id)
  987. }).Return(nil)
  988. return mock
  989. },
  990. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  991. mock := mocks.NewThingTagRepositoryMock(mc)
  992. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  993. assert.Equal(mc, thingID, id)
  994. }).Return(nil)
  995. return mock
  996. },
  997. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  998. mock := mocks.NewThingNotificationRepositoryMock(mc)
  999. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1000. assert.Equal(mc, thingID, id)
  1001. }).Return(nil)
  1002. return mock
  1003. },
  1004. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  1005. mock := mocks.NewFileRepositoryMock(mc)
  1006. mock.DeleteMock.When(placeImageURL).Then(testError)
  1007. return mock
  1008. },
  1009. },
  1010. {
  1011. name: "negative case - delete thing image error",
  1012. req: correctReq,
  1013. resCode: fiber.StatusInternalServerError,
  1014. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  1015. mock := mocks.NewPlaceRepositoryMock(mc)
  1016. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  1017. assert.Equal(mc, placeID, id)
  1018. }).Return(nil, nil)
  1019. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  1020. assert.Equal(mc, placeID, id)
  1021. }).Return(nil, nil)
  1022. mock.BeginTxMock.Return(nil, nil)
  1023. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1024. assert.Equal(mc, placeID, id)
  1025. }).Return(nil)
  1026. mock.CommitTxMock.Return(nil)
  1027. return mock
  1028. },
  1029. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  1030. mock := mocks.NewThingRepositoryMock(mc)
  1031. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  1032. assert.Equal(mc, placeID, id)
  1033. }).Return(thingRepoRes, nil)
  1034. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1035. assert.Equal(mc, thingID, id)
  1036. }).Return(nil)
  1037. return mock
  1038. },
  1039. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  1040. mock := mocks.NewPlaceThingRepositoryMock(mc)
  1041. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1042. assert.Equal(mc, thingID, id)
  1043. }).Return(nil)
  1044. return mock
  1045. },
  1046. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  1047. mock := mocks.NewThingImageRepositoryMock(mc)
  1048. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  1049. assert.Equal(mc, thingID, id)
  1050. }).Return(thingImageRepoRes, nil)
  1051. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1052. assert.Equal(mc, thingImageID, id)
  1053. }).Return(nil)
  1054. return mock
  1055. },
  1056. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  1057. mock := mocks.NewPlaceImageRepositoryMock(mc)
  1058. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  1059. assert.Equal(mc, placeID, id)
  1060. }).Return(placeImageRepoRes, nil)
  1061. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1062. assert.Equal(mc, placeImageID, id)
  1063. }).Return(nil)
  1064. return mock
  1065. },
  1066. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  1067. mock := mocks.NewThingTagRepositoryMock(mc)
  1068. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1069. assert.Equal(mc, thingID, id)
  1070. }).Return(nil)
  1071. return mock
  1072. },
  1073. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  1074. mock := mocks.NewThingNotificationRepositoryMock(mc)
  1075. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1076. assert.Equal(mc, thingID, id)
  1077. }).Return(nil)
  1078. return mock
  1079. },
  1080. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  1081. mock := mocks.NewFileRepositoryMock(mc)
  1082. mock.DeleteMock.When(placeImageURL).Then(nil)
  1083. mock.DeleteMock.When(thingImageURL).Then(testError)
  1084. return mock
  1085. },
  1086. },
  1087. {
  1088. name: "positive case",
  1089. req: correctReq,
  1090. resCode: fiber.StatusOK,
  1091. resBody: &dto.EmptyResponse{},
  1092. placeRepoMock: func(mc *minimock.Controller) PlaceRepository {
  1093. mock := mocks.NewPlaceRepositoryMock(mc)
  1094. mock.GetMock.Inspect(func(ctx context.Context, id int) {
  1095. assert.Equal(mc, placeID, id)
  1096. }).Return(nil, nil)
  1097. mock.GetNestedPlacesMock.Inspect(func(ctx context.Context, id int) {
  1098. assert.Equal(mc, placeID, id)
  1099. }).Return(nil, nil)
  1100. mock.BeginTxMock.Return(nil, nil)
  1101. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1102. assert.Equal(mc, placeID, id)
  1103. }).Return(nil)
  1104. mock.CommitTxMock.Return(nil)
  1105. return mock
  1106. },
  1107. thingRepoMock: func(mc *minimock.Controller) ThingRepository {
  1108. mock := mocks.NewThingRepositoryMock(mc)
  1109. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  1110. assert.Equal(mc, placeID, id)
  1111. }).Return(thingRepoRes, nil)
  1112. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1113. assert.Equal(mc, thingID, id)
  1114. }).Return(nil)
  1115. return mock
  1116. },
  1117. placeThingRepoMock: func(mc *minimock.Controller) PlaceThingRepository {
  1118. mock := mocks.NewPlaceThingRepositoryMock(mc)
  1119. mock.DeleteThingMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1120. assert.Equal(mc, thingID, id)
  1121. }).Return(nil)
  1122. return mock
  1123. },
  1124. thingImageRepoMock: func(mc *minimock.Controller) ThingImageRepository {
  1125. mock := mocks.NewThingImageRepositoryMock(mc)
  1126. mock.GetByThingIDMock.Inspect(func(ctx context.Context, id int) {
  1127. assert.Equal(mc, thingID, id)
  1128. }).Return(thingImageRepoRes, nil)
  1129. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1130. assert.Equal(mc, thingImageID, id)
  1131. }).Return(nil)
  1132. return mock
  1133. },
  1134. placeImageRepoMock: func(mc *minimock.Controller) PlaceImageRepository {
  1135. mock := mocks.NewPlaceImageRepositoryMock(mc)
  1136. mock.GetByPlaceIDMock.Inspect(func(ctx context.Context, id int) {
  1137. assert.Equal(mc, placeID, id)
  1138. }).Return(placeImageRepoRes, nil)
  1139. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1140. assert.Equal(mc, placeImageID, id)
  1141. }).Return(nil)
  1142. return mock
  1143. },
  1144. thingTagRepoMock: func(mc *minimock.Controller) ThingTagRepository {
  1145. mock := mocks.NewThingTagRepositoryMock(mc)
  1146. mock.DeleteByThingIDMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1147. assert.Equal(mc, thingID, id)
  1148. }).Return(nil)
  1149. return mock
  1150. },
  1151. thingNotificationRepoMock: func(mc *minimock.Controller) ThingNotificationRepository {
  1152. mock := mocks.NewThingNotificationRepositoryMock(mc)
  1153. mock.DeleteMock.Inspect(func(ctx context.Context, id int, tx *sql.Tx) {
  1154. assert.Equal(mc, thingID, id)
  1155. }).Return(nil)
  1156. return mock
  1157. },
  1158. fileRepoMock: func(mc *minimock.Controller) FileRepository {
  1159. mock := mocks.NewFileRepositoryMock(mc)
  1160. mock.DeleteMock.When(placeImageURL).Then(nil)
  1161. mock.DeleteMock.When(thingImageURL).Then(nil)
  1162. return mock
  1163. },
  1164. },
  1165. }
  1166. for _, tt := range tests {
  1167. t.Run(tt.name, func(t *testing.T) {
  1168. t.Parallel()
  1169. mc := minimock.NewController(t)
  1170. fiberApp := fiber.New()
  1171. fiberApp.Delete("/v1/places/:placeId", DeletePlaceHandler(
  1172. tt.placeRepoMock(mc),
  1173. tt.thingRepoMock(mc),
  1174. tt.placeImageRepoMock(mc),
  1175. tt.thingImageRepoMock(mc),
  1176. tt.placeThingRepoMock(mc),
  1177. tt.thingTagRepoMock(mc),
  1178. tt.thingNotificationRepoMock(mc),
  1179. tt.fileRepoMock(mc),
  1180. ))
  1181. fiberRes, _ := fiberApp.Test(httptest.NewRequest(tt.req.method, tt.req.route, nil), API.DefaultTestTimeOut)
  1182. assert.Equal(t, tt.resCode, fiberRes.StatusCode)
  1183. if tt.resBody != nil {
  1184. assert.Equal(t, helpers.MarshalResponse(tt.resBody), helpers.ConvertBodyToString(fiberRes.Body))
  1185. }
  1186. })
  1187. }
  1188. }