mirror of
https://github.com/fluencelabs/examples
synced 2025-04-25 10:42:16 +00:00
SQLite: add test for last_insert_rowid (#8)
This commit is contained in:
parent
a66ccd6514
commit
b759aa746c
@ -13,3 +13,5 @@ path = "src/main.rs"
|
||||
marine-rs-sdk = "0.6.10"
|
||||
marine-sqlite-connector = "0.5.0"
|
||||
|
||||
[dev-dependencies]
|
||||
marine-rs-sdk-test = "0.1.11"
|
||||
|
@ -18,7 +18,7 @@ use marine_rs_sdk::marine;
|
||||
use marine_rs_sdk::module_manifest;
|
||||
|
||||
use marine_sqlite_connector;
|
||||
use marine_sqlite_connector::State;
|
||||
use marine_sqlite_connector::{State, Value};
|
||||
|
||||
module_manifest!();
|
||||
|
||||
@ -105,3 +105,47 @@ pub fn test3() {
|
||||
|
||||
println!("table size is: {:?}", cursor.count());
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn test_last_rowid() -> i64 {
|
||||
let db_path = "/tmp/users.sqlite";
|
||||
let connection = marine_sqlite_connector::open(db_path).expect("db should be opened");
|
||||
|
||||
connection.execute(
|
||||
"CREATE TABLE IF NOT EXISTS users (id integer not null primary key AUTOINCREMENT, name TEXT, age INTEGER);"
|
||||
).expect("table should be created successfully");
|
||||
|
||||
connection
|
||||
.execute(
|
||||
"
|
||||
INSERT INTO users (name, age) VALUES ('Alice', 42);
|
||||
INSERT INTO users (name, age) VALUES ('Bob', 69);
|
||||
",
|
||||
)
|
||||
.expect("insert");
|
||||
|
||||
let stmt = connection
|
||||
.prepare("SELECT last_insert_rowid();")
|
||||
.expect("select");
|
||||
|
||||
let mut cursor = stmt.cursor();
|
||||
match cursor.next() {
|
||||
Ok(Some(&[Value::Integer(id)])) => return id,
|
||||
other => panic!(
|
||||
"Expected integer to be returned from last_insert_rowid, got {:?}",
|
||||
other
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use marine_rs_sdk_test::marine_test;
|
||||
use marine_rs_sdk_test::CallParameters;
|
||||
use marine_rs_sdk_test::SecurityTetraplet;
|
||||
|
||||
#[marine_test(config_path = "../Config.toml", modules_dir = "../artifacts")]
|
||||
fn test() {
|
||||
assert!(sqlite_test.test_last_rowid() > 0);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user