v1_test.go 32 KB

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