v1_test.go 32 KB

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