v1_test.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. package v1
  2. import (
  3. "fmt"
  4. "git.sxidc.com/service-supports/dps-sdk/client"
  5. "math/rand"
  6. "testing"
  7. "time"
  8. )
  9. var tableModelDescribe = client.TableModelDescribe{
  10. Fields: []client.TableModelField{
  11. {"ID", "gorm:\"primary_key;type:varchar(32);comment:id;\""},
  12. {"Name", "gorm:\"not null;type:varchar(128);comment:数据库名称;\""},
  13. {"Time", "gorm:\"not null;type:timestamp with time zone;comment:数据库时间;\""},
  14. {"TableNum", "gorm:\"not null;type:integer;comment:数据库表数量;\""},
  15. },
  16. }
  17. func TestAutoMigrate(t *testing.T) {
  18. initClient(t, "localhost:30170")
  19. defer destroyClient(t)
  20. newToolKit(t).
  21. autoMigrate(&client.AutoMigrateRequest{
  22. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  23. AutoMigrateItem: client.AutoMigrateItem{
  24. TablePrefixWithSchema: "test." + simpleUUID()[0:8],
  25. Version: "v1",
  26. TableModelDescribe: tableModelDescribe,
  27. },
  28. }).
  29. autoMigrateBatch(&client.AutoMigrateBatchRequest{
  30. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  31. Items: []client.AutoMigrateItem{
  32. {
  33. TablePrefixWithSchema: "test." + simpleUUID()[0:8],
  34. Version: "v1",
  35. TableModelDescribe: tableModelDescribe,
  36. },
  37. {
  38. TablePrefixWithSchema: "test." + simpleUUID()[0:8],
  39. Version: "v1",
  40. TableModelDescribe: tableModelDescribe,
  41. },
  42. },
  43. })
  44. }
  45. func TestTransaction(t *testing.T) {
  46. initClient(t, "localhost:30170")
  47. defer destroyClient(t)
  48. tablePrefix := "test." + simpleUUID()[0:8]
  49. id := simpleUUID()
  50. name := simpleUUID()
  51. now := time.Now().Local()
  52. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  53. newName := simpleUUID()
  54. newNow := time.Now().Local()
  55. newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  56. var count int64
  57. resultMap := make(map[string]any)
  58. newToolKit(t).
  59. autoMigrate(&client.AutoMigrateRequest{
  60. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  61. AutoMigrateItem: client.AutoMigrateItem{
  62. TablePrefixWithSchema: tablePrefix,
  63. Version: "v1",
  64. TableModelDescribe: tableModelDescribe,
  65. },
  66. }).
  67. transaction("2b78141779ee432295ca371b91c5cac7", func(tx client.Transaction) error {
  68. statement, err := tx.InsertTx(&client.InsertRequest{
  69. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  70. TablePrefixWithSchema: tablePrefix,
  71. Version: "v1",
  72. KeyColumns: []string{"id"},
  73. TableRow: map[string]any{
  74. "id": id,
  75. "name": name,
  76. "time": now,
  77. "table_num": tableNum,
  78. },
  79. UserID: "test",
  80. })
  81. if err != nil {
  82. return err
  83. }
  84. fmt.Println(statement)
  85. err = tx.End()
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }).
  91. queryByKeys(&client.QueryByKeysRequest{
  92. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  93. TablePrefixWithSchema: tablePrefix,
  94. Version: "v1",
  95. KeyValues: map[string]string{"id": id},
  96. }, &resultMap).
  97. assertEqual(id, resultMap["id"], "ID不一致").
  98. assertEqual(name, resultMap["name"], "名称不一致").
  99. assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
  100. assertEqual(tableNum, resultMap["table_num"], "表数量不一致").
  101. transaction("2b78141779ee432295ca371b91c5cac7", func(tx client.Transaction) error {
  102. statement, err := tx.UpdateTx(&client.UpdateRequest{
  103. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  104. TablePrefixWithSchema: tablePrefix,
  105. Version: "v1",
  106. KeyValues: map[string]string{"id": id},
  107. NewTableRow: map[string]any{
  108. "id": id,
  109. "name": newName,
  110. "time": newNow,
  111. "table_num": newTableNum,
  112. },
  113. UserID: "test",
  114. })
  115. if err != nil {
  116. return err
  117. }
  118. fmt.Println(statement)
  119. err = tx.End()
  120. if err != nil {
  121. return err
  122. }
  123. return nil
  124. }).
  125. queryByKeys(&client.QueryByKeysRequest{
  126. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  127. TablePrefixWithSchema: tablePrefix,
  128. Version: "v1",
  129. KeyValues: map[string]string{"id": id},
  130. }, &resultMap).
  131. assertEqual(id, resultMap["id"], "ID不一致").
  132. assertEqual(newName, resultMap["name"], "名称不一致").
  133. assertEqual(newNow.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
  134. assertEqual(newTableNum, resultMap["table_num"], "表数量不一致").
  135. transaction("2b78141779ee432295ca371b91c5cac7", func(tx client.Transaction) error {
  136. statement, err := tx.UpdateTx(&client.UpdateRequest{
  137. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  138. TablePrefixWithSchema: tablePrefix,
  139. Version: "v1",
  140. KeyValues: map[string]string{"id": id},
  141. NewTableRow: map[string]any{
  142. "id": id,
  143. "name": name,
  144. "time": now,
  145. "table_num": tableNum,
  146. },
  147. UserID: "test",
  148. })
  149. if err != nil {
  150. return err
  151. }
  152. fmt.Println(statement)
  153. statement, err = tx.DeleteTx(&client.DeleteRequest{
  154. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  155. TablePrefixWithSchema: tablePrefix,
  156. Version: "v1",
  157. KeyValues: map[string]string{"id": id},
  158. UserID: "test",
  159. })
  160. if err != nil {
  161. return err
  162. }
  163. fmt.Println(statement)
  164. err = tx.End()
  165. if err != nil {
  166. return err
  167. }
  168. return nil
  169. }).
  170. countWhere(&client.CountWhereRequest{
  171. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  172. TablePrefixWithSchema: tablePrefix,
  173. Version: "v1",
  174. Where: []client.ColumnCompare{
  175. {
  176. Column: "id",
  177. Value: id,
  178. Compare: client.CompareEqual,
  179. },
  180. },
  181. }, &count).
  182. assertEqual(int64(0), count, "数量不一致")
  183. }
  184. func TestTransactionBatch(t *testing.T) {
  185. initClient(t, "localhost:30170")
  186. defer destroyClient(t)
  187. tablePrefix := "test." + simpleUUID()[0:8]
  188. id1 := simpleUUID()
  189. name1 := simpleUUID()
  190. now1 := time.Now().Local()
  191. tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
  192. id2 := simpleUUID()
  193. name2 := simpleUUID()
  194. now2 := time.Now().Local()
  195. tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
  196. var count int64
  197. resultMap := make(map[string]any)
  198. newToolKit(t).
  199. autoMigrate(&client.AutoMigrateRequest{
  200. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  201. AutoMigrateItem: client.AutoMigrateItem{
  202. TablePrefixWithSchema: tablePrefix,
  203. Version: "v1",
  204. TableModelDescribe: tableModelDescribe,
  205. },
  206. }).
  207. transaction("2b78141779ee432295ca371b91c5cac7", func(tx client.Transaction) error {
  208. statement, err := tx.InsertBatchTx(&client.InsertBatchRequest{
  209. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  210. Items: []*client.InsertTableItem{
  211. {
  212. TablePrefixWithSchema: tablePrefix,
  213. Version: "v1",
  214. Items: []*client.InsertItem{
  215. {
  216. KeyColumns: []string{"id"},
  217. TableRow: map[string]any{
  218. "id": id1,
  219. "name": name1,
  220. "time": now1,
  221. "table_num": tableNum1,
  222. },
  223. },
  224. {
  225. KeyColumns: []string{"id"},
  226. TableRow: map[string]any{
  227. "id": id2,
  228. "name": name2,
  229. "time": now2,
  230. "table_num": tableNum2,
  231. },
  232. },
  233. },
  234. },
  235. },
  236. UserID: "test",
  237. })
  238. if err != nil {
  239. return err
  240. }
  241. fmt.Println(statement)
  242. err = tx.End()
  243. if err != nil {
  244. return err
  245. }
  246. return nil
  247. }).
  248. queryByKeys(&client.QueryByKeysRequest{
  249. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  250. TablePrefixWithSchema: tablePrefix,
  251. Version: "v1",
  252. KeyValues: map[string]string{"id": id1},
  253. }, &resultMap).
  254. assertEqual(id1, resultMap["id"], "ID不一致").
  255. assertEqual(name1, resultMap["name"], "名称不一致").
  256. assertEqual(now1.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
  257. assertEqual(tableNum1, resultMap["table_num"], "表数量不一致").
  258. queryByKeys(&client.QueryByKeysRequest{
  259. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  260. TablePrefixWithSchema: tablePrefix,
  261. Version: "v1",
  262. KeyValues: map[string]string{"id": id2},
  263. }, &resultMap).
  264. assertEqual(id2, resultMap["id"], "ID不一致").
  265. assertEqual(name2, resultMap["name"], "名称不一致").
  266. assertEqual(now2.UnixMilli(), resultMap["time"].(time.Time).UnixMilli(), "时间不一致").
  267. assertEqual(tableNum2, resultMap["table_num"], "表数量不一致").
  268. transaction("2b78141779ee432295ca371b91c5cac7", func(tx client.Transaction) error {
  269. statement, err := tx.DeleteBatchTx(&client.DeleteBatchRequest{
  270. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  271. Items: []*client.DeleteTableItem{
  272. {
  273. TablePrefixWithSchema: tablePrefix,
  274. Version: "v1",
  275. Items: []*client.DeleteItem{
  276. {KeyValues: map[string]string{"id": id1}},
  277. {KeyValues: map[string]string{"id": id2}},
  278. },
  279. },
  280. },
  281. UserID: "test",
  282. })
  283. if err != nil {
  284. return err
  285. }
  286. fmt.Println(statement)
  287. err = tx.End()
  288. if err != nil {
  289. return err
  290. }
  291. return nil
  292. }).
  293. countWhere(&client.CountWhereRequest{
  294. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  295. TablePrefixWithSchema: tablePrefix,
  296. Version: "v1",
  297. Where: []client.ColumnCompare{
  298. {
  299. Column: "id",
  300. Value: id1,
  301. Compare: client.CompareEqual,
  302. },
  303. },
  304. }, &count).
  305. assertEqual(int64(0), count, "数量不一致").
  306. countWhere(&client.CountWhereRequest{
  307. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  308. TablePrefixWithSchema: tablePrefix,
  309. Version: "v1",
  310. Where: []client.ColumnCompare{
  311. {
  312. Column: "id",
  313. Value: id2,
  314. Compare: client.CompareEqual,
  315. },
  316. },
  317. }, &count).
  318. assertEqual(int64(0), count, "数量不一致")
  319. }
  320. func TestInsert(t *testing.T) {
  321. initClient(t, "localhost:30170")
  322. defer destroyClient(t)
  323. tablePrefix := "test." + simpleUUID()[0:8]
  324. id := simpleUUID()
  325. name := simpleUUID()
  326. now := time.Now().Local()
  327. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  328. resultMap := make(map[string]any)
  329. newToolKit(t).
  330. autoMigrate(&client.AutoMigrateRequest{
  331. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  332. AutoMigrateItem: client.AutoMigrateItem{
  333. TablePrefixWithSchema: tablePrefix,
  334. Version: "v1",
  335. TableModelDescribe: tableModelDescribe,
  336. },
  337. }).
  338. insert(&client.InsertRequest{
  339. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  340. TablePrefixWithSchema: tablePrefix,
  341. Version: "v1",
  342. KeyColumns: []string{"id"},
  343. TableRow: map[string]any{
  344. "id": id,
  345. "name": name,
  346. "time": now,
  347. "table_num": tableNum,
  348. },
  349. UserID: "test",
  350. }).
  351. queryByKeys(&client.QueryByKeysRequest{
  352. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  353. TablePrefixWithSchema: tablePrefix,
  354. Version: "v1",
  355. KeyValues: map[string]string{"id": id},
  356. }, &resultMap).
  357. assertEqual(id, resultMap["id"], "ID不一致").
  358. assertEqual(name, resultMap["name"], "名称不一致").
  359. assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
  360. assertEqual(tableNum, resultMap["table_num"], "表数量不一致")
  361. }
  362. func TestInsertBatch(t *testing.T) {
  363. initClient(t, "localhost:30170")
  364. defer destroyClient(t)
  365. tablePrefix := "test." + simpleUUID()[0:8]
  366. id1 := simpleUUID()
  367. name1 := simpleUUID()
  368. now1 := time.Now().Local()
  369. tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
  370. id2 := simpleUUID()
  371. name2 := simpleUUID()
  372. now2 := time.Now().Local()
  373. tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
  374. resultsMap := make([]map[string]any, 0)
  375. var totalCount int64
  376. newToolKit(t).
  377. autoMigrate(&client.AutoMigrateRequest{
  378. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  379. AutoMigrateItem: client.AutoMigrateItem{
  380. TablePrefixWithSchema: tablePrefix,
  381. Version: "v1",
  382. TableModelDescribe: tableModelDescribe,
  383. },
  384. }).
  385. insertBatch(&client.InsertBatchRequest{
  386. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  387. Items: []*client.InsertTableItem{
  388. {
  389. TablePrefixWithSchema: tablePrefix,
  390. Version: "v1",
  391. Items: []*client.InsertItem{
  392. {
  393. KeyColumns: []string{"id"},
  394. TableRow: map[string]any{
  395. "id": id1,
  396. "name": name1,
  397. "time": now1,
  398. "table_num": tableNum1,
  399. },
  400. },
  401. {
  402. KeyColumns: []string{"id"},
  403. TableRow: map[string]any{
  404. "id": id2,
  405. "name": name2,
  406. "time": now2,
  407. "table_num": tableNum2,
  408. },
  409. },
  410. },
  411. },
  412. },
  413. UserID: "test",
  414. }).
  415. queryByWhereAndOrderBy(&client.QueryByWhereAndOrderByRequest{
  416. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  417. TablePrefixWithSchema: tablePrefix,
  418. Version: "v1",
  419. Where: []client.ColumnCompare{
  420. {Column: "id", Value: id1, Compare: client.CompareEqual},
  421. {Column: "name", Value: name1, Compare: client.CompareEqual},
  422. {Column: "table_num", Value: tableNum1, Compare: client.CompareEqual},
  423. },
  424. PageNo: 1,
  425. PageSize: 1,
  426. }, &resultsMap, &totalCount).
  427. assertEqual(1, int(totalCount), "总数不一致").
  428. assertEqual(id1, resultsMap[0]["id"], "ID不一致").
  429. assertEqual(name1, resultsMap[0]["name"], "名称不一致").
  430. assertEqual(now1.UnixMilli(), resultsMap[0]["time"].(time.Time).Local().UnixMilli(), "时间不一致").
  431. assertEqual(tableNum1, resultsMap[0]["table_num"], "表数量不一致").
  432. commonQuery(&client.CommonQueryRequest{
  433. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  434. TablePrefixWithSchema: tablePrefix,
  435. Version: "v1",
  436. Where: []client.ColumnCompare{
  437. {Column: "id", Value: id2, Compare: client.CompareEqual},
  438. {Column: "name", Value: name2, Compare: client.CompareEqual},
  439. {Column: "table_num", Value: tableNum2, Compare: client.CompareEqual},
  440. },
  441. PageNo: 1,
  442. PageSize: 1,
  443. }, &resultsMap, &totalCount).
  444. assertEqual(1, int(totalCount), "总数不一致").
  445. assertEqual(id2, resultsMap[0]["id"], "ID不一致").
  446. assertEqual(name2, resultsMap[0]["name"], "名称不一致").
  447. assertEqual(now2.UnixMilli(), resultsMap[0]["time"].(time.Time).Local().UnixMilli(), "时间不一致").
  448. assertEqual(tableNum2, resultsMap[0]["table_num"], "表数量不一致")
  449. }
  450. func TestUpdate(t *testing.T) {
  451. initClient(t, "localhost:30170")
  452. defer destroyClient(t)
  453. tablePrefix := "test." + simpleUUID()[0:8]
  454. id := simpleUUID()
  455. name := simpleUUID()
  456. now := time.Now().Local()
  457. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  458. newName := simpleUUID()
  459. newNow := time.Now().Local()
  460. newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  461. resultMap := make(map[string]any)
  462. newToolKit(t).
  463. autoMigrate(&client.AutoMigrateRequest{
  464. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  465. AutoMigrateItem: client.AutoMigrateItem{
  466. TablePrefixWithSchema: tablePrefix,
  467. Version: "v1",
  468. TableModelDescribe: tableModelDescribe,
  469. },
  470. }).
  471. insert(&client.InsertRequest{
  472. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  473. TablePrefixWithSchema: tablePrefix,
  474. Version: "v1",
  475. KeyColumns: []string{"id"},
  476. TableRow: map[string]any{
  477. "id": id,
  478. "name": name,
  479. "time": now,
  480. "table_num": tableNum,
  481. },
  482. UserID: "test",
  483. }).
  484. update(&client.UpdateRequest{
  485. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  486. TablePrefixWithSchema: tablePrefix,
  487. Version: "v1",
  488. KeyValues: map[string]string{"id": id},
  489. NewTableRow: map[string]any{
  490. "id": id,
  491. "name": newName,
  492. "time": newNow,
  493. "table_num": newTableNum,
  494. },
  495. UserID: "test",
  496. }).
  497. queryByKeys(&client.QueryByKeysRequest{
  498. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  499. TablePrefixWithSchema: tablePrefix,
  500. Version: "v1",
  501. KeyValues: map[string]string{"id": id},
  502. }, &resultMap).
  503. assertEqual(id, resultMap["id"], "ID不一致").
  504. assertEqual(newName, resultMap["name"], "名称不一致").
  505. assertEqual(newNow.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
  506. assertEqual(newTableNum, resultMap["table_num"], "表数量不一致")
  507. }
  508. func TestDelete(t *testing.T) {
  509. initClient(t, "localhost:30170")
  510. defer destroyClient(t)
  511. tablePrefix := "test." + simpleUUID()[0:8]
  512. id := simpleUUID()
  513. name := simpleUUID()
  514. now := time.Now().Local()
  515. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  516. var count int64
  517. newToolKit(t).
  518. autoMigrate(&client.AutoMigrateRequest{
  519. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  520. AutoMigrateItem: client.AutoMigrateItem{
  521. TablePrefixWithSchema: tablePrefix,
  522. Version: "v1",
  523. TableModelDescribe: tableModelDescribe,
  524. },
  525. }).
  526. insert(&client.InsertRequest{
  527. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  528. TablePrefixWithSchema: tablePrefix,
  529. Version: "v1",
  530. KeyColumns: []string{"id"},
  531. TableRow: map[string]any{
  532. "id": id,
  533. "name": name,
  534. "time": now,
  535. "table_num": tableNum,
  536. },
  537. UserID: "test",
  538. }).
  539. delete(&client.DeleteRequest{
  540. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  541. TablePrefixWithSchema: tablePrefix,
  542. Version: "v1",
  543. KeyValues: map[string]string{"id": id},
  544. UserID: "test",
  545. }).
  546. countWhere(&client.CountWhereRequest{
  547. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  548. TablePrefixWithSchema: tablePrefix,
  549. Version: "v1",
  550. Where: []client.ColumnCompare{
  551. {
  552. Column: "id",
  553. Value: id,
  554. Compare: client.CompareEqual,
  555. },
  556. },
  557. }, &count).
  558. assertEqual(int64(0), count, "数量不一致")
  559. }
  560. func TestDeleteBatch(t *testing.T) {
  561. initClient(t, "localhost:30170")
  562. defer destroyClient(t)
  563. tablePrefix := "test." + simpleUUID()[0:8]
  564. id1 := simpleUUID()
  565. name1 := simpleUUID()
  566. now1 := time.Now().Local()
  567. tableNum1 := rand.New(rand.NewSource(now1.Unix())).Intn(10)
  568. id2 := simpleUUID()
  569. name2 := simpleUUID()
  570. now2 := time.Now().Local()
  571. tableNum2 := rand.New(rand.NewSource(now2.Unix())).Intn(10)
  572. var count int64
  573. newToolKit(t).
  574. autoMigrate(&client.AutoMigrateRequest{
  575. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  576. AutoMigrateItem: client.AutoMigrateItem{
  577. TablePrefixWithSchema: tablePrefix,
  578. Version: "v1",
  579. TableModelDescribe: tableModelDescribe,
  580. },
  581. }).
  582. insertBatch(&client.InsertBatchRequest{
  583. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  584. Items: []*client.InsertTableItem{
  585. {
  586. TablePrefixWithSchema: tablePrefix,
  587. Version: "v1",
  588. Items: []*client.InsertItem{
  589. {
  590. KeyColumns: []string{"id"},
  591. TableRow: map[string]any{
  592. "id": id1,
  593. "name": name1,
  594. "time": now1,
  595. "table_num": tableNum1,
  596. },
  597. },
  598. {
  599. KeyColumns: []string{"id"},
  600. TableRow: map[string]any{
  601. "id": id2,
  602. "name": name2,
  603. "time": now2,
  604. "table_num": tableNum2,
  605. },
  606. },
  607. },
  608. },
  609. },
  610. UserID: "test",
  611. }).
  612. deleteBatch(&client.DeleteBatchRequest{
  613. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  614. Items: []*client.DeleteTableItem{
  615. {
  616. TablePrefixWithSchema: tablePrefix,
  617. Version: "v1",
  618. Items: []*client.DeleteItem{
  619. {KeyValues: map[string]string{"id": id1}},
  620. {KeyValues: map[string]string{"id": id2}},
  621. },
  622. },
  623. },
  624. UserID: "test",
  625. }).
  626. commonCount(&client.CommonCountRequest{
  627. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  628. TablePrefixWithSchema: tablePrefix,
  629. Version: "v1",
  630. }, &count).
  631. assertEqual(int64(0), count, "数量不一致")
  632. }
  633. func TestReply(t *testing.T) {
  634. initClient(t, "localhost:30170")
  635. defer destroyClient(t)
  636. tablePrefix := "test." + simpleUUID()[0:8]
  637. id := simpleUUID()
  638. name := simpleUUID()
  639. now := time.Now().Local()
  640. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  641. resultMap := make(map[string]any)
  642. newToolKit(t).
  643. autoMigrate(&client.AutoMigrateRequest{
  644. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  645. AutoMigrateItem: client.AutoMigrateItem{
  646. TablePrefixWithSchema: tablePrefix,
  647. Version: "v1",
  648. TableModelDescribe: tableModelDescribe,
  649. },
  650. }).
  651. insert(&client.InsertRequest{
  652. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  653. TablePrefixWithSchema: tablePrefix,
  654. Version: "v1",
  655. KeyColumns: []string{"id"},
  656. TableRow: map[string]any{
  657. "id": id,
  658. "name": name,
  659. "time": now,
  660. "table_num": tableNum,
  661. },
  662. UserID: "test",
  663. }).
  664. reply(&client.ReplayRequest{
  665. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  666. TablePrefixWithSchema: tablePrefix,
  667. Version: "v1",
  668. KeyValues: map[string]string{"id": id},
  669. UserID: "test",
  670. }).
  671. queryByKeys(&client.QueryByKeysRequest{
  672. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  673. TablePrefixWithSchema: tablePrefix,
  674. Version: "v1",
  675. KeyValues: map[string]string{"id": id},
  676. }, &resultMap).
  677. assertEqual(id, resultMap["id"], "ID不一致").
  678. assertEqual(name, resultMap["name"], "名称不一致").
  679. assertEqual(now.UnixMilli(), resultMap["time"].(time.Time).Local().UnixMilli(), "时间不一致").
  680. assertEqual(tableNum, resultMap["table_num"], "表数量不一致")
  681. }
  682. func TestEventQuery(t *testing.T) {
  683. initClient(t, "localhost:30170")
  684. defer destroyClient(t)
  685. tablePrefix := "test." + simpleUUID()[0:8]
  686. id := simpleUUID()
  687. name := simpleUUID()
  688. now := time.Now().Local()
  689. tableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  690. newName := simpleUUID()
  691. newNow := time.Now().Local()
  692. newTableNum := rand.New(rand.NewSource(now.Unix())).Intn(10)
  693. var totalCount int64
  694. eventInfos := make([]client.EventInfo, 0)
  695. newToolKit(t).
  696. autoMigrate(&client.AutoMigrateRequest{
  697. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  698. AutoMigrateItem: client.AutoMigrateItem{
  699. TablePrefixWithSchema: tablePrefix,
  700. Version: "v1",
  701. TableModelDescribe: tableModelDescribe,
  702. },
  703. }).
  704. insert(&client.InsertRequest{
  705. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  706. TablePrefixWithSchema: tablePrefix,
  707. Version: "v1",
  708. KeyColumns: []string{"id"},
  709. TableRow: map[string]any{
  710. "id": id,
  711. "name": name,
  712. "time": now,
  713. "table_num": tableNum,
  714. },
  715. UserID: "test",
  716. }).
  717. update(&client.UpdateRequest{
  718. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  719. TablePrefixWithSchema: tablePrefix,
  720. Version: "v1",
  721. KeyValues: map[string]string{"id": id},
  722. NewTableRow: map[string]any{
  723. "id": id,
  724. "name": newName,
  725. "time": newNow,
  726. "table_num": newTableNum,
  727. },
  728. UserID: "test",
  729. }).
  730. countEventByKeys(&client.CountEventByKeysRequest{
  731. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  732. TablePrefixWithSchema: tablePrefix,
  733. KeyValues: []string{id},
  734. }, &totalCount).
  735. assertEqual(2, int(totalCount), "总数不一致").
  736. commonCountEvent(&client.CommonCountEventRequest{
  737. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  738. TablePrefixWithSchema: tablePrefix,
  739. KeyValues: []string{id},
  740. Version: "v1",
  741. Operation: "create",
  742. CreatorID: "test",
  743. StartCreatedTime: now.Format(time.DateTime),
  744. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  745. }, &totalCount).
  746. assertEqual(1, int(totalCount), "总数不一致").
  747. commonCountEvent(&client.CommonCountEventRequest{
  748. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  749. TablePrefixWithSchema: tablePrefix,
  750. KeyValues: []string{id},
  751. Version: "v1",
  752. Operation: "update",
  753. CreatorID: "test",
  754. StartCreatedTime: now.Format(time.DateTime),
  755. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  756. }, &totalCount).
  757. assertEqual(1, int(totalCount), "总数不一致").
  758. eventQueryByKeys(&client.EventQueryByKeysRequest{
  759. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  760. TablePrefixWithSchema: tablePrefix,
  761. KeyValues: []string{id},
  762. PageNo: 0,
  763. PageSize: 0,
  764. }, &eventInfos, &totalCount).
  765. assertEqual(2, int(totalCount), "总数不一致").
  766. assertEqual(2, len(eventInfos), "事件数量不一致").
  767. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  768. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  769. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  770. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  771. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  772. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  773. assertEqual(id, eventInfos[1].Key, "关键字段不一致").
  774. assertEqual("v1", eventInfos[1].Version, "版本不一致").
  775. assertEqual("update", eventInfos[1].Operation, "操作不一致").
  776. assertEqual("test", eventInfos[1].CreatorID, "创建者ID不一致").
  777. assertNotEmpty(eventInfos[1].CreateTime, "创建事件为空").
  778. assertNotEmpty(eventInfos[1].Value, "值为空不一致").
  779. eventQueryByKeys(&client.EventQueryByKeysRequest{
  780. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  781. TablePrefixWithSchema: tablePrefix,
  782. KeyValues: []string{id},
  783. PageNo: 1,
  784. PageSize: 1,
  785. }, &eventInfos, &totalCount).
  786. assertEqual(2, int(totalCount), "总数不一致").
  787. assertEqual(1, len(eventInfos), "事件数量不一致").
  788. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  789. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  790. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  791. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  792. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  793. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  794. commonEventQuery(&client.CommonEventQueryRequest{
  795. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  796. TablePrefixWithSchema: tablePrefix,
  797. KeyValues: []string{id},
  798. Version: "v1",
  799. Operation: "create",
  800. CreatorID: "test",
  801. StartCreatedTime: now.Format(time.DateTime),
  802. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  803. PageNo: 0,
  804. PageSize: 0,
  805. }, &eventInfos, &totalCount).
  806. assertEqual(1, int(totalCount), "总数不一致").
  807. assertEqual(1, len(eventInfos), "事件数量不一致").
  808. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  809. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  810. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  811. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  812. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  813. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  814. commonEventQuery(&client.CommonEventQueryRequest{
  815. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  816. TablePrefixWithSchema: tablePrefix,
  817. KeyValues: []string{id},
  818. Version: "v1",
  819. Operation: "update",
  820. CreatorID: "test",
  821. StartCreatedTime: now.Format(time.DateTime),
  822. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  823. PageNo: 0,
  824. PageSize: 0,
  825. }, &eventInfos, &totalCount).
  826. assertEqual(1, int(totalCount), "总数不一致").
  827. assertEqual(1, len(eventInfos), "事件数量不一致").
  828. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  829. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  830. assertEqual("update", eventInfos[0].Operation, "操作不一致").
  831. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  832. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  833. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  834. delete(&client.DeleteRequest{
  835. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  836. TablePrefixWithSchema: tablePrefix,
  837. Version: "v1",
  838. KeyValues: map[string]string{"id": id},
  839. UserID: "test",
  840. }).
  841. countEventHistoryByKeys(&client.CountEventByKeysRequest{
  842. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  843. TablePrefixWithSchema: tablePrefix,
  844. KeyValues: []string{id},
  845. }, &totalCount).
  846. assertEqual(3, int(totalCount), "总数不一致").
  847. commonCountEventHistory(&client.CommonCountEventRequest{
  848. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  849. TablePrefixWithSchema: tablePrefix,
  850. KeyValues: []string{id},
  851. Version: "v1",
  852. Operation: "create",
  853. CreatorID: "test",
  854. StartCreatedTime: now.Format(time.DateTime),
  855. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  856. }, &totalCount).
  857. assertEqual(1, int(totalCount), "总数不一致").
  858. commonCountEventHistory(&client.CommonCountEventRequest{
  859. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  860. TablePrefixWithSchema: tablePrefix,
  861. KeyValues: []string{id},
  862. Version: "v1",
  863. Operation: "update",
  864. CreatorID: "test",
  865. StartCreatedTime: now.Format(time.DateTime),
  866. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  867. }, &totalCount).
  868. assertEqual(1, int(totalCount), "总数不一致").
  869. commonCountEventHistory(&client.CommonCountEventRequest{
  870. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  871. TablePrefixWithSchema: tablePrefix,
  872. KeyValues: []string{id},
  873. Version: "v1",
  874. Operation: "delete",
  875. CreatorID: "test",
  876. StartCreatedTime: now.Format(time.DateTime),
  877. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  878. }, &totalCount).
  879. assertEqual(1, int(totalCount), "总数不一致").
  880. eventHistoryQueryByKeys(&client.EventQueryByKeysRequest{
  881. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  882. TablePrefixWithSchema: tablePrefix,
  883. KeyValues: []string{id},
  884. PageNo: 0,
  885. PageSize: 0,
  886. }, &eventInfos, &totalCount).
  887. assertEqual(3, int(totalCount), "总数不一致").
  888. assertEqual(3, len(eventInfos), "事件数量不一致").
  889. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  890. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  891. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  892. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  893. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  894. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  895. assertEqual(id, eventInfos[1].Key, "关键字段不一致").
  896. assertEqual("v1", eventInfos[1].Version, "版本不一致").
  897. assertEqual("update", eventInfos[1].Operation, "操作不一致").
  898. assertEqual("test", eventInfos[1].CreatorID, "创建者ID不一致").
  899. assertNotEmpty(eventInfos[1].CreateTime, "创建事件为空").
  900. assertNotEmpty(eventInfos[1].Value, "值为空不一致").
  901. assertEqual(id, eventInfos[2].Key, "关键字段不一致").
  902. assertEqual("v1", eventInfos[2].Version, "版本不一致").
  903. assertEqual("delete", eventInfos[2].Operation, "操作不一致").
  904. assertEqual("test", eventInfos[2].CreatorID, "创建者ID不一致").
  905. assertNotEmpty(eventInfos[2].CreateTime, "创建事件为空").
  906. assertEqual("", eventInfos[2].Value, "值为空不一致").
  907. eventHistoryQueryByKeys(&client.EventQueryByKeysRequest{
  908. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  909. TablePrefixWithSchema: tablePrefix,
  910. KeyValues: []string{id},
  911. PageNo: 1,
  912. PageSize: 1,
  913. }, &eventInfos, &totalCount).
  914. assertEqual(3, int(totalCount), "总数不一致").
  915. assertEqual(1, len(eventInfos), "事件数量不一致").
  916. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  917. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  918. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  919. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  920. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  921. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  922. commonEventHistoryQuery(&client.CommonEventQueryRequest{
  923. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  924. TablePrefixWithSchema: tablePrefix,
  925. KeyValues: []string{id},
  926. Version: "v1",
  927. Operation: "create",
  928. CreatorID: "test",
  929. StartCreatedTime: now.Format(time.DateTime),
  930. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  931. PageNo: 0,
  932. PageSize: 0,
  933. }, &eventInfos, &totalCount).
  934. assertEqual(1, int(totalCount), "总数不一致").
  935. assertEqual(1, len(eventInfos), "事件数量不一致").
  936. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  937. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  938. assertEqual("create", eventInfos[0].Operation, "操作不一致").
  939. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  940. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  941. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  942. commonEventHistoryQuery(&client.CommonEventQueryRequest{
  943. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  944. TablePrefixWithSchema: tablePrefix,
  945. KeyValues: []string{id},
  946. Version: "v1",
  947. Operation: "update",
  948. CreatorID: "test",
  949. StartCreatedTime: now.Format(time.DateTime),
  950. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  951. PageNo: 0,
  952. PageSize: 0,
  953. }, &eventInfos, &totalCount).
  954. assertEqual(1, int(totalCount), "总数不一致").
  955. assertEqual(1, len(eventInfos), "事件数量不一致").
  956. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  957. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  958. assertEqual("update", eventInfos[0].Operation, "操作不一致").
  959. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  960. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  961. assertNotEmpty(eventInfos[0].Value, "值为空不一致").
  962. commonEventHistoryQuery(&client.CommonEventQueryRequest{
  963. DatabaseID: "2b78141779ee432295ca371b91c5cac7",
  964. TablePrefixWithSchema: tablePrefix,
  965. KeyValues: []string{id},
  966. Version: "v1",
  967. Operation: "delete",
  968. CreatorID: "test",
  969. StartCreatedTime: now.Format(time.DateTime),
  970. EndCreatedTime: now.Add(time.Second).Format(time.DateTime),
  971. PageNo: 0,
  972. PageSize: 0,
  973. }, &eventInfos, &totalCount).
  974. assertEqual(1, int(totalCount), "总数不一致").
  975. assertEqual(1, len(eventInfos), "事件数量不一致").
  976. assertEqual(id, eventInfos[0].Key, "关键字段不一致").
  977. assertEqual("v1", eventInfos[0].Version, "版本不一致").
  978. assertEqual("delete", eventInfos[0].Operation, "操作不一致").
  979. assertEqual("test", eventInfos[0].CreatorID, "创建者ID不一致").
  980. assertNotEmpty(eventInfos[0].CreateTime, "创建事件为空").
  981. assertEqual("", eventInfos[0].Value, "值为空不一致")
  982. }