#[cfg(test)]
mod tests {
use crate::query::QueryBuilderWithMethods;
use crate::relationships::constraints::RelationshipConstraintBuilder;
use crate::relationships::eager_loading::*;
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct TestModel {
pub id: i64,
pub name: String,
}
impl crate::model::Model for TestModel {
type PrimaryKey = i64;
fn table_name() -> &'static str {
"test_models"
}
fn primary_key(&self) -> Option<Self::PrimaryKey> {
Some(self.id)
}
fn set_primary_key(&mut self, key: Self::PrimaryKey) {
self.id = key;
}
fn from_row(row: &sqlx::postgres::PgRow) -> crate::error::ModelResult<Self> {
use sqlx::Row;
Ok(TestModel {
id: row.get("id"),
name: row.get("name"),
})
}
fn to_fields(&self) -> std::collections::HashMap<String, serde_json::Value> {
let mut fields = std::collections::HashMap::new();
fields.insert("id".to_string(), serde_json::Value::from(self.id));
fields.insert(
"name".to_string(),
serde_json::Value::from(self.name.clone()),
);
fields
}
}
#[test]
fn test_eager_loader_creation() {
let loader = EagerLoader::new();
assert!(loader.loaded_relations().is_empty());
}
#[test]
fn test_eager_loader_with_relationship() {
let loader = EagerLoader::new().with("posts").with("comments");
assert!(loader.is_loaded("posts") == false); assert!(loader.is_loaded("comments") == false); }
#[test]
fn test_eager_loader_with_constraints() {
let loader = EagerLoader::new().with_constraint("posts", |builder| {
builder
.where_eq("published", true)
.order_by_desc("created_at")
.limit(5)
});
assert!(loader.loaded_relations().is_empty()); }
#[test]
fn test_relationship_constraint_builder() {
let builder = RelationshipConstraintBuilder::new()
.where_eq("status", "published")
.where_gt("views", 1000)
.order_by_desc("created_at")
.limit(10);
drop(builder);
}
#[test]
fn test_query_builder_with_methods() {
use crate::query::QueryBuilder;
let _query = QueryBuilder::<TestModel>::new()
.from("test_models")
.with("posts")
.with("profile")
.limit(10);
}
#[test]
fn test_query_builder_conditional_loading() {
use crate::query::QueryBuilder;
let include_posts = true;
let include_comments = false;
let _query = QueryBuilder::<TestModel>::new()
.from("test_models")
.with_when(include_posts, "posts")
.with_when(include_comments, "comments");
}
#[test]
fn test_query_builder_with_count() {
use crate::query::QueryBuilder;
let _query = QueryBuilder::<TestModel>::new()
.from("test_models")
.with_count("posts")
.with_count("comments");
}
#[test]
fn test_query_builder_with_count_custom_alias() {
use crate::query::QueryBuilder;
let _query = QueryBuilder::<TestModel>::new()
.from("test_models")
.with_count_where("published_posts_count", "posts", |builder| {
builder.where_eq("published", true)
});
}
#[test]
fn test_nested_relationship_parsing() {
let loader = EagerLoader::new().with("posts.comments.user");
assert!(loader.loaded_relations().is_empty()); }
#[test]
fn test_relationship_cache_basic_operations() {
use crate::relationships::cache::OptimizedRelationshipCache;
let cache = OptimizedRelationshipCache::default();
drop(cache);
}
#[test]
fn test_cache_configuration() {
use crate::relationships::cache::{OptimizedRelationshipCache, RelationshipCacheConfig};
use std::time::Duration;
let config = RelationshipCacheConfig {
max_relationships_per_type: 500,
max_memory_bytes: 10 * 1024 * 1024, ttl: Some(Duration::from_secs(600)), enable_metrics: true,
};
let _cache = OptimizedRelationshipCache::new(config);
}
#[test]
fn test_lazy_relationship_loader() {
use crate::relationships::loader::Lazy;
let posts = vec![1, 2, 3]; let lazy_posts = Lazy::loaded(posts);
assert!(lazy_posts.is_loaded());
}
#[test]
fn test_eager_loading_spec_structure() {
let spec = EagerLoadSpec {
relation: "posts.comments".to_string(),
constraints: None,
};
assert_eq!(spec.relation, "posts.comments");
assert!(spec.constraints.is_none());
let debug_str = format!("{:?}", spec);
assert!(debug_str.contains("posts.comments"));
}
#[test]
fn test_relationship_loading_integration() {
let loader = EagerLoader::new()
.with("posts")
.with_constraint("comments", |builder| {
builder.where_eq("approved", true).limit(10)
});
assert!(loader.loaded_relations().is_empty());
assert_eq!(loader.get_loaded_data("posts", "1"), None);
assert_eq!(loader.get_loaded_data("comments", "1"), None);
}
#[test]
fn test_relationship_cache_stats() {
use crate::relationships::cache::{CacheStatistics, OptimizedRelationshipCache};
let stats = CacheStatistics {
total_entries: 0,
memory_usage_bytes: 0,
model_type_counts: std::collections::HashMap::new(),
hits: 0,
misses: 0,
stores: 0,
expired: 0,
hit_rate: 0.0,
};
assert_eq!(stats.total_entries, 0);
assert_eq!(stats.hit_rate, 0.0);
let cache = OptimizedRelationshipCache::default();
drop(cache); }
#[test]
fn test_query_builder_chaining() {
use crate::query::QueryBuilder;
let _query = QueryBuilder::<TestModel>::new()
.from("test_models")
.with("posts")
.with("profile")
.with_when(true, "settings")
.with_count("followers")
.where_eq("active", "true")
.order_by_desc("created_at")
.limit(50);
}
}