-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathsqlite.rs
More file actions
54 lines (51 loc) · 1.41 KB
/
sqlite.rs
File metadata and controls
54 lines (51 loc) · 1.41 KB
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
use deadpool_sqlite::{Config, InteractError, Pool, Runtime};
fn create_pool() -> Pool {
let cfg = Config {
path: "db.sqlite3".into(),
pool: None,
open_flags: None,
};
cfg.create_pool(Runtime::Tokio1).unwrap()
}
#[tokio::test]
async fn basic() {
let pool = create_pool();
let conn = pool.get().await.unwrap();
let result: i64 = conn
.interact(|conn| {
let mut stmt = conn.prepare("SELECT 1")?;
let mut rows = stmt.query([])?;
let row = rows.next()?.unwrap();
row.get(0)
})
.await
.unwrap()
.unwrap();
assert_eq!(result, 1);
}
#[tokio::test]
async fn panic() {
let pool = create_pool();
{
let conn = pool.get().await.unwrap();
let result = conn
.interact::<_, ()>(|_| {
panic!("Whopsies!");
})
.await;
assert!(matches!(result, Err(InteractError::Panic(_))))
}
// The previous callback panicked. The pool should recover from this.
let conn = pool.get().await.unwrap();
let result: i64 = conn
.interact(|conn| {
let mut stmt = conn.prepare("SELECT 1").unwrap();
let mut rows = stmt.query([]).unwrap();
let row = rows.next().unwrap().unwrap();
row.get(0)
})
.await
.unwrap()
.unwrap();
assert_eq!(result, 1);
}