reinhardt-rest 0.1.2

REST API framework aggregator for Reinhardt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
//! ORM integration for relation fields
//!
//! This module provides ORM-backed implementations of relation fields:
//! - `PrimaryKeyRelatedField` with database lookups
//! - `SlugRelatedField` with slug-based queries
//! - Query optimization with select_related/prefetch_related

use super::{SerializerError, ValidatorError};
use async_trait::async_trait;
use reinhardt_db::orm::{Model, query::*};
use serde::{Serialize, de::DeserializeOwned};
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::Arc;

// Type wrappers to simplify complex function pointer types

/// Type alias for async many-to-many relation resolver trait object
type ManyResolverFn<T, RelatedItem> = Arc<
	dyn Fn(&T) -> Pin<Box<dyn Future<Output = Result<Vec<RelatedItem>, String>> + Send>>
		+ Send
		+ Sync,
>;

/// Async many-to-many relation resolver function
pub struct ManyRelationResolverFn<T, RelatedItem> {
	inner: ManyResolverFn<T, RelatedItem>,
	_phantom: PhantomData<RelatedItem>,
}

impl<T, RelatedItem> ManyRelationResolverFn<T, RelatedItem> {
	/// Create a new many-to-many relation resolver
	pub fn new<F>(func: F) -> Self
	where
		F: Fn(&T) -> Pin<Box<dyn Future<Output = Result<Vec<RelatedItem>, String>> + Send>>
			+ Send
			+ Sync
			+ 'static,
	{
		Self {
			inner: Arc::new(func),
			_phantom: PhantomData,
		}
	}
}

impl<T, RelatedItem> std::ops::Deref for ManyRelationResolverFn<T, RelatedItem> {
	type Target = dyn Fn(&T) -> Pin<Box<dyn Future<Output = Result<Vec<RelatedItem>, String>> + Send>>
		+ Send
		+ Sync;

	fn deref(&self) -> &Self::Target {
		&*self.inner
	}
}

impl<T, RelatedItem> Clone for ManyRelationResolverFn<T, RelatedItem> {
	fn clone(&self) -> Self {
		Self {
			inner: self.inner.clone(),
			_phantom: PhantomData,
		}
	}
}

/// Type alias for async one-to-one/foreign-key relation resolver trait object
type SingleResolverFn<T, RelatedItem> = Arc<
	dyn Fn(&T) -> Pin<Box<dyn Future<Output = Result<RelatedItem, String>> + Send>> + Send + Sync,
>;

/// Async one-to-one/foreign-key relation resolver function
pub struct SingleRelationResolverFn<T, RelatedItem> {
	inner: SingleResolverFn<T, RelatedItem>,
	_phantom: PhantomData<RelatedItem>,
}

impl<T, RelatedItem> SingleRelationResolverFn<T, RelatedItem> {
	/// Create a new single relation resolver
	pub fn new<F>(func: F) -> Self
	where
		F: Fn(&T) -> Pin<Box<dyn Future<Output = Result<RelatedItem, String>> + Send>>
			+ Send
			+ Sync
			+ 'static,
	{
		Self {
			inner: Arc::new(func),
			_phantom: PhantomData,
		}
	}
}

impl<T, RelatedItem> std::ops::Deref for SingleRelationResolverFn<T, RelatedItem> {
	type Target = dyn Fn(&T) -> Pin<Box<dyn Future<Output = Result<RelatedItem, String>> + Send>>
		+ Send
		+ Sync;

	fn deref(&self) -> &Self::Target {
		&*self.inner
	}
}

impl<T, RelatedItem> Clone for SingleRelationResolverFn<T, RelatedItem> {
	fn clone(&self) -> Self {
		Self {
			inner: self.inner.clone(),
			_phantom: PhantomData,
		}
	}
}

/// Primary key related field with ORM query support
///
/// Represents a relationship using the related object's primary key.
/// Provides database lookup and validation.
///
/// # Examples
///
/// ```rust,no_run,ignore
/// # #[tokio::main]
/// # async fn main() {
/// use reinhardt_rest::serializers::relation_fields_orm::PrimaryKeyRelatedFieldORM;
///
/// // Field that references User by ID
/// let field = PrimaryKeyRelatedFieldORM::<User>::new();
///
/// // Verify that user exists (database lookup)
/// field.validate_exists(123).await?;
///
/// // Get the user instance (database query)
/// let user = field.get_instance(123).await?;
///
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct PrimaryKeyRelatedFieldORM<T>
where
	T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
{
	_phantom: PhantomData<T>,
	/// Whether to allow null values
	pub allow_null: bool,
	/// Custom queryset for filtering
	pub queryset_filter: Option<Filter>,
}

impl<T> PrimaryKeyRelatedFieldORM<T>
where
	T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
{
	/// Create a new primary key related field
	///
	/// # Examples
	///
	/// ```ignore
	/// let field = PrimaryKeyRelatedFieldORM::<User>::new();
	/// // Verify the field is created with default settings
	/// let _: PrimaryKeyRelatedFieldORM<User> = field;
	/// ```
	pub fn new() -> Self {
		Self {
			_phantom: PhantomData,
			allow_null: false,
			queryset_filter: None,
		}
	}

	/// Allow null values
	pub fn with_allow_null(mut self, allow_null: bool) -> Self {
		self.allow_null = allow_null;
		self
	}

	/// Add queryset filter
	///
	/// Restricts lookups to instances matching the filter.
	///
	/// # Examples
	///
	/// ```ignore
	/// use reinhardt_db::orm::query::{Filter, FilterOperator, FilterValue};
	///
	/// let field = PrimaryKeyRelatedFieldORM::<User>::new()
	///     .with_queryset_filter(Filter::new(
	///         "is_active".to_string(),
	///         FilterOperator::Eq,
	///         FilterValue::Boolean(true),
	///     ));
	/// // Verify the field is configured with the filter
	/// let _: PrimaryKeyRelatedFieldORM<User> = field;
	/// ```
	pub fn with_queryset_filter(mut self, filter: Filter) -> Self {
		self.queryset_filter = Some(filter);
		self
	}

	/// Validate that an instance with the given primary key exists
	///
	/// # Errors
	///
	/// Returns `SerializerError` if:
	/// - Instance not found
	/// - Instance doesn't match queryset filter
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify existence check runs without error (requires database)
	/// field.validate_exists(&123).await?;
	/// ```
	pub async fn validate_exists(&self, pk: &T::PrimaryKey) -> Result<(), SerializerError>
	where
		T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
		T::PrimaryKey: std::fmt::Display + Clone + Send + Sync,
	{
		use reinhardt_db::orm::QuerySet;

		let mut queryset = QuerySet::<T>::new();

		// Filter by primary key
		let pk_field = T::primary_key_field();
		let filter = Filter::new(
			pk_field.to_string(),
			FilterOperator::Eq,
			FilterValue::String(pk.to_string()),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Count results
		let count = queryset.count().await.map_err(|e| SerializerError::Other {
			message: format!("Failed to validate existence: {}", e),
		})?;

		if count == 0 {
			return Err(SerializerError::Validation(ValidatorError::Custom {
				message: format!("Instance with pk {} does not exist", pk),
			}));
		}

		Ok(())
	}

	/// Get the related instance by primary key
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify instance is retrieved correctly (requires database)
	/// let user = field.get_instance(&123).await?;
	/// assert_eq!(user.id, Some(123));
	/// ```
	pub async fn get_instance(&self, pk: &T::PrimaryKey) -> Result<T, SerializerError>
	where
		T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
		T::PrimaryKey: std::fmt::Display + Clone + Send + Sync,
	{
		use reinhardt_db::orm::QuerySet;

		let mut queryset = QuerySet::<T>::new();

		// Filter by primary key
		let pk_field = T::primary_key_field();
		let filter = Filter::new(
			pk_field.to_string(),
			FilterOperator::Eq,
			FilterValue::String(pk.to_string()),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Get first result
		let instance = queryset
			.first()
			.await
			.map_err(|e| SerializerError::Other {
				message: format!("Failed to fetch instance: {}", e),
			})?
			.ok_or_else(|| {
				SerializerError::Validation(ValidatorError::Custom {
					message: format!("Instance with pk {} not found", pk),
				})
			})?;

		Ok(instance)
	}

	/// Get multiple instances by primary keys (batch lookup)
	///
	/// More efficient than calling `get_instance` multiple times.
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify batch retrieval works (requires database)
	/// let users = field.get_instances(vec![1, 2, 3]).await?;
	/// assert_eq!(users.len(), 3);
	/// ```
	pub async fn get_instances(&self, pks: Vec<T::PrimaryKey>) -> Result<Vec<T>, SerializerError>
	where
		T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
		T::PrimaryKey: std::fmt::Display + Clone + Send + Sync,
	{
		use reinhardt_db::orm::QuerySet;

		if pks.is_empty() {
			return Ok(Vec::new());
		}

		let mut queryset = QuerySet::<T>::new();

		// Filter by primary key IN (pks)
		let pk_field = T::primary_key_field();
		let pk_values: Vec<String> = pks.iter().map(|pk| pk.to_string()).collect();

		let filter = Filter::new(
			pk_field.to_string(),
			FilterOperator::In,
			FilterValue::Array(pk_values),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Get all results
		let instances = queryset.all().await.map_err(|e| SerializerError::Other {
			message: format!("Failed to fetch instances: {}", e),
		})?;

		Ok(instances)
	}
}

impl<T> Default for PrimaryKeyRelatedFieldORM<T>
where
	T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
{
	fn default() -> Self {
		Self::new()
	}
}

/// Slug related field with ORM query support
///
/// Represents a relationship using a slug field (e.g., username, slug).
/// Provides database lookup by slug value.
///
/// # Examples
///
/// ```rust,no_run,ignore
/// # #[tokio::main]
/// # async fn main() {
/// use reinhardt_rest::serializers::relation_fields_orm::SlugRelatedFieldORM;
///
/// // Field that references User by username
/// let field = SlugRelatedFieldORM::<User>::new("username");
///
/// // Verify that user exists (database lookup by slug)
/// field.validate_exists("alice").await?;
///
/// // Get the user instance (database query by slug)
/// let user = field.get_instance("alice").await?;
/// assert_eq!(user.username, "alice");
///
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct SlugRelatedFieldORM<T>
where
	T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
{
	_phantom: PhantomData<T>,
	/// The slug field name to query by
	pub slug_field: String,
	/// Whether to allow null values
	pub allow_null: bool,
	/// Custom queryset filter
	pub queryset_filter: Option<Filter>,
}

impl<T> SlugRelatedFieldORM<T>
where
	T: Model + Serialize + DeserializeOwned + Clone + Send + Sync,
{
	/// Create a new slug related field
	///
	/// # Examples
	///
	/// ```ignore
	/// let field = SlugRelatedFieldORM::<User>::new("username");
	/// // Verify the field is created with the slug field
	/// let _: SlugRelatedFieldORM<User> = field;
	/// ```
	pub fn new(slug_field: impl Into<String>) -> Self {
		Self {
			_phantom: PhantomData,
			slug_field: slug_field.into(),
			allow_null: false,
			queryset_filter: None,
		}
	}

	/// Allow null values
	pub fn with_allow_null(mut self, allow_null: bool) -> Self {
		self.allow_null = allow_null;
		self
	}

	/// Add queryset filter
	pub fn with_queryset_filter(mut self, filter: Filter) -> Self {
		self.queryset_filter = Some(filter);
		self
	}

	/// Validate that an instance with the given slug exists
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify slug existence check runs (requires database)
	/// field.validate_exists("alice").await?;
	/// ```
	pub async fn validate_exists(&self, slug: &str) -> Result<(), SerializerError> {
		use reinhardt_db::orm::QuerySet;

		let mut queryset = QuerySet::<T>::new();

		// Filter by slug field
		let filter = Filter::new(
			self.slug_field.clone(),
			FilterOperator::Eq,
			FilterValue::String(slug.to_string()),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Count results
		let count = queryset.count().await.map_err(|e| SerializerError::Other {
			message: format!("Failed to validate existence: {}", e),
		})?;

		if count == 0 {
			return Err(SerializerError::Validation(ValidatorError::Custom {
				message: format!(
					"Instance with {} '{}' does not exist",
					self.slug_field, slug
				),
			}));
		}

		Ok(())
	}

	/// Get the related instance by slug
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify instance retrieval by slug (requires database)
	/// let user = field.get_instance("alice").await?;
	/// assert_eq!(user.username, "alice");
	/// ```
	pub async fn get_instance(&self, slug: &str) -> Result<T, SerializerError> {
		use reinhardt_db::orm::QuerySet;

		let mut queryset = QuerySet::<T>::new();

		// Filter by slug field
		let filter = Filter::new(
			self.slug_field.clone(),
			FilterOperator::Eq,
			FilterValue::String(slug.to_string()),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Get first result
		let instance = queryset
			.first()
			.await
			.map_err(|e| SerializerError::Other {
				message: format!("Failed to fetch instance: {}", e),
			})?
			.ok_or_else(|| {
				SerializerError::Validation(ValidatorError::Custom {
					message: format!("Instance with {} '{}' not found", self.slug_field, slug),
				})
			})?;

		Ok(instance)
	}

	/// Get multiple instances by slugs (batch lookup)
	///
	/// # Examples
	///
	/// ```ignore
	/// // Verify batch retrieval by slug (requires database)
	/// let users = field.get_instances(vec!["alice".to_string(), "bob".to_string()]).await?;
	/// assert_eq!(users.len(), 2);
	/// ```
	pub async fn get_instances(&self, slugs: Vec<String>) -> Result<Vec<T>, SerializerError> {
		use reinhardt_db::orm::QuerySet;

		if slugs.is_empty() {
			return Ok(Vec::new());
		}

		let mut queryset = QuerySet::<T>::new();

		// Filter by slug field IN (slugs)
		let slug_values: Vec<String> = slugs.to_vec();

		let filter = Filter::new(
			self.slug_field.clone(),
			FilterOperator::In,
			FilterValue::Array(slug_values),
		);
		queryset = queryset.filter(filter);

		// Apply additional filter if present
		if let Some(ref custom_filter) = self.queryset_filter {
			queryset = queryset.filter(custom_filter.clone());
		}

		// Get all results
		let instances = queryset.all().await.map_err(|e| SerializerError::Other {
			message: format!("Failed to fetch instances: {}", e),
		})?;

		Ok(instances)
	}
}

/// Query optimization manager for relation fields
///
/// Manages select_related and prefetch_related optimizations
/// for efficient loading of related objects.
///
/// # Examples
///
/// ```rust,no_run,ignore
/// use reinhardt_rest::serializers::relation_fields_orm::QueryOptimizer;
///
/// let optimizer = QueryOptimizer::new()
///     .with_select_related(vec!["author", "category"])
///     .with_prefetch_related(vec!["comments", "tags"]);
///
/// // Verify optimizer is configured correctly
/// let _: QueryOptimizer = optimizer.clone();
/// let queryset = optimizer.apply(QuerySet::<Post>::new());
/// let posts = queryset.all();
/// ```
#[derive(Debug, Clone, Default)]
pub struct QueryOptimizer {
	/// Fields to load via JOIN (select_related)
	pub select_related: Vec<String>,
	/// Fields to load via separate queries (prefetch_related)
	pub prefetch_related: Vec<String>,
}

impl QueryOptimizer {
	/// Create a new query optimizer
	pub fn new() -> Self {
		Self {
			select_related: Vec::new(),
			prefetch_related: Vec::new(),
		}
	}

	/// Add select_related fields
	///
	/// Loads related objects using JOIN queries.
	/// Best for ForeignKey and OneToOne relationships.
	pub fn with_select_related(mut self, fields: Vec<String>) -> Self {
		self.select_related = fields;
		self
	}

	/// Add prefetch_related fields
	///
	/// Loads related objects using separate queries.
	/// Best for ManyToMany and reverse ForeignKey relationships.
	pub fn with_prefetch_related(mut self, fields: Vec<String>) -> Self {
		self.prefetch_related = fields;
		self
	}

	/// Apply optimizations to a QuerySet
	pub fn apply<T>(&self, mut queryset: QuerySet<T>) -> QuerySet<T>
	where
		T: Model,
	{
		if !self.select_related.is_empty() {
			let fields: Vec<&str> = self.select_related.iter().map(|s| s.as_str()).collect();
			queryset = queryset.select_related(&fields);
		}

		if !self.prefetch_related.is_empty() {
			let fields: Vec<&str> = self.prefetch_related.iter().map(|s| s.as_str()).collect();
			queryset = queryset.prefetch_related(&fields);
		}

		queryset
	}
}

/// Trait for relation fields that support query optimization
#[async_trait]
pub trait OptimizableRelationField {
	/// Get the query optimizer for this field
	fn get_optimizer(&self) -> Option<&QueryOptimizer>;

	/// Set the query optimizer for this field
	fn set_optimizer(&mut self, optimizer: QueryOptimizer);
}

#[cfg(test)]
mod tests {
	use super::*;
	use reinhardt_db::orm::{FieldSelector, Model};
	use serde::{Deserialize, Serialize};

	#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
	struct TestUser {
		id: Option<i64>,
		username: String,
		email: String,
	}

	#[derive(Debug, Clone)]
	struct TestUserFields;

	impl FieldSelector for TestUserFields {
		fn with_alias(self, _alias: &str) -> Self {
			self
		}
	}

	impl Model for TestUser {
		type PrimaryKey = i64;
		type Fields = TestUserFields;

		fn table_name() -> &'static str {
			"test_users"
		}

		fn new_fields() -> Self::Fields {
			TestUserFields
		}

		fn primary_key(&self) -> Option<Self::PrimaryKey> {
			self.id
		}

		fn set_primary_key(&mut self, value: Self::PrimaryKey) {
			self.id = Some(value);
		}
	}

	#[test]
	fn test_pk_related_field_creation() {
		let field = PrimaryKeyRelatedFieldORM::<TestUser>::new();
		assert!(!field.allow_null);
		assert!(field.queryset_filter.is_none());
	}

	#[test]
	fn test_pk_related_field_with_allow_null() {
		let field = PrimaryKeyRelatedFieldORM::<TestUser>::new().with_allow_null(true);
		assert!(field.allow_null);
	}

	#[test]
	fn test_pk_related_field_with_filter() {
		let filter = Filter::new(
			"is_active".to_string(),
			FilterOperator::Eq,
			FilterValue::Boolean(true),
		);
		let field = PrimaryKeyRelatedFieldORM::<TestUser>::new().with_queryset_filter(filter);
		assert!(field.queryset_filter.is_some());
	}

	#[test]
	fn test_slug_related_field_creation() {
		let field = SlugRelatedFieldORM::<TestUser>::new("username");
		assert_eq!(field.slug_field, "username");
		assert!(!field.allow_null);
		assert!(field.queryset_filter.is_none());
	}

	#[test]
	fn test_slug_related_field_with_allow_null() {
		let field = SlugRelatedFieldORM::<TestUser>::new("username").with_allow_null(true);
		assert!(field.allow_null);
	}

	#[test]
	fn test_query_optimizer_creation() {
		let optimizer = QueryOptimizer::new();
		assert!(optimizer.select_related.is_empty());
		assert!(optimizer.prefetch_related.is_empty());
	}

	#[test]
	fn test_query_optimizer_with_select_related() {
		let optimizer = QueryOptimizer::new()
			.with_select_related(vec!["author".to_string(), "category".to_string()]);

		assert_eq!(optimizer.select_related.len(), 2);
		assert!(optimizer.select_related.contains(&"author".to_string()));
		assert!(optimizer.select_related.contains(&"category".to_string()));
	}

	#[test]
	fn test_query_optimizer_with_prefetch_related() {
		let optimizer = QueryOptimizer::new()
			.with_prefetch_related(vec!["comments".to_string(), "tags".to_string()]);

		assert_eq!(optimizer.prefetch_related.len(), 2);
		assert!(optimizer.prefetch_related.contains(&"comments".to_string()));
		assert!(optimizer.prefetch_related.contains(&"tags".to_string()));
	}

	#[test]
	fn test_query_optimizer_apply() {
		let optimizer = QueryOptimizer::new()
			.with_select_related(vec!["author".to_string()])
			.with_prefetch_related(vec!["comments".to_string()]);

		let queryset = QuerySet::<TestUser>::new();
		let optimized = optimizer.apply(queryset);

		// QuerySet methods are called, but we can't easily verify the result
		// without a real database. This test just ensures no panics.
		drop(optimized);
	}
}