forked from datafuse-extras/test-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
109 lines (89 loc) · 3.45 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::vec;
use anyhow::Result;
use databend_driver::Client;
use util::ConnectionExt;
mod util;
#[tokio::main]
async fn main() -> Result<()> {
let dsn = std::env::var("DATABEND_DSN").unwrap_or(
"databend://root:@localhost:8000/default?sslmode=disable&enable_experimental_merge_into=1"
.to_owned(),
);
let client = Client::new(dsn);
let c1 = client.get_conn().await.unwrap();
let c2 = client.get_conn().await.unwrap();
let select_t = "SELECT * FROM t ORDER BY c;";
c1.exec("CREATE OR REPLACE TABLE t(c int);").await?;
// c1 commit failed due to c2 has modified the data
c1.begin().await?;
c1.exec("INSERT INTO t VALUES(1);").await?;
c1.assert_query(select_t, vec![(1,)]).await;
c2.assert_query::<(i32,)>(select_t, vec![]).await;
c2.begin().await?;
c2.exec("INSERT INTO t VALUES(2);").await?;
c1.assert_query(select_t, vec![(1,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
c2.commit().await?;
c1.assert_query(select_t, vec![(1,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
let result = c1.commit().await;
assert!(result.is_err());
c1.assert_query(select_t, vec![(2,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
// rollback
c1.begin().await?;
c1.exec("INSERT INTO t VALUES(1);").await?;
let result = c1.exec("qwerty").await;
assert!(result.is_err());
c1.commit().await?;
c1.assert_query(select_t, vec![(2,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
// rollback
c1.exec("drop table if exists t1;").await?;
c1.begin().await?;
c1.exec("INSERT INTO t VALUES(1);").await?;
let result = c1.exec("select * from t1").await;
assert!(result.is_err());
c1.commit().await?;
c1.assert_query(select_t, vec![(2,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
//stream
c1.exec("create or replace table base(c int);").await?;
c1.exec("CREATE or replace STREAM s ON TABLE base APPEND_ONLY=true;")
.await?;
c1.begin().await?;
c1.exec("INSERT INTO base VALUES(1);").await?;
// First time query stream s
c1.assert_query("SELECT c FROM s;", vec![(1,)]).await;
c2.begin().await?;
c2.exec("INSERT INTO base VALUES(2);").await?;
c2.commit().await?;
// Second time query stream s
c1.assert_query("SELECT c FROM s;", vec![(1,)]).await;
c1.exec("Insert into base values(3);").await?;
// Third time query stream s
c1.assert_query("SELECT c FROM s;", vec![(1,)]).await;
let result = c1.commit().await;
assert!(result.is_err());
// no conflict, both commit success
c1.assert_query(select_t, vec![(2,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
c1.exec("CREATE OR REPLACE TABLE t1(c int);").await?;
let select_t1 = "SELECT * FROM t1 ORDER BY c;";
c1.begin().await?;
c1.exec("INSERT INTO t VALUES(1);").await?;
c1.assert_query(select_t, vec![(1,), (2,)]).await;
c2.assert_query(select_t, vec![(2,)]).await;
c2.begin().await?;
c2.exec("INSERT INTO t1 VALUES(3);").await?;
c1.assert_query::<(i32,)>(select_t1, vec![]).await;
c2.assert_query(select_t1, vec![(3,)]).await;
c2.commit().await?;
c1.commit().await?;
c1.assert_query(select_t, vec![(1,), (2,)]).await;
c2.assert_query(select_t, vec![(1,), (2,)]).await;
c1.assert_query(select_t1, vec![(3,)]).await;
c2.assert_query(select_t1, vec![(3,)]).await;
println!("All tests passed!");
Ok(())
}