#![cfg(any(feature = "sqlite", feature = "postgresql", feature = "mysql"))]
use ormer::DbType;
pub type DbConfig = (DbType, &'static str);
#[allow(dead_code)]
#[allow(unused_mut)]
#[allow(clippy::vec_init_then_push)]
pub fn get_all_db_configs() -> Vec<DbConfig> {
let mut configs = Vec::new();
#[cfg(feature = "sqlite")]
configs.push((DbType::Sqlite, ":memory:"));
#[cfg(feature = "postgresql")]
configs.push((
DbType::PostgreSQL,
option_env!("ORMER_TEST_POSTGRES")
.unwrap_or("postgres://postgres:postgres@localhost:5432/ormer_test"),
));
#[cfg(feature = "mysql")]
configs.push((
DbType::MySQL,
option_env!("ORMER_TEST_MYSQL").unwrap_or("mysql://root:root@localhost:3306/ormer_test"),
));
configs
}
#[cfg(feature = "sqlite")]
#[allow(dead_code)]
pub fn get_sqlite_config() -> Vec<DbConfig> {
vec![(DbType::Sqlite, ":memory:")]
}
#[allow(dead_code)]
pub async fn create_db_connection(
config: &DbConfig,
) -> Result<ormer::Database, Box<dyn std::error::Error>> {
let db = ormer::Database::connect(config.0, config.1).await?;
Ok(db)
}
#[cfg(feature = "sqlite")]
#[allow(dead_code)]
pub fn sqlite_config() -> DbConfig {
(DbType::Sqlite, ":memory:")
}
#[cfg(feature = "postgresql")]
#[allow(dead_code)]
pub fn postgresql_config() -> DbConfig {
(
DbType::PostgreSQL,
option_env!("ORMER_TEST_POSTGRES")
.unwrap_or("postgres://postgres:postgres@localhost:5432/ormer_test"),
)
}
#[cfg(feature = "mysql")]
#[allow(dead_code)]
pub fn mysql_config() -> DbConfig {
(
DbType::MySQL,
option_env!("ORMER_TEST_MYSQL").unwrap_or("mysql://root:root@localhost:3306/ormer_test"),
)
}
#[macro_export]
macro_rules! test_on_all_dbs {
($test_fn:ident) => {
paste::paste! {
#[tokio::test]
async fn [<test_on_all_dbs_ $test_fn>]() {
let configs = $crate::_test_common::get_all_db_configs();
for (idx, config) in configs.iter().enumerate() {
let db_type_name = match config.0 {
#[cfg(not(any(feature = "sqlite", feature = "postgresql", feature = "mysql")))]
ormer::DbType::None => "Unknown",
#[cfg(feature = "sqlite")]
ormer::DbType::Sqlite => "Sqlite",
#[cfg(feature = "postgresql")]
ormer::DbType::PostgreSQL => "PostgreSQL",
#[cfg(feature = "mysql")]
ormer::DbType::MySQL => "MySQL",
#[allow(unreachable_patterns)]
_ => "Unknown",
};
println!("\n=== Testing on {} (config {}) ===", db_type_name, idx);
$test_fn(config).await;
}
}
}
};
}
#[macro_export]
macro_rules! test_on_all_dbs_result {
($test_fn:ident) => {
paste::paste! {
#[tokio::test]
async fn [<test_on_all_dbs_ $test_fn>]() -> Result<(), Box<dyn std::error::Error>> {
let configs = $crate::_test_common::get_all_db_configs();
for (idx, config) in configs.iter().enumerate() {
let db_type_name = match config.0 {
#[cfg(not(any(feature = "sqlite", feature = "postgresql", feature = "mysql")))]
ormer::DbType::None => "Unknown",
#[cfg(feature = "sqlite")]
ormer::DbType::Sqlite => "Sqlite",
#[cfg(feature = "postgresql")]
ormer::DbType::PostgreSQL => "PostgreSQL",
#[cfg(feature = "mysql")]
ormer::DbType::MySQL => "MySQL",
#[allow(unreachable_patterns)]
_ => "Unknown",
};
println!("\n=== Testing on {} (config {}) ===", db_type_name, idx);
$test_fn(config).await?;
}
Ok(())
}
}
};
}
#[macro_export]
#[cfg(feature = "sqlite")]
macro_rules! test_on_sqlite_only {
($test_fn:ident) => {
paste::paste! {
#[tokio::test]
async fn [<test_on_sqlite_ $test_fn>]() {
let configs = $crate::_test_common::get_sqlite_config();
for (idx, config) in configs.iter().enumerate() {
let db_type_name = "Sqlite";
println!("\n=== Testing on {} (config {}) ===", db_type_name, idx);
$test_fn(config).await;
}
}
}
};
}
#[macro_export]
macro_rules! define_test_user {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
#[unique]
name: String,
#[index]
age: i32,
email: Option<String>,
}
};
}
#[macro_export]
macro_rules! define_test_role {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
#[index]
uid: i32,
name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_with_score {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
name: String,
age: i32,
score: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_simple {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
name: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_with_option_id {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: Option<i64>,
name: String,
email: String,
}
};
}
#[macro_export]
macro_rules! define_test_role_with_unique_group {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
#[unique(group = 1)]
uid: i32,
#[unique(group = 1)]
name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_join {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
#[unique]
name: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_role_for_join {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
#[index]
uid: i32,
role_name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_aggregate_type {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i64,
name: String,
age: i32,
score: f64,
}
};
}
#[macro_export]
macro_rules! define_test_complete_types {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i64,
text_val: String,
optional_text: Option<String>,
optional_int: Option<i32>,
bool_val: bool,
optional_bool: Option<bool>,
}
};
}
#[macro_export]
macro_rules! define_test_user_direct {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
name: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_role_for_collect {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
user_id: i32,
role_name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_id {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_minimal {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_fk {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: i32,
name: String,
}
};
}
#[macro_export]
macro_rules! define_test_role_for_fk {
($struct_name:ident, $table_name:literal, $foreign_type:ty) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
#[foreign($foreign_type)]
user_id: i32,
role_name: String,
}
};
}
#[macro_export]
macro_rules! define_test_role_simple {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_pool {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
name: String,
age: i32,
email: Option<String>,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_map_to {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
name: String,
email: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_role_for_map_to {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
user_id: i32,
role_name: String,
}
};
}
#[macro_export]
macro_rules! define_test_user_id_with_eq {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone, PartialEq)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_name_age {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone, PartialEq)]
#[table = $table_name]
struct $struct_name {
#[primary]
name: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_range {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary]
id: i32,
name: String,
age: i32,
}
};
}
#[macro_export]
macro_rules! define_test_user_for_simple_txn {
($struct_name:ident, $table_name:literal) => {
#[derive(Debug, ormer::Model, Clone)]
#[table = $table_name]
struct $struct_name {
#[primary(auto)]
id: Option<i64>,
name: String,
}
};
}