mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-27 13:04:20 +01:00
118 lines
3.1 KiB
Rust
118 lines
3.1 KiB
Rust
use tokio::fs;
|
|
use turso::{Builder, Value};
|
|
|
|
#[tokio::test]
|
|
async fn test_rows_next() {
|
|
let builder = Builder::new_local(":memory:");
|
|
let db = builder.build().await.unwrap();
|
|
let conn = db.connect().unwrap();
|
|
conn.execute("CREATE TABLE test (x INTEGER)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.execute("INSERT INTO test (x) VALUES (1)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.execute("INSERT INTO test (x) VALUES (2)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.execute(
|
|
"INSERT INTO test (x) VALUES (:x)",
|
|
vec![(":x".to_string(), Value::Integer(3))],
|
|
)
|
|
.await
|
|
.unwrap();
|
|
conn.execute(
|
|
"INSERT INTO test (x) VALUES (@x)",
|
|
vec![("@x".to_string(), Value::Integer(4))],
|
|
)
|
|
.await
|
|
.unwrap();
|
|
conn.execute(
|
|
"INSERT INTO test (x) VALUES ($x)",
|
|
vec![("$x".to_string(), Value::Integer(5))],
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let mut res = conn.query("SELECT * FROM test", ()).await.unwrap();
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
1.into()
|
|
);
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
2.into()
|
|
);
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
3.into()
|
|
);
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
4.into()
|
|
);
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
5.into()
|
|
);
|
|
assert!(res.next().await.unwrap().is_none());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_cacheflush() {
|
|
let builder = Builder::new_local("test.db");
|
|
let db = builder.build().await.unwrap();
|
|
|
|
let conn = db.connect().unwrap();
|
|
|
|
conn.execute("CREATE TABLE IF NOT EXISTS asdf (x INTEGER)", ())
|
|
.await
|
|
.unwrap();
|
|
|
|
// Tests if cache flush breaks transaction isolation
|
|
conn.execute("BEGIN", ()).await.unwrap();
|
|
conn.execute("INSERT INTO asdf (x) VALUES (1)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.cacheflush().unwrap();
|
|
conn.execute("ROLLBACK", ()).await.unwrap();
|
|
|
|
conn.execute("INSERT INTO asdf (x) VALUES (2)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.execute("INSERT INTO asdf (x) VALUES (3)", ())
|
|
.await
|
|
.unwrap();
|
|
|
|
let mut res = conn.query("SELECT * FROM asdf", ()).await.unwrap();
|
|
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
2.into()
|
|
);
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
3.into()
|
|
);
|
|
|
|
// Tests if cache flush doesn't break a committed transaction
|
|
conn.execute("BEGIN", ()).await.unwrap();
|
|
conn.execute("INSERT INTO asdf (x) VALUES (1)", ())
|
|
.await
|
|
.unwrap();
|
|
conn.cacheflush().unwrap();
|
|
conn.execute("COMMIT", ()).await.unwrap();
|
|
|
|
let mut res = conn
|
|
.query("SELECT * FROM asdf WHERE x = 1", ())
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(
|
|
res.next().await.unwrap().unwrap().get_value(0).unwrap(),
|
|
1.into()
|
|
);
|
|
|
|
fs::remove_file("test.db").await.unwrap();
|
|
fs::remove_file("test.db-wal").await.unwrap();
|
|
}
|