#![cfg(feature = "std")]
extern crate alloc;
use alloc::string::String;
use alloc::vec::Vec;
use remdb::*;
use serial_test::serial;
static mut DB_MEMORY: [u8; 1024 * 1024] = [0u8; 1024 * 1024];
remdb::table!(
TEST_TABLE,
100, primary_key: id,
secondary_index: name,
fields: {
id: i32,
name: str(32), age: i8,
active: bool,
created_at: u64
}
);
remdb::database!(
TEST_DB,
tables: [TEST_TABLE]
);
#[test]
#[serial]
fn test_export_ddl() {
crate::transaction::reset_log_manager();
use std::fs::remove_file;
let _ = remove_file("./wal");
unsafe {
crate::memory::allocator::init_global_allocator(DB_MEMORY.as_mut_ptr(), DB_MEMORY.len())
.unwrap();
}
let db = init_global_db(&TEST_DB).unwrap();
let ddl_path = "test_ddl.sql";
let result = db.export_ddl(ddl_path);
assert!(result.is_ok(), "Failed to export DDL");
use std::fs::File;
use std::io::Read;
let mut file = File::open(ddl_path).expect("Failed to open DDL file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read DDL file");
println!(
"Generated DDL:
{}",
contents
);
assert!(
contents.contains("CREATE TABLE test_table"),
"DDL file should contain CREATE TABLE statement"
);
assert!(
contents.contains("PRIMARY KEY"),
"DDL file should contain primary key definition"
);
assert!(
contents.contains("name"),
"DDL file should contain name field definition"
);
assert!(
contents.contains("CREATE INDEX"),
"DDL file should contain CREATE INDEX statement"
);
std::fs::remove_file(ddl_path).expect("Failed to remove DDL file");
reset_global_db();
crate::memory::allocator::reset_global_allocator().unwrap();
}
#[test]
#[serial]
fn test_export_data() {
crate::transaction::reset_log_manager();
use std::fs::remove_file;
let _ = remove_file("./wal");
unsafe {
crate::memory::allocator::init_global_allocator(DB_MEMORY.as_mut_ptr(), DB_MEMORY.len())
.unwrap();
}
let db = init_global_db(&TEST_DB).unwrap();
let result = db.sql_query("INSERT INTO TEST_TABLE (id, name, age, active, created_at) VALUES (1, 'Test User', 25, true, 1234567890)");
assert!(result.is_ok(), "插入测试数据应该成功");
let data_path = "test_data.sql";
let result = db.export_data(data_path);
assert!(result.is_ok(), "Failed to export data");
use std::fs::File;
use std::io::Read;
let mut file = File::open(data_path).expect("Failed to open data file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read data file");
println!(
"Generated data:
{}",
contents
);
assert!(
contents.contains("INSERT INTO test_table"),
"Data file should contain INSERT statement"
);
assert!(
contents.contains("1"),
"Data file should contain correct record values"
);
std::fs::remove_file(data_path).expect("Failed to remove data file");
reset_global_db();
crate::memory::allocator::reset_global_allocator().unwrap();
}
#[test]
#[serial]
fn test_export_empty_table() {
crate::transaction::reset_log_manager();
use std::fs::remove_file;
let _ = remove_file("./wal");
unsafe {
crate::memory::allocator::init_global_allocator(DB_MEMORY.as_mut_ptr(), DB_MEMORY.len())
.unwrap();
}
let db = init_global_db(&TEST_DB).unwrap();
let ddl_path = "test_empty_ddl.sql";
let result = db.export_ddl(ddl_path);
assert!(result.is_ok(), "Failed to export DDL for empty table");
let data_path = "test_empty_data.sql";
let result = db.export_data(data_path);
assert!(result.is_ok(), "Failed to export data for empty table");
use std::fs::File;
use std::io::Read;
let mut file = File::open(data_path).expect("Failed to open empty data file");
let mut contents = String::new();
file.read_to_string(&mut contents)
.expect("Failed to read empty data file");
assert!(
contents.trim().is_empty(),
"Data file for empty table should be empty"
);
std::fs::remove_file(ddl_path).expect("Failed to remove empty DDL file");
std::fs::remove_file(data_path).expect("Failed to remove empty data file");
reset_global_db();
crate::memory::allocator::reset_global_allocator().unwrap();
}