v1_test.go 31 KB

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