sysDeptModel_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. package dept
  2. import (
  3. "context"
  4. "errors"
  5. "testing"
  6. "time"
  7. "perms-system-server/internal/testutil"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "github.com/zeromicro/go-zero/core/stores/sqlx"
  11. )
  12. // TC-0310: 正常 CRUD
  13. func TestSysDeptModel_CRUD(t *testing.T) {
  14. ctx := context.Background()
  15. conn := testutil.GetTestSqlConn()
  16. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  17. now := time.Now().Unix()
  18. data := &SysDept{
  19. ParentId: 0,
  20. Name: "dept_crud_" + testutil.UniqueId(),
  21. Path: "/crud/" + testutil.UniqueId() + "/",
  22. Sort: 10,
  23. Remark: "r",
  24. Status: 1,
  25. CreateTime: now,
  26. UpdateTime: now,
  27. }
  28. res, err := m.Insert(ctx, data)
  29. require.NoError(t, err)
  30. id, err := res.LastInsertId()
  31. require.NoError(t, err)
  32. tbl := m.TableName()
  33. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id) })
  34. found, err := m.FindOne(ctx, id)
  35. require.NoError(t, err)
  36. require.NotNil(t, found)
  37. assert.Equal(t, id, found.Id)
  38. assert.Equal(t, data.Name, found.Name)
  39. found.Remark = "updated"
  40. found.UpdateTime = now + 1
  41. require.NoError(t, m.Update(ctx, found))
  42. after, err := m.FindOne(ctx, id)
  43. require.NoError(t, err)
  44. assert.Equal(t, "updated", after.Remark)
  45. require.NoError(t, m.Delete(ctx, id))
  46. _, err = m.FindOne(ctx, id)
  47. require.Error(t, err)
  48. assert.True(t, errors.Is(err, ErrNotFound))
  49. }
  50. // TC-0442: FindAll 排序 sort asc, id asc
  51. func TestSysDeptModel_FindAll_OrderBySortAscIdAsc(t *testing.T) {
  52. ctx := context.Background()
  53. conn := testutil.GetTestSqlConn()
  54. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  55. base := testutil.UniqueId()
  56. now := time.Now().Unix()
  57. rows := []*SysDept{
  58. {ParentId: 0, Name: "a_" + base, Path: "/fa/" + base + "/a/", Sort: 30, Status: 1, CreateTime: now, UpdateTime: now},
  59. {ParentId: 0, Name: "b_" + base, Path: "/fa/" + base + "/b/", Sort: 10, Status: 1, CreateTime: now, UpdateTime: now},
  60. {ParentId: 0, Name: "c_" + base, Path: "/fa/" + base + "/c/", Sort: 20, Status: 1, CreateTime: now, UpdateTime: now},
  61. }
  62. require.NoError(t, m.BatchInsert(ctx, rows))
  63. all, err := m.FindAll(ctx)
  64. require.NoError(t, err)
  65. nameSet := map[string]struct{}{rows[0].Name: {}, rows[1].Name: {}, rows[2].Name: {}}
  66. var picked []*SysDept
  67. var ids []int64
  68. for i := range all {
  69. if _, ok := nameSet[all[i].Name]; ok {
  70. picked = append(picked, all[i])
  71. ids = append(ids, all[i].Id)
  72. }
  73. }
  74. tbl := m.TableName()
  75. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, ids...) })
  76. require.Len(t, picked, 3)
  77. for i := 1; i < len(picked); i++ {
  78. prev, cur := picked[i-1], picked[i]
  79. if prev.Sort == cur.Sort {
  80. assert.Less(t, prev.Id, cur.Id)
  81. } else {
  82. assert.Less(t, prev.Sort, cur.Sort)
  83. }
  84. }
  85. assert.Equal(t, int64(10), picked[0].Sort)
  86. assert.Equal(t, int64(20), picked[1].Sort)
  87. assert.Equal(t, int64(30), picked[2].Sort)
  88. }
  89. // TC-0336: 批量 Insert/Delete
  90. func TestSysDeptModel_BatchInsert_BatchDelete(t *testing.T) {
  91. ctx := context.Background()
  92. conn := testutil.GetTestSqlConn()
  93. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  94. now := time.Now().Unix()
  95. tag := testutil.UniqueId()
  96. batch := []*SysDept{
  97. {ParentId: 0, Name: "b1_" + tag, Path: "/bi/" + tag + "/1/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now},
  98. {ParentId: 0, Name: "b2_" + tag, Path: "/bi/" + tag + "/2/", Sort: 2, Status: 1, CreateTime: now, UpdateTime: now},
  99. }
  100. require.NoError(t, m.BatchInsert(ctx, batch))
  101. all, err := m.FindAll(ctx)
  102. require.NoError(t, err)
  103. wanted := map[string]struct{}{batch[0].Name: {}, batch[1].Name: {}}
  104. var ids []int64
  105. for _, row := range all {
  106. if _, ok := wanted[row.Name]; ok {
  107. ids = append(ids, row.Id)
  108. }
  109. }
  110. require.Len(t, ids, 2)
  111. tbl := m.TableName()
  112. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, ids...) })
  113. require.NoError(t, m.BatchDelete(ctx, ids))
  114. for _, id := range ids {
  115. _, err := m.FindOne(ctx, id)
  116. require.Error(t, err)
  117. assert.True(t, errors.Is(err, ErrNotFound))
  118. }
  119. }
  120. // TC-0327: 事务内 Insert/Update
  121. func TestSysDeptModel_TransactCtx_InsertWithTx_UpdateWithTx(t *testing.T) {
  122. ctx := context.Background()
  123. conn := testutil.GetTestSqlConn()
  124. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  125. now := time.Now().Unix()
  126. d := &SysDept{
  127. ParentId: 0, Name: "tx_" + testutil.UniqueId(),
  128. Path: "/tx/" + testutil.UniqueId() + "/",
  129. Sort: 1, Remark: "before", Status: 1,
  130. CreateTime: now, UpdateTime: now,
  131. }
  132. var finalId int64
  133. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  134. res, err := m.InsertWithTx(c, s, d)
  135. if err != nil {
  136. return err
  137. }
  138. lid, err := res.LastInsertId()
  139. if err != nil {
  140. return err
  141. }
  142. finalId = lid
  143. d.Id = finalId
  144. d.Remark = "after_tx"
  145. d.UpdateTime = now + 2
  146. return m.UpdateWithTx(c, s, d)
  147. })
  148. require.NoError(t, err)
  149. tbl := m.TableName()
  150. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, finalId) })
  151. out, err := m.FindOne(ctx, finalId)
  152. require.NoError(t, err)
  153. assert.Equal(t, "after_tx", out.Remark)
  154. }
  155. // TC-0333: 表名
  156. func TestSysDeptModel_TableName(t *testing.T) {
  157. conn := testutil.GetTestSqlConn()
  158. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  159. assert.Equal(t, "`sys_dept`", m.TableName())
  160. }
  161. // TC-0319: FindOne 不存在
  162. func TestSysDeptModel_FindOne_NotFound(t *testing.T) {
  163. conn := testutil.GetTestSqlConn()
  164. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  165. _, err := m.FindOne(context.Background(), 999999999999)
  166. require.ErrorIs(t, err, ErrNotFound)
  167. }
  168. // TC-0326: Update 不存在行不报错
  169. func TestSysDeptModel_Update_NonExistentRow_NoError(t *testing.T) {
  170. conn := testutil.GetTestSqlConn()
  171. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  172. err := m.Update(context.Background(), &SysDept{
  173. Id: 999999999999, Name: "ghost", Path: "/x/", Status: 1,
  174. CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix(),
  175. })
  176. require.NoError(t, err)
  177. }
  178. // TC-0329: Delete 不存在行不报错
  179. func TestSysDeptModel_Delete_NonExistentRow_NoError(t *testing.T) {
  180. conn := testutil.GetTestSqlConn()
  181. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  182. err := m.Delete(context.Background(), 999999999999)
  183. require.NoError(t, err)
  184. }
  185. // TC-0334: BatchInsert 空
  186. func TestSysDeptModel_BatchInsert_Empty(t *testing.T) {
  187. conn := testutil.GetTestSqlConn()
  188. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  189. require.NoError(t, m.BatchInsert(context.Background(), nil))
  190. require.NoError(t, m.BatchInsert(context.Background(), []*SysDept{}))
  191. }
  192. // TC-0353: BatchDelete 空
  193. func TestSysDeptModel_BatchDelete_Empty(t *testing.T) {
  194. conn := testutil.GetTestSqlConn()
  195. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  196. require.NoError(t, m.BatchDelete(context.Background(), nil))
  197. require.NoError(t, m.BatchDelete(context.Background(), []int64{}))
  198. }
  199. // TC-0316: 事务回滚无数据
  200. func TestSysDeptModel_InsertWithTx_Rollback(t *testing.T) {
  201. ctx := context.Background()
  202. conn := testutil.GetTestSqlConn()
  203. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  204. now := time.Now().Unix()
  205. uniq := "txrb_" + testutil.UniqueId()
  206. d := &SysDept{
  207. ParentId: 0, Name: uniq, Path: "/" + uniq + "/",
  208. Sort: 1, Status: 1, CreateTime: now, UpdateTime: now,
  209. }
  210. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  211. if _, e := m.InsertWithTx(c, s, d); e != nil {
  212. return e
  213. }
  214. return errors.New("force rollback")
  215. })
  216. require.Error(t, err)
  217. all, err := m.FindAll(ctx)
  218. require.NoError(t, err)
  219. for _, row := range all {
  220. assert.NotEqual(t, uniq, row.Name)
  221. }
  222. }
  223. // TC-0330: 事务内删除
  224. func TestSysDeptModel_DeleteWithTx(t *testing.T) {
  225. ctx := context.Background()
  226. conn := testutil.GetTestSqlConn()
  227. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  228. now := time.Now().Unix()
  229. d := &SysDept{
  230. ParentId: 0, Name: "deltx_" + testutil.UniqueId(),
  231. Path: "/deltx/" + testutil.UniqueId() + "/",
  232. Sort: 1, Status: 1, CreateTime: now, UpdateTime: now,
  233. }
  234. res, err := m.Insert(ctx, d)
  235. require.NoError(t, err)
  236. id, _ := res.LastInsertId()
  237. tbl := m.TableName()
  238. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id) })
  239. err = m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  240. return m.DeleteWithTx(c, s, id)
  241. })
  242. require.NoError(t, err)
  243. _, err = m.FindOne(ctx, id)
  244. require.ErrorIs(t, err, ErrNotFound)
  245. }
  246. // TC-0332: TransactCtx fn 返回错误时回滚
  247. func TestSysDeptModel_TransactCtx_Rollback(t *testing.T) {
  248. ctx := context.Background()
  249. conn := testutil.GetTestSqlConn()
  250. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  251. now := time.Now().Unix()
  252. uniq := "txrb2_" + testutil.UniqueId()
  253. d := &SysDept{
  254. ParentId: 0, Name: uniq, Path: "/" + uniq + "/",
  255. Sort: 1, Status: 1, CreateTime: now, UpdateTime: now,
  256. }
  257. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  258. if _, e := m.InsertWithTx(c, s, d); e != nil {
  259. return e
  260. }
  261. return errors.New("force rollback")
  262. })
  263. require.Error(t, err)
  264. require.Contains(t, err.Error(), "force rollback")
  265. all, err := m.FindAll(ctx)
  266. require.NoError(t, err)
  267. for _, row := range all {
  268. assert.NotEqual(t, uniq, row.Name)
  269. }
  270. }
  271. // TC-0343: BatchUpdate 空
  272. func TestSysDeptModel_BatchUpdate_Empty(t *testing.T) {
  273. conn := testutil.GetTestSqlConn()
  274. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  275. require.NoError(t, m.BatchUpdate(context.Background(), nil))
  276. require.NoError(t, m.BatchUpdate(context.Background(), []*SysDept{}))
  277. }
  278. // TC-0345: BatchUpdate 多条
  279. func TestSysDeptModel_BatchUpdate_Multi(t *testing.T) {
  280. ctx := context.Background()
  281. conn := testutil.GetTestSqlConn()
  282. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  283. now := time.Now().Unix()
  284. tag := testutil.UniqueId()
  285. d1 := &SysDept{ParentId: 0, Name: "bu1_" + tag, Path: "/bu/" + tag + "/1/", Sort: 1, Remark: "r1", Status: 1, CreateTime: now, UpdateTime: now}
  286. d2 := &SysDept{ParentId: 0, Name: "bu2_" + tag, Path: "/bu/" + tag + "/2/", Sort: 2, Remark: "r2", Status: 1, CreateTime: now, UpdateTime: now}
  287. r1, err := m.Insert(ctx, d1)
  288. require.NoError(t, err)
  289. id1, _ := r1.LastInsertId()
  290. r2, err := m.Insert(ctx, d2)
  291. require.NoError(t, err)
  292. id2, _ := r2.LastInsertId()
  293. tbl := m.TableName()
  294. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id1, id2) })
  295. now2 := time.Now().Unix()
  296. upd := []*SysDept{
  297. {Id: id1, ParentId: 0, Name: "bu1_upd", Path: d1.Path, Sort: 10, Remark: "updated", Status: 1, CreateTime: now, UpdateTime: now2},
  298. {Id: id2, ParentId: 0, Name: "bu2_upd", Path: d2.Path, Sort: 20, Remark: "updated", Status: 2, CreateTime: now, UpdateTime: now2},
  299. }
  300. require.NoError(t, m.BatchUpdate(ctx, upd))
  301. g1, err := m.FindOne(ctx, id1)
  302. require.NoError(t, err)
  303. assert.Equal(t, "bu1_upd", g1.Name)
  304. assert.Equal(t, int64(10), g1.Sort)
  305. g2, err := m.FindOne(ctx, id2)
  306. require.NoError(t, err)
  307. assert.Equal(t, "bu2_upd", g2.Name)
  308. assert.Equal(t, int64(2), g2.Status)
  309. }
  310. // TC-0335: BatchInsert 单条
  311. func TestSysDeptModel_BatchInsert_Single(t *testing.T) {
  312. ctx := context.Background()
  313. conn := testutil.GetTestSqlConn()
  314. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  315. now := time.Now().Unix()
  316. uniq := "bis_" + testutil.UniqueId()
  317. d := &SysDept{ParentId: 0, Name: uniq, Path: "/" + uniq + "/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now}
  318. require.NoError(t, m.BatchInsert(ctx, []*SysDept{d}))
  319. all, err := m.FindAll(ctx)
  320. require.NoError(t, err)
  321. var id int64
  322. for _, row := range all {
  323. if row.Name == uniq {
  324. id = row.Id
  325. break
  326. }
  327. }
  328. require.NotZero(t, id)
  329. tbl := m.TableName()
  330. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id) })
  331. }
  332. // TC-0341: BatchInsertWithTx 正常
  333. func TestSysDeptModel_BatchInsertWithTx_Normal(t *testing.T) {
  334. ctx := context.Background()
  335. conn := testutil.GetTestSqlConn()
  336. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  337. now := time.Now().Unix()
  338. tag := testutil.UniqueId()
  339. batch := []*SysDept{
  340. {ParentId: 0, Name: "bitx1_" + tag, Path: "/bitx/" + tag + "/1/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now},
  341. {ParentId: 0, Name: "bitx2_" + tag, Path: "/bitx/" + tag + "/2/", Sort: 2, Status: 1, CreateTime: now, UpdateTime: now},
  342. }
  343. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  344. return m.BatchInsertWithTx(c, s, batch)
  345. })
  346. require.NoError(t, err)
  347. all, err := m.FindAll(ctx)
  348. require.NoError(t, err)
  349. wanted := map[string]struct{}{batch[0].Name: {}, batch[1].Name: {}}
  350. var ids []int64
  351. for _, row := range all {
  352. if _, ok := wanted[row.Name]; ok {
  353. ids = append(ids, row.Id)
  354. }
  355. }
  356. require.Len(t, ids, 2)
  357. tbl := m.TableName()
  358. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, ids...) })
  359. }
  360. // TC-0340: BatchInsertWithTx 空
  361. func TestSysDeptModel_BatchInsertWithTx_Empty(t *testing.T) {
  362. conn := testutil.GetTestSqlConn()
  363. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  364. err := m.TransactCtx(context.Background(), func(c context.Context, s sqlx.Session) error {
  365. return m.BatchInsertWithTx(c, s, nil)
  366. })
  367. require.NoError(t, err)
  368. }
  369. // TC-0342: BatchInsertWithTx 回滚
  370. func TestSysDeptModel_BatchInsertWithTx_Rollback(t *testing.T) {
  371. ctx := context.Background()
  372. conn := testutil.GetTestSqlConn()
  373. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  374. now := time.Now().Unix()
  375. uniq := "rbn_" + testutil.UniqueId()
  376. batch := []*SysDept{
  377. {ParentId: 0, Name: uniq, Path: "/rbn/" + uniq + "/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now},
  378. }
  379. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  380. if e := m.BatchInsertWithTx(c, s, batch); e != nil {
  381. return e
  382. }
  383. return errors.New("force rollback")
  384. })
  385. require.Error(t, err)
  386. all, err := m.FindAll(ctx)
  387. require.NoError(t, err)
  388. for _, row := range all {
  389. assert.NotEqual(t, uniq, row.Name)
  390. }
  391. }
  392. // TC-0349: BatchUpdateWithTx 正常
  393. func TestSysDeptModel_BatchUpdateWithTx_Normal(t *testing.T) {
  394. ctx := context.Background()
  395. conn := testutil.GetTestSqlConn()
  396. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  397. now := time.Now().Unix()
  398. tag := testutil.UniqueId()
  399. d1 := &SysDept{ParentId: 0, Name: "butx1_" + tag, Path: "/butx/" + tag + "/1/", Sort: 1, Remark: "old", Status: 1, CreateTime: now, UpdateTime: now}
  400. d2 := &SysDept{ParentId: 0, Name: "butx2_" + tag, Path: "/butx/" + tag + "/2/", Sort: 2, Remark: "old", Status: 1, CreateTime: now, UpdateTime: now}
  401. r1, err := m.Insert(ctx, d1)
  402. require.NoError(t, err)
  403. id1, _ := r1.LastInsertId()
  404. r2, err := m.Insert(ctx, d2)
  405. require.NoError(t, err)
  406. id2, _ := r2.LastInsertId()
  407. tbl := m.TableName()
  408. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id1, id2) })
  409. now2 := time.Now().Unix()
  410. err = m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  411. return m.BatchUpdateWithTx(c, s, []*SysDept{
  412. {Id: id1, ParentId: 0, Name: "butx1_new", Path: d1.Path, Sort: 10, Remark: "new", Status: 1, CreateTime: now, UpdateTime: now2},
  413. {Id: id2, ParentId: 0, Name: "butx2_new", Path: d2.Path, Sort: 20, Remark: "new", Status: 1, CreateTime: now, UpdateTime: now2},
  414. })
  415. })
  416. require.NoError(t, err)
  417. g1, err := m.FindOne(ctx, id1)
  418. require.NoError(t, err)
  419. assert.Equal(t, "butx1_new", g1.Name)
  420. assert.Equal(t, int64(10), g1.Sort)
  421. g2, err := m.FindOne(ctx, id2)
  422. require.NoError(t, err)
  423. assert.Equal(t, "butx2_new", g2.Name)
  424. }
  425. // TC-0348: BatchUpdateWithTx 空
  426. func TestSysDeptModel_BatchUpdateWithTx_Empty(t *testing.T) {
  427. conn := testutil.GetTestSqlConn()
  428. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  429. err := m.TransactCtx(context.Background(), func(c context.Context, s sqlx.Session) error {
  430. return m.BatchUpdateWithTx(c, s, nil)
  431. })
  432. require.NoError(t, err)
  433. }
  434. // TC-0354: BatchDelete 单条
  435. func TestSysDeptModel_BatchDelete_Single(t *testing.T) {
  436. ctx := context.Background()
  437. conn := testutil.GetTestSqlConn()
  438. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  439. now := time.Now().Unix()
  440. d := &SysDept{ParentId: 0, Name: "bds_" + testutil.UniqueId(), Path: "/bds/" + testutil.UniqueId() + "/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now}
  441. res, err := m.Insert(ctx, d)
  442. require.NoError(t, err)
  443. id, _ := res.LastInsertId()
  444. tbl := m.TableName()
  445. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id) })
  446. require.NoError(t, m.BatchDelete(ctx, []int64{id}))
  447. _, err = m.FindOne(ctx, id)
  448. require.ErrorIs(t, err, ErrNotFound)
  449. }
  450. // TC-0356: BatchDelete 包含不存在 id
  451. func TestSysDeptModel_BatchDelete_ContainsNonExist(t *testing.T) {
  452. ctx := context.Background()
  453. conn := testutil.GetTestSqlConn()
  454. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  455. now := time.Now().Unix()
  456. d := &SysDept{ParentId: 0, Name: "bdne_" + testutil.UniqueId(), Path: "/bdne/" + testutil.UniqueId() + "/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now}
  457. res, err := m.Insert(ctx, d)
  458. require.NoError(t, err)
  459. id, _ := res.LastInsertId()
  460. tbl := m.TableName()
  461. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id) })
  462. require.NoError(t, m.BatchDelete(ctx, []int64{id, 999999999}))
  463. _, err = m.FindOne(ctx, id)
  464. require.ErrorIs(t, err, ErrNotFound)
  465. }
  466. // TC-0358: BatchDeleteWithTx 正常
  467. func TestSysDeptModel_BatchDeleteWithTx_Normal(t *testing.T) {
  468. ctx := context.Background()
  469. conn := testutil.GetTestSqlConn()
  470. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  471. now := time.Now().Unix()
  472. tag := testutil.UniqueId()
  473. d1 := &SysDept{ParentId: 0, Name: "bdtx1_" + tag, Path: "/bdtx/" + tag + "/1/", Sort: 1, Status: 1, CreateTime: now, UpdateTime: now}
  474. d2 := &SysDept{ParentId: 0, Name: "bdtx2_" + tag, Path: "/bdtx/" + tag + "/2/", Sort: 2, Status: 1, CreateTime: now, UpdateTime: now}
  475. r1, err := m.Insert(ctx, d1)
  476. require.NoError(t, err)
  477. id1, _ := r1.LastInsertId()
  478. r2, err := m.Insert(ctx, d2)
  479. require.NoError(t, err)
  480. id2, _ := r2.LastInsertId()
  481. tbl := m.TableName()
  482. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, id1, id2) })
  483. err = m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  484. return m.BatchDeleteWithTx(c, s, []int64{id1, id2})
  485. })
  486. require.NoError(t, err)
  487. _, err = m.FindOne(ctx, id1)
  488. require.ErrorIs(t, err, ErrNotFound)
  489. _, err = m.FindOne(ctx, id2)
  490. require.ErrorIs(t, err, ErrNotFound)
  491. }
  492. // TC-0357: BatchDeleteWithTx 空
  493. func TestSysDeptModel_BatchDeleteWithTx_Empty(t *testing.T) {
  494. conn := testutil.GetTestSqlConn()
  495. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  496. err := m.TransactCtx(context.Background(), func(c context.Context, s sqlx.Session) error {
  497. return m.BatchDeleteWithTx(c, s, nil)
  498. })
  499. require.NoError(t, err)
  500. }
  501. // TC-0323: 事务内 FindOne
  502. func TestSysDeptModel_FindOneWithTx_InsertThenFind(t *testing.T) {
  503. ctx := context.Background()
  504. conn := testutil.GetTestSqlConn()
  505. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  506. now := time.Now().Unix()
  507. var foundInTx *SysDept
  508. var insertedId int64
  509. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  510. res, err := m.InsertWithTx(c, s, &SysDept{
  511. ParentId: 0, Name: "ftx_" + testutil.UniqueId(), Path: "/ftx/" + testutil.UniqueId() + "/",
  512. Sort: 1, DeptType: "NORMAL", Status: 1, CreateTime: now, UpdateTime: now,
  513. })
  514. if err != nil {
  515. return err
  516. }
  517. insertedId, _ = res.LastInsertId()
  518. foundInTx, err = m.FindOneWithTx(c, s, insertedId)
  519. return err
  520. })
  521. require.NoError(t, err)
  522. tbl := m.TableName()
  523. t.Cleanup(func() { testutil.CleanTable(ctx, conn, tbl, insertedId) })
  524. require.NotNil(t, foundInTx)
  525. assert.Equal(t, insertedId, foundInTx.Id)
  526. }
  527. // TC-0322: FindOneWithTx 不存在
  528. func TestSysDeptModel_FindOneWithTx_NotFound(t *testing.T) {
  529. ctx := context.Background()
  530. conn := testutil.GetTestSqlConn()
  531. m := NewSysDeptModel(conn, testutil.GetTestCacheConf(), testutil.GetTestCachePrefix())
  532. err := m.TransactCtx(ctx, func(c context.Context, s sqlx.Session) error {
  533. _, err := m.FindOneWithTx(c, s, 999999999999)
  534. return err
  535. })
  536. require.ErrorIs(t, err, ErrNotFound)
  537. }