use sqltool::{DataTransfer, StructureMigration, create_connection, DatabaseType, FieldMapping};
#[tokio::test]
async fn test_full_structure_data_transfer() {
let source_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
source_conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)").await.unwrap();
source_conn.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')").await.unwrap();
source_conn.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')").await.unwrap();
let target_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
target_conn.execute("CREATE TABLE users_copy (id INTEGER PRIMARY KEY, name TEXT, email TEXT)").await.unwrap();
let transfer = DataTransfer::new(source_conn, target_conn);
let mappings = vec![
FieldMapping {
source_table: "users".to_string(),
source_field: "id".to_string(),
target_table: "users_copy".to_string(),
target_field: "id".to_string(),
},
FieldMapping {
source_table: "users".to_string(),
source_field: "name".to_string(),
target_table: "users_copy".to_string(),
target_field: "name".to_string(),
},
FieldMapping {
source_table: "users".to_string(),
source_field: "email".to_string(),
target_table: "users_copy".to_string(),
target_field: "email".to_string(),
},
];
transfer.transfer(mappings).await.unwrap();
println!("Full structure and data transfer test completed");
}
#[tokio::test]
async fn test_only_structure_transfer() {
let source_db_path = ":memory:";
let target_db_path = ":memory:";
let source_conn = create_connection(
DatabaseType::SQLite,
&format!("sqlite://{}", source_db_path)
).await.unwrap();
let target_conn = create_connection(
DatabaseType::SQLite,
&format!("sqlite://{}", target_db_path)
).await.unwrap();
source_conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)").await.unwrap();
let migration = StructureMigration::new(source_conn, target_conn);
migration.migrate_structure("users", "users_copy").await.unwrap();
println!("Only structure transfer test completed");
}
#[tokio::test]
async fn test_only_data_transfer() {
let source_db_path = ":memory:";
let target_db_path = ":memory:";
let source_conn = create_connection(
DatabaseType::SQLite,
&format!("sqlite://{}", source_db_path)
).await.unwrap();
let target_conn = create_connection(
DatabaseType::SQLite,
&format!("sqlite://{}", target_db_path)
).await.unwrap();
source_conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)").await.unwrap();
source_conn.execute("INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')").await.unwrap();
source_conn.execute("INSERT INTO users (name, email) VALUES ('Bob', 'bob@example.com')").await.unwrap();
target_conn.execute("CREATE TABLE users_copy (id INTEGER PRIMARY KEY, name TEXT, email TEXT)").await.unwrap();
let transfer = DataTransfer::new(source_conn, target_conn);
let mappings = vec![
FieldMapping {
source_table: "users".to_string(),
source_field: "id".to_string(),
target_table: "users_copy".to_string(),
target_field: "id".to_string(),
},
FieldMapping {
source_table: "users".to_string(),
source_field: "name".to_string(),
target_table: "users_copy".to_string(),
target_field: "name".to_string(),
},
FieldMapping {
source_table: "users".to_string(),
source_field: "email".to_string(),
target_table: "users_copy".to_string(),
target_field: "email".to_string(),
},
];
transfer.transfer(mappings).await.unwrap();
println!("Only data transfer test completed");
}
#[tokio::test]
async fn test_related_tables_transfer() {
let source_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
source_conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").await.unwrap();
source_conn.execute("CREATE TABLE posts (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT, FOREIGN KEY (user_id) REFERENCES users(id))").await.unwrap();
source_conn.execute("INSERT INTO users (name) VALUES ('Alice'), ('Bob')").await.unwrap();
source_conn.execute("INSERT INTO posts (user_id, title) VALUES (1, 'Post 1'), (1, 'Post 2'), (2, 'Post 3')").await.unwrap();
let target_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
target_conn.execute("CREATE TABLE users_copy (id INTEGER PRIMARY KEY, name TEXT)").await.unwrap();
target_conn.execute("CREATE TABLE posts_copy (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT, FOREIGN KEY (user_id) REFERENCES users_copy(id))").await.unwrap();
let transfer = DataTransfer::new(source_conn, target_conn);
let user_mappings = vec![
FieldMapping {
source_table: "users".to_string(),
source_field: "id".to_string(),
target_table: "users_copy".to_string(),
target_field: "id".to_string(),
},
FieldMapping {
source_table: "users".to_string(),
source_field: "name".to_string(),
target_table: "users_copy".to_string(),
target_field: "name".to_string(),
},
];
transfer.transfer(user_mappings).await.unwrap();
let source_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
source_conn.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").await.unwrap();
source_conn.execute("CREATE TABLE posts (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT, FOREIGN KEY (user_id) REFERENCES users(id))").await.unwrap();
source_conn.execute("INSERT INTO users (name) VALUES ('Alice'), ('Bob')").await.unwrap();
source_conn.execute("INSERT INTO posts (user_id, title) VALUES (1, 'Post 1'), (1, 'Post 2'), (2, 'Post 3')").await.unwrap();
let target_conn = create_connection(
DatabaseType::SQLite,
"sqlite::memory:"
).await.unwrap();
target_conn.execute("CREATE TABLE users_copy (id INTEGER PRIMARY KEY, name TEXT)").await.unwrap();
target_conn.execute("CREATE TABLE posts_copy (id INTEGER PRIMARY KEY, user_id INTEGER, title TEXT, FOREIGN KEY (user_id) REFERENCES users_copy(id))").await.unwrap();
let transfer = DataTransfer::new(source_conn, target_conn);
let post_mappings = vec![
FieldMapping {
source_table: "posts".to_string(),
source_field: "id".to_string(),
target_table: "posts_copy".to_string(),
target_field: "id".to_string(),
},
FieldMapping {
source_table: "posts".to_string(),
source_field: "user_id".to_string(),
target_table: "posts_copy".to_string(),
target_field: "user_id".to_string(),
},
FieldMapping {
source_table: "posts".to_string(),
source_field: "title".to_string(),
target_table: "posts_copy".to_string(),
target_field: "title".to_string(),
},
];
transfer.transfer(post_mappings).await.unwrap();
println!("Related tables transfer test completed");
}