-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsql_executor_test.rs
345 lines (283 loc) · 10.4 KB
/
sql_executor_test.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Sql executor test case
//!
//! test case:
//! 1. create sqlite db at local path
//! 1. test sql connection
//! 1. test save: fail_if_exists
//! 1. test save: replace
//! 1. test save: append
//! 1. test save: upsert
//! 1. test delete
use std::str::FromStr;
use ::fabrix_sql::*;
use fabrix_core::*;
const CONN1: &str = "mysql://root:secret@localhost:3306/dev";
const CONN2: &str = "postgres://root:secret@localhost:5432/dev";
const CONN3: &str = "sqlite://dev.sqlite";
const TABLE_NAME: &str = "dev";
/*
To create a new sqlite database, run the following commands:
(project_root/fabrix/dev.sqlite)
cargo test --package fabrix --test sql_executor_test -- create_sqlite_db_success --exact --nocapture
*/
#[tokio::test]
async fn create_sqlite_db_success() -> anyhow::Result<()> {
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode};
use sqlx::ConnectOptions;
use std::str::FromStr;
let conn = SqliteConnectOptions::from_str(CONN3)?
.journal_mode(SqliteJournalMode::Wal)
.create_if_missing(true)
.connect()
.await;
assert!(conn.is_ok(), "create local sqlite db should not fail");
Ok(())
}
/*
cargo test --package fabrix --test sql_executor_test -- connection_success --exact --nocapture
*/
#[tokio::test]
async fn connection_success() {
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
let r1 = exc1.connect().await;
assert!(r1.is_ok(), "connection should not fail");
let r2 = exc2.connect().await;
assert!(r2.is_ok(), "connection should not fail");
let r3 = exc3.connect().await;
assert!(r3.is_ok(), "connection should not fail");
}
/*
cargo test --package fabrix --test sql_executor_test -- save_fail_if_exists_success --exact --nocapture
*/
#[tokio::test]
async fn save_fail_if_exists_success() {
let df = fx![
"ord";
"names" => ["Jacob", "Sam", "James", "Lucas", "Mia"],
"ord" => [10,11,12,20,22],
"val" => [Some(10.1), None, Some(8.0), Some(9.5), Some(10.8)],
"dt" => [
datetime!(2016, 1, 8, 9, 10, 11),
datetime!(2017, 1, 7, 9, 10, 11),
datetime!(2018, 1, 6, 9, 10, 11),
datetime!(2019, 1, 5, 9, 10, 11),
datetime!(2020, 1, 4, 9, 10, 11),
]
]
.unwrap();
let save_strategy = sql_adt::SaveStrategy::FailIfExists;
// mysql
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
exc1.connect().await.expect("connection is ok");
let res1 = exc1.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res1.is_ok(), "saving to table should not fail");
// postgres
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
exc2.connect().await.expect("connection is ok");
let res2 = exc2.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res2.is_ok(), "saving to table should not fail");
// sqlite
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
exc3.connect().await.expect("connection is ok");
let res3 = exc3.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res3.is_ok(), "saving to table should not fail");
}
/*
cargo test --package fabrix --test sql_executor_test -- save_replace_success --exact --nocapture
*/
#[tokio::test]
async fn save_replace_success() {
// df
let df = fx![
"ord";
"names" => ["Jacob", "Sam", "James", "Lucas", "Mia", "Livia"],
"ord" => [10,11,12,20,22,31],
"val" => [Some(10.1), None, Some(8.0), Some(9.5), Some(10.8), Some(11.2)],
"note" => [Some("FS"), Some("OP"), Some("TEC"), None, Some("SS"), None],
"dt" => [
datetime!(2016, 1, 8, 9, 10, 11),
datetime!(2017, 1, 7, 9, 10, 11),
datetime!(2018, 1, 6, 9, 10, 11),
datetime!(2019, 1, 5, 9, 10, 11),
datetime!(2020, 1, 4, 9, 10, 11),
datetime!(2020, 1, 3, 9, 10, 11),
]
]
.unwrap();
let save_strategy = sql_adt::SaveStrategy::Replace;
// mysql
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
exc1.connect().await.expect("connection is ok");
let res1 = exc1.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res1.is_ok(), "saving to table should not fail");
// postgres
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
exc2.connect().await.expect("connection is ok");
let res2 = exc2.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res2.is_ok(), "saving to table should not fail");
// sqlite
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
exc3.connect().await.expect("connection is ok");
let res3 = exc3.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res3.is_ok(), "saving to table should not fail");
}
/*
cargo test --package fabrix --test sql_executor_test -- save_append_success --exact --nocapture
*/
#[tokio::test]
async fn save_append_success() {
// df
let df = fx![
"ord";
"names" => ["Fila", "Ada", "Kevin"],
"ord" => [25,17,32],
"val" => [None, Some(7.1), Some(2.4)],
"note" => [Some(""), Some("M"), None],
"dt" => [
datetime!(2010, 2, 5, 9, 10, 11),
datetime!(2011, 2, 4, 9, 10, 11),
datetime!(2012, 2, 3, 9, 10, 11),
]
]
.unwrap();
let save_strategy = sql_adt::SaveStrategy::Append;
// mysql
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
exc1.connect().await.expect("connection is ok");
let res1 = exc1.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res1.is_ok(), "saving to table should not fail");
// postgres
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
exc2.connect().await.expect("connection is ok");
let res2 = exc2.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res2.is_ok(), "saving to table should not fail");
// sqlite
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
exc3.connect().await.expect("connection is ok");
let res3 = exc3.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res3.is_ok(), "saving to table should not fail");
}
/*
cargo test --package fabrix --test sql_executor_test -- save_upsert_success --exact --nocapture
*/
#[tokio::test]
async fn save_upsert_success() {
// df
let df = fx![
"ord";
"ord" => [10,15,20],
"val" => [Some(12.7), Some(7.1), Some(8.9)],
]
.unwrap();
let save_strategy = sql_adt::SaveStrategy::Upsert;
// mysql
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
exc1.connect().await.expect("connection is ok");
let res1 = exc1.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res1.is_ok(), "saving to table should not fail");
// postgres
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
exc2.connect().await.expect("connection is ok");
let res2 = exc2.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res2.is_ok(), "saving to table should not fail");
// sqlite
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
exc3.connect().await.expect("connection is ok");
let res3 = exc3.save(TABLE_NAME, df.clone(), &save_strategy).await;
assert!(res3.is_ok(), "saving to table should not fail");
}
/*
cargo test --package fabrix --test sql_executor_test -- delete_success --exact --nocapture
*/
#[tokio::test]
async fn delete_success() {
let filter = xpr!([
xpr!("ord", "=", 15),
xpr_or!(),
xpr!([
xpr!("names", "=", "Livia"),
xpr_and!(),
xpr!("val", ">=", 10.0)
])
]);
let delete = sql_adt::Delete {
table: TABLE_NAME.to_owned(),
filter,
};
// mysql
let mut exc1 = SqlExecutor::<DatabaseMysql>::from_str(CONN1).unwrap();
exc1.connect().await.expect("connection is ok");
let res1 = exc1.delete(&delete).await;
assert!(
res1.is_ok(),
"deleting from an existing table should not fail"
);
// postgres
let mut exc2 = SqlExecutor::<DatabasePg>::from_str(CONN2).unwrap();
exc2.connect().await.expect("connection is ok");
let res2 = exc2.delete(&delete).await;
assert!(
res2.is_ok(),
"deleting from an existing table should not fail"
);
// sqlite
let mut exc3 = SqlExecutor::<DatabaseSqlite>::from_str(CONN3).unwrap();
exc3.connect().await.expect("connection is ok");
let res3 = exc3.delete(&delete).await;
assert!(
res3.is_ok(),
"deleting from an existing table should not fail"
);
}
/*
cargo test --package fabrix --test sql_executor_test -- transaction_success --exact --nocapture
Transaction test:
The second create_table will fail, and the first create_table will be rolled back.
*/
#[tokio::test]
async fn transaction_success() {
let df = fx!(
"id";
"id" => [1,2,3,4,5,6,7,8,9,10],
"name" => ["A","B","C","D","E","F","G","H","I","J"],
"dt" => [
datetime!(2010, 2, 5, 9, 10, 11),
datetime!(2011, 2, 4, 9, 10, 11),
datetime!(2012, 2, 3, 9, 10, 11),
datetime!(2013, 2, 2, 9, 10, 11),
datetime!(2014, 2, 1, 9, 10, 11),
datetime!(2015, 2, 1, 9, 10, 11),
datetime!(2016, 2, 1, 9, 10, 11),
datetime!(2017, 2, 1, 9, 10, 11),
datetime!(2018, 2, 1, 9, 10, 11),
datetime!(2019, 2, 1, 9, 10, 11),
]
)
.unwrap();
let pool = sqlx::postgres::PgPool::connect(CONN2).await.unwrap();
let conn = DatabasePg::new_pg_pool(pool);
let mut txn = conn
.begin_transaction()
.await
.expect("begin transaction is ok");
let columns = df.fields();
let que = SqlBuilder::Postgres.create_table("test_transaction", &columns, None, None);
txn.execute(&que).await.unwrap();
let assert_value;
let res = match txn.execute(&que).await {
Ok(_) => {
println!("success");
assert_value = 1;
txn.commit().await
}
Err(_) => {
println!("fail and rollback");
assert_value = 2;
txn.rollback().await
}
};
assert!(res.is_ok(), "transaction execution should not fail");
assert_eq!(assert_value, 2);
}