v1_test.go 31 KB

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