instance_test.go 30 KB

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