use elif_orm::{
error::ModelResult,
loading::{EagerLoadConfig, OptimizedEagerLoader},
model::Model,
query::{QueryBuilder, QueryBuilderWithMethods},
};
use serde_json::json;
use std::collections::HashMap;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestUser {
id: i32,
name: String,
}
impl Model for TestUser {
type PrimaryKey = i32;
fn table_name() -> &'static str {
"users"
}
fn primary_key(&self) -> Option<Self::PrimaryKey> {
Some(self.id)
}
fn from_row(_row: &sqlx::postgres::PgRow) -> ModelResult<Self> {
Ok(Self {
id: 1,
name: "Test User".to_string(),
})
}
fn set_primary_key(&mut self, key: Self::PrimaryKey) {
self.id = key;
}
fn to_fields(&self) -> HashMap<String, serde_json::Value> {
let mut fields = HashMap::new();
fields.insert("id".to_string(), json!(self.id));
fields.insert("name".to_string(), json!(self.name));
fields
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_builder_optimization_api() {
let query = QueryBuilder::<TestUser>::new();
let _optimized_query = query
.with("posts")
.with("comments")
.optimize_loading()
.batch_size(50)
.parallel_loading(true)
.max_depth(5);
assert!(true);
}
#[test]
fn test_eager_load_config_creation() {
let config = EagerLoadConfig {
max_batch_size: 50,
deduplicate_queries: true,
max_depth: 3,
enable_parallelism: true,
query_timeout_ms: 5000,
};
assert_eq!(config.max_batch_size, 50);
assert!(config.enable_parallelism);
assert_eq!(config.max_depth, 3);
}
#[test]
fn test_optimization_with_custom_config() {
let config = EagerLoadConfig {
max_batch_size: 25,
deduplicate_queries: true,
max_depth: 2,
enable_parallelism: false,
query_timeout_ms: 10000,
};
let query = QueryBuilder::<TestUser>::new();
let _optimized_query = query
.with("posts.comments")
.optimize_loading_with_config(config);
assert!(true);
}
#[test]
fn test_optimized_eager_loader_creation() {
let loader = OptimizedEagerLoader::new();
let config = loader.config();
assert_eq!(config.max_batch_size, 100); assert!(config.enable_parallelism); assert_eq!(config.max_depth, 10); }
#[test]
fn test_batch_size_configuration() {
let query = QueryBuilder::<TestUser>::new();
let _optimized_query = query.with("posts").batch_size(25).optimize_loading();
let query2 = QueryBuilder::<TestUser>::new();
let _optimized_query2 = query2
.with("posts.comments")
.batch_size(100)
.parallel_loading(false);
assert!(true);
}
#[test]
fn test_fluent_api_chaining() {
let query = QueryBuilder::<TestUser>::new();
let _final_query = query
.with("posts")
.with("profile")
.with_count("comments")
.optimize_loading()
.batch_size(75)
.parallel_loading(true)
.max_depth(4)
.limit(20)
.order_by("created_at")
.where_eq("active", "true");
assert!(true);
}
}