#![allow(dead_code)]
use serde_json::json;
use yang_db::Database;
const TEST_DB_URL: &str = "mysql://root:111111@localhost:3306/test";
#[tokio::test]
async fn test_small_batch_insert() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_small_batch";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=100 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 小批量插入成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 100, "应该插入 100 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 100, "应该有 100 条记录");
println!("✓ 验证: {} 条记录", c);
}
}
Err(e) => {
println!("小批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 小批量插入测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_medium_batch_insert() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_medium_batch";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=500 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 中等批量插入成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 500, "应该插入 500 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 500, "应该有 500 条记录");
println!("✓ 验证: {} 条记录", c);
}
}
Err(e) => {
println!("中等批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 中等批量插入测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_large_batch_insert_1000() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_large_batch_1000";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=1000 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 大批量插入(1000条)成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 1000, "应该插入 1000 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 1000, "应该有 1000 条记录");
println!("✓ 验证: {} 条记录", c);
}
}
Err(e) => {
println!("大批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 大批量插入(1000条)测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_very_large_batch_insert_5000() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_very_large_batch_5000";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=5000 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
println!("✓ 生成 5000 条测试数据");
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 超大批量插入(5000条)成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 5000, "应该插入 5000 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 5000, "应该有 5000 条记录");
println!("✓ 验证: {} 条记录", c);
}
println!("✓ 数据完整性验证通过");
}
Err(e) => {
println!("超大批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 超大批量插入(5000条)测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_extreme_large_batch_insert_10000() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_extreme_large_batch_10000";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=10000 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
println!("✓ 生成 10000 条测试数据");
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 极大批量插入(10000条)成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 10000, "应该插入 10000 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 10000, "应该有 10000 条记录");
println!("✓ 验证: {} 条记录", c);
}
println!("✓ 数据完整性验证通过");
}
Err(e) => {
println!("极大批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 极大批量插入(10000条)测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_irregular_batch_insert() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_irregular_batch";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
value INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=1234 {
records.push(json!({
"name": format!("用户{}", i),
"value": i * 10
}));
}
let start = std::time::Instant::now();
let batch_result = db.table(table_name).insert_batch(&records).await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 不规则批量插入(1234条)成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 1234, "应该插入 1234 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 1234, "应该有 1234 条记录");
println!("✓ 验证: {} 条记录", c);
}
}
Err(e) => {
println!("不规则批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 不规则批量插入测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}
#[tokio::test]
async fn test_large_batch_insert_with_json() {
let result = Database::connect(TEST_DB_URL).await;
if let Ok(db) = result {
let table_name = "test_large_batch_json";
let _ = db.drop_table(table_name).await;
let create_result = db
.create_table(&format!(
"CREATE TABLE {} (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
metadata JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)",
table_name
))
.await;
if create_result.is_err() {
println!("警告: 无法创建测试表");
return;
}
println!("✓ 测试表创建成功");
let mut records = Vec::new();
for i in 1..=2000 {
records.push(json!({
"name": format!("用户{}", i),
"metadata": {
"age": 20 + (i % 50),
"city": format!("城市{}", i % 10),
"tags": vec![format!("标签{}", i % 5), format!("标签{}", i % 3)]
}
}));
}
println!("✓ 生成 2000 条带 JSON 字段的测试数据");
let start = std::time::Instant::now();
let batch_result = db
.table(table_name)
.json("metadata")
.insert_batch(&records)
.await;
let duration = start.elapsed();
match batch_result {
Ok(affected) => {
println!(
"✓ 带 JSON 字段的大批量插入成功,影响 {} 行,耗时: {:?}",
affected, duration
);
assert_eq!(affected, 2000, "应该插入 2000 条记录");
let count: Result<i64, _> = db.table(table_name).count().await;
if let Ok(c) = count {
assert_eq!(c, 2000, "应该有 2000 条记录");
println!("✓ 验证: {} 条记录", c);
}
}
Err(e) => {
println!("带 JSON 字段的大批量插入失败: {}", e);
panic!("测试失败");
}
}
let _ = db.drop_table(table_name).await;
println!("\n✓✓✓ 带 JSON 字段的大批量插入测试通过 ✓✓✓");
} else {
println!("警告: 无法连接到测试数据库,跳过测试");
}
}