v1_test.go 32 KB

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