#![cfg(not(target_arch = "wasm32"))]
use absurder_sql::storage::export::validate_sqlite_file;
use absurder_sql::storage::import::import_database_from_bytes;
use absurder_sql::storage::vfs_sync::with_global_storage;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_import_from_rusqlite_file() {
let temp_dir = TempDir::new().expect("Should create temp dir");
let db_path = temp_dir.path().join("rusqlite.db");
{
let conn = rusqlite::Connection::open(&db_path).expect("Should create with rusqlite");
conn.execute("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)", [])
.expect("Should create table");
conn.execute(
"INSERT INTO test (value) VALUES (?1), (?2)",
["Hello", "World"],
)
.expect("Should insert data");
}
let file_bytes = fs::read(&db_path).expect("Should read file");
println!("rusqlite created file: {} bytes", file_bytes.len());
validate_sqlite_file(&file_bytes).expect("Should be valid SQLite");
futures::executor::block_on(import_database_from_bytes(
"imported_from_rusqlite",
file_bytes,
))
.expect("Should import");
with_global_storage(|gs| {
#[cfg(target_arch = "wasm32")]
let storage_map = gs;
#[cfg(not(target_arch = "wasm32"))]
let storage_map = gs.borrow();
let blocks = storage_map
.get("imported_from_rusqlite")
.expect("Should have blocks");
assert!(!blocks.is_empty(), "Should have imported blocks");
let header = blocks.get(&0).expect("Should have header");
assert_eq!(&header[0..15], b"SQLite format 3");
});
println!("Successfully imported rusqlite-created file");
}
#[test]
fn test_import_rusqlite_with_multiple_tables() {
let temp_dir = TempDir::new().expect("Should create temp dir");
let db_path = temp_dir.path().join("complex.db");
{
let conn = rusqlite::Connection::open(&db_path).expect("Should create database");
conn.execute(
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)",
[],
)
.expect("Should create users table");
conn.execute(
"CREATE TABLE posts (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT, content TEXT)",
[],
).expect("Should create posts table");
conn.execute(
"INSERT INTO users (name, email) VALUES (?1, ?2), (?3, ?4)",
[
"Alice O'Brien",
"alice@example.com",
"Bob \"The Builder\"",
"bob@example.com",
],
)
.expect("Should insert users");
conn.execute(
"INSERT INTO posts (user_id, title, content) VALUES (1, 'Hello World', 'This is a test post with ''quotes'' and\nnewlines')",
[],
).expect("Should insert post");
}
let file_bytes = fs::read(&db_path).expect("Should read file");
println!("Complex database: {} bytes", file_bytes.len());
validate_sqlite_file(&file_bytes).expect("Should be valid");
futures::executor::block_on(import_database_from_bytes("complex_db", file_bytes))
.expect("Should import complex database");
with_global_storage(|gs| {
#[cfg(target_arch = "wasm32")]
let storage_map = gs;
#[cfg(not(target_arch = "wasm32"))]
let storage_map = gs.borrow();
let blocks = storage_map.get("complex_db").expect("Should have blocks");
assert!(!blocks.is_empty(), "Should have multiple blocks");
let header = blocks.get(&0).expect("Should have header");
assert_eq!(&header[0..15], b"SQLite format 3");
});
println!("Successfully imported complex rusqlite database with multiple tables");
}