#![cfg(any(feature = "turso", feature = "postgresql", feature = "mysql"))]
mod _test_common;
define_test_user_simple!(ValidateTestUser, "validate_table_users_1");
#[cfg(any(feature = "turso", feature = "postgresql", feature = "mysql"))]
mod validate_table_tests {
use super::*;
use _test_common::{DbConfig, create_db_connection};
async fn test_validate_table_success_impl(
config: &DbConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let db = create_db_connection(config).await?;
db.drop_table::<ValidateTestUser>().execute().await.ok();
db.create_table::<ValidateTestUser>().execute().await?;
db.validate_table::<ValidateTestUser>().await?;
println!("validate_table succeeded for existing table");
db.drop_table::<ValidateTestUser>().execute().await?;
Ok(())
}
async fn test_validate_table_not_exists_impl(
config: &DbConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let db = create_db_connection(config).await?;
db.drop_table::<ValidateTestUser>().execute().await.ok();
let result = db.validate_table::<ValidateTestUser>().await;
assert!(
result.is_err(),
"validate_table should fail for non-existent table"
);
if let Err(ormer::Error::SchemaMismatch { table, reason }) = result {
assert_eq!(table, "validate_table_users_1");
assert!(
reason.contains("does not exist"),
"Error reason should mention table does not exist"
);
println!("Correctly detected non-existent table: {}", reason);
} else {
panic!("Expected SchemaMismatch error");
}
Ok(())
}
async fn test_create_table_without_validation_impl(
config: &DbConfig,
) -> Result<(), Box<dyn std::error::Error>> {
let db = create_db_connection(config).await?;
db.drop_table::<ValidateTestUser>().execute().await?;
db.create_table::<ValidateTestUser>().execute().await?;
println!("create_table succeeded without validation");
db.drop_table::<ValidateTestUser>().execute().await?;
Ok(())
}
test_on_all_dbs_result!(test_validate_table_success_impl);
test_on_all_dbs_result!(test_validate_table_not_exists_impl);
test_on_all_dbs_result!(test_create_table_without_validation_impl);
}