v1_test.go 31 KB

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