reinhardt-db 0.1.0

Django-style database layer for Reinhardt framework
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/// Generic content type system for polymorphic relationships
/// Based on Django's contenttypes framework
///
/// This module provides both string-based (runtime) and type-safe (compile-time)
/// content type registry mechanisms.
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::RwLock;

/// Represents a content type (model) in the system
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct ContentType {
	/// The id.
	pub id: Option<i64>,
	/// The app label.
	pub app_label: String,
	/// The model.
	pub model: String,
}

impl ContentType {
	/// Creates a new instance.
	pub fn new(app_label: impl Into<String>, model: impl Into<String>) -> Self {
		Self {
			id: None,
			app_label: app_label.into(),
			model: model.into(),
		}
	}

	/// Sets the id and returns self for chaining.
	pub fn with_id(mut self, id: i64) -> Self {
		self.id = Some(id);
		self
	}

	/// Get the natural key for this content type
	pub fn natural_key(&self) -> (String, String) {
		(self.app_label.clone(), self.model.clone())
	}

	/// Get fully qualified model name
	pub fn qualified_name(&self) -> String {
		format!("{}.{}", self.app_label, self.model)
	}
}

/// Registry for managing content types
pub struct ContentTypeRegistry {
	types: RwLock<HashMap<(String, String), ContentType>>,
	by_id: RwLock<HashMap<i64, ContentType>>,
	next_id: RwLock<i64>,
}

impl ContentTypeRegistry {
	/// Creates a new instance.
	pub fn new() -> Self {
		Self {
			types: RwLock::new(HashMap::new()),
			by_id: RwLock::new(HashMap::new()),
			next_id: RwLock::new(1),
		}
	}

	/// Register a new content type
	pub fn register(&self, mut ct: ContentType) -> ContentType {
		let key = ct.natural_key();

		// Check if already exists
		if let Some(existing) = self
			.types
			.read()
			.unwrap_or_else(|e| e.into_inner())
			.get(&key)
		{
			return existing.clone();
		}

		// Assign ID if not present
		if ct.id.is_none() {
			let mut next_id = self.next_id.write().unwrap_or_else(|e| e.into_inner());
			ct.id = Some(*next_id);
			*next_id += 1;
		}

		// Store in both maps
		self.types
			.write()
			.unwrap_or_else(|e| e.into_inner())
			.insert(key, ct.clone());
		if let Some(id) = ct.id {
			self.by_id
				.write()
				.unwrap_or_else(|e| e.into_inner())
				.insert(id, ct.clone());
		}

		ct
	}

	/// Get content type by app label and model name
	pub fn get(&self, app_label: &str, model: &str) -> Option<ContentType> {
		let key = (app_label.to_string(), model.to_string());
		self.types
			.read()
			.unwrap_or_else(|e| e.into_inner())
			.get(&key)
			.cloned()
	}

	/// Get content type by ID
	pub fn get_by_id(&self, id: i64) -> Option<ContentType> {
		self.by_id
			.read()
			.unwrap_or_else(|e| e.into_inner())
			.get(&id)
			.cloned()
	}

	/// Get or create a content type
	pub fn get_or_create(&self, app_label: &str, model: &str) -> ContentType {
		if let Some(ct) = self.get(app_label, model) {
			ct
		} else {
			self.register(ContentType::new(app_label, model))
		}
	}

	/// List all registered content types
	pub fn all(&self) -> Vec<ContentType> {
		self.types
			.read()
			.unwrap_or_else(|e| e.into_inner())
			.values()
			.cloned()
			.collect()
	}

	/// Clear all registered types (mainly for testing)
	pub fn clear(&self) {
		self.types
			.write()
			.unwrap_or_else(|e| e.into_inner())
			.clear();
		self.by_id
			.write()
			.unwrap_or_else(|e| e.into_inner())
			.clear();
		*self.next_id.write().unwrap_or_else(|e| e.into_inner()) = 1;
	}
}

impl Default for ContentTypeRegistry {
	fn default() -> Self {
		Self::new()
	}
}

use once_cell::sync::Lazy;

/// Global content type registry.
pub static CONTENT_TYPE_REGISTRY: Lazy<ContentTypeRegistry> = Lazy::new(ContentTypeRegistry::new);

/// Generic foreign key field
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericForeignKey {
	/// The content type id.
	pub content_type_id: Option<i64>,
	/// The object id.
	pub object_id: Option<i64>,
}

impl GenericForeignKey {
	/// Creates a new instance.
	pub fn new() -> Self {
		Self {
			content_type_id: None,
			object_id: None,
		}
	}

	/// Performs the set operation.
	pub fn set(&mut self, content_type: &ContentType, object_id: i64) {
		self.content_type_id = content_type.id;
		self.object_id = Some(object_id);
	}

	/// Returns the content type.
	pub fn get_content_type(&self) -> Option<ContentType> {
		self.content_type_id
			.and_then(|id| CONTENT_TYPE_REGISTRY.get_by_id(id))
	}

	/// Returns the et.
	pub fn is_set(&self) -> bool {
		self.content_type_id.is_some() && self.object_id.is_some()
	}

	/// Performs the clear operation.
	pub fn clear(&mut self) {
		self.content_type_id = None;
		self.object_id = None;
	}
}

impl Default for GenericForeignKey {
	fn default() -> Self {
		Self::new()
	}
}

/// Trait for models that can be targets of generic relations
pub trait GenericRelatable {
	/// Returns the content type for this model.
	fn get_content_type() -> ContentType;
	/// Returns the object identifier for this instance.
	fn get_object_id(&self) -> i64;
}

/// Helper for building generic relation queries
pub struct GenericRelationQuery {
	content_type: ContentType,
	object_ids: Vec<i64>,
}

impl GenericRelationQuery {
	/// Creates a new instance.
	pub fn new(content_type: ContentType) -> Self {
		Self {
			content_type,
			object_ids: Vec::new(),
		}
	}

	/// Adds object.
	pub fn add_object(&mut self, object_id: i64) {
		self.object_ids.push(object_id);
	}

	/// Converts to sql.
	pub fn to_sql(&self, table: &str) -> String {
		let ct_id = self.content_type.id.unwrap_or(0);
		let ids = self
			.object_ids
			.iter()
			.map(|id| id.to_string())
			.collect::<Vec<_>>()
			.join(", ");

		format!(
			"SELECT * FROM {} WHERE content_type_id = {} AND object_id IN ({})",
			table, ct_id, ids
		)
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn test_content_type_creation() {
		let ct = ContentType::new("blog", "Post");
		assert_eq!(ct.app_label, "blog");
		assert_eq!(ct.model, "Post");
		assert_eq!(ct.qualified_name(), "blog.Post");
	}

	#[test]
	fn test_content_type_natural_key() {
		let ct = ContentType::new("auth", "User");
		let (app, model) = ct.natural_key();
		assert_eq!(app, "auth");
		assert_eq!(model, "User");
	}

	#[test]
	fn test_registry_register() {
		let registry = ContentTypeRegistry::new();
		let ct = ContentType::new("test", "Model");
		let registered = registry.register(ct);

		assert!(registered.id.is_some());
		assert_eq!(registered.app_label, "test");
	}

	#[test]
	fn test_registry_get() {
		let registry = ContentTypeRegistry::new();
		registry.register(ContentType::new("app1", "Model1"));

		let found = registry.get("app1", "Model1");
		assert!(found.is_some());
		assert_eq!(found.unwrap().model, "Model1");
	}

	#[test]
	fn test_registry_get_by_id() {
		let registry = ContentTypeRegistry::new();
		let ct = registry.register(ContentType::new("app2", "Model2"));
		let id = ct.id.unwrap();

		let found = registry.get_by_id(id);
		assert!(found.is_some());
		assert_eq!(found.unwrap().app_label, "app2");
	}

	#[test]
	fn test_registry_get_or_create() {
		let registry = ContentTypeRegistry::new();

		let ct1 = registry.get_or_create("app3", "Model3");
		let ct2 = registry.get_or_create("app3", "Model3");

		assert_eq!(ct1.id, ct2.id);
	}

	#[test]
	fn test_registry_all() {
		let registry = ContentTypeRegistry::new();
		registry.register(ContentType::new("app4", "Model4"));
		registry.register(ContentType::new("app5", "Model5"));

		let all = registry.all();
		assert_eq!(all.len(), 2);
	}

	#[test]
	fn test_generic_foreign_key() {
		let registry = ContentTypeRegistry::new();
		let ct = registry.register(ContentType::new("test", "Article"));

		let mut gfk = GenericForeignKey::new();
		assert!(!gfk.is_set());

		gfk.set(&ct, 42);
		assert!(gfk.is_set());
		assert_eq!(gfk.object_id, Some(42));
		assert_eq!(gfk.content_type_id, ct.id);

		gfk.clear();
		assert!(!gfk.is_set());
	}

	#[test]
	fn test_generic_foreign_key_get_content_type() {
		let ct = CONTENT_TYPE_REGISTRY.register(ContentType::new("blog", "Comment"));

		let mut gfk = GenericForeignKey::new();
		gfk.set(&ct, 100);

		let retrieved_ct = gfk.get_content_type();
		assert!(retrieved_ct.is_some());
		assert_eq!(retrieved_ct.unwrap().model, "Comment");
	}

	#[test]
	fn test_generic_relation_query() {
		let ct = ContentType::new("shop", "Product").with_id(5);
		let mut query = GenericRelationQuery::new(ct);

		query.add_object(1);
		query.add_object(2);
		query.add_object(3);

		let sql = query.to_sql("ratings");
		assert!(sql.contains("content_type_id = 5"));
		assert!(sql.contains("object_id IN (1, 2, 3)"));
	}

	#[test]
	fn test_registry_clear() {
		let registry = ContentTypeRegistry::new();
		registry.register(ContentType::new("temp", "TempModel"));

		assert_eq!(registry.all().len(), 1);

		registry.clear();
		assert_eq!(registry.all().len(), 0);
	}
}

// ============================================================================
// Type-safe content type registry (compile-time checked)
// ============================================================================

/// Trait for models that can be registered as content types
///
/// Implement this trait for each model in your application.
/// The compiler will ensure that only valid model types can be used.
///
/// # Example
///
/// ```rust
/// use reinhardt_db::contenttypes::ModelType;
///
/// pub struct UserModel;
/// impl ModelType for UserModel {
///     const APP_LABEL: &'static str = "auth";
///     const MODEL_NAME: &'static str = "User";
/// }
/// ```
pub trait ModelType {
	/// The app label for this model
	const APP_LABEL: &'static str;

	/// The model name
	const MODEL_NAME: &'static str;
}

impl ContentTypeRegistry {
	/// Type-safe get method
	///
	/// Get a content type using compile-time verified model types.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::{ContentTypeRegistry, ModelType};
	///
	/// pub struct UserModel;
	/// impl ModelType for UserModel {
	///     const APP_LABEL: &'static str = "auth";
	///     const MODEL_NAME: &'static str = "User";
	/// }
	///
	/// let registry = ContentTypeRegistry::new();
	/// let ct = registry.get_typed::<UserModel>();
	/// ```
	pub fn get_typed<M: ModelType>(&self) -> Option<ContentType> {
		self.get(M::APP_LABEL, M::MODEL_NAME)
	}

	/// Type-safe get_or_create method
	///
	/// Get or create a content type using compile-time verified model types.
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::{ContentTypeRegistry, ModelType};
	///
	/// pub struct PostModel;
	/// impl ModelType for PostModel {
	///     const APP_LABEL: &'static str = "blog";
	///     const MODEL_NAME: &'static str = "Post";
	/// }
	///
	/// let registry = ContentTypeRegistry::new();
	/// let ct = registry.get_or_create_typed::<PostModel>();
	/// ```
	pub fn get_or_create_typed<M: ModelType>(&self) -> ContentType {
		self.get_or_create(M::APP_LABEL, M::MODEL_NAME)
	}

	/// Type-safe register method
	///
	/// Register a content type using compile-time verified model types.
	pub fn register_typed<M: ModelType>(&self) -> ContentType {
		self.register(ContentType::new(M::APP_LABEL, M::MODEL_NAME))
	}
}

impl GenericForeignKey {
	/// Type-safe set method
	///
	/// Set the generic foreign key using a typed model.
	pub fn set_typed<M: ModelType>(&mut self, registry: &ContentTypeRegistry, object_id: i64) {
		let ct = registry.get_or_create_typed::<M>();
		self.set(&ct, object_id);
	}
}

#[cfg(test)]
mod typed_tests {
	use super::*;

	// Test model types
	struct UserModel;
	impl ModelType for UserModel {
		const APP_LABEL: &'static str = "auth";
		const MODEL_NAME: &'static str = "User";
	}

	struct PostModel;
	impl ModelType for PostModel {
		const APP_LABEL: &'static str = "blog";
		const MODEL_NAME: &'static str = "Post";
	}

	struct CommentModel;
	impl ModelType for CommentModel {
		const APP_LABEL: &'static str = "blog";
		const MODEL_NAME: &'static str = "Comment";
	}

	#[test]
	fn test_contenttypes_typed_register() {
		let registry = ContentTypeRegistry::new();
		let ct = registry.register_typed::<UserModel>();

		assert_eq!(ct.app_label, "auth");
		assert_eq!(ct.model, "User");
		assert!(ct.id.is_some());
	}

	#[test]
	fn test_contenttypes_typed_get() {
		let registry = ContentTypeRegistry::new();
		registry.register_typed::<PostModel>();

		let ct = registry.get_typed::<PostModel>();
		assert!(ct.is_some());
		assert_eq!(ct.unwrap().model, "Post");
	}

	#[test]
	fn test_contenttypes_typed_get_not_found() {
		let registry = ContentTypeRegistry::new();

		let ct = registry.get_typed::<CommentModel>();
		assert!(ct.is_none());
	}

	#[test]
	fn test_typed_get_or_create() {
		let registry = ContentTypeRegistry::new();

		let ct1 = registry.get_or_create_typed::<UserModel>();
		let ct2 = registry.get_or_create_typed::<UserModel>();

		assert_eq!(ct1.id, ct2.id);
	}

	#[test]
	fn test_typed_generic_foreign_key() {
		// Clean up global registry first
		CONTENT_TYPE_REGISTRY.clear();

		let registry = ContentTypeRegistry::new();
		let mut gfk = GenericForeignKey::new();

		gfk.set_typed::<PostModel>(&registry, 42);

		assert!(gfk.is_set());
		assert_eq!(gfk.object_id, Some(42));

		// Note: get_content_type uses global registry, so we need to register there too
		CONTENT_TYPE_REGISTRY
			.register(ContentType::new("blog", "Post").with_id(gfk.content_type_id.unwrap()));

		let ct = gfk.get_content_type();
		assert!(ct.is_some());
		assert_eq!(ct.unwrap().model, "Post");

		// Clean up
		CONTENT_TYPE_REGISTRY.clear();
	}

	#[test]
	fn test_contenttypes_typed_and_regular_mixed() {
		let registry = ContentTypeRegistry::new();

		// Register using typed method
		registry.register_typed::<UserModel>();

		// Can access using both methods
		let typed = registry.get_typed::<UserModel>();
		let regular = registry.get("auth", "User");

		assert!(typed.is_some());
		assert!(regular.is_some());
		assert_eq!(typed.unwrap().id, regular.unwrap().id);
	}
}

// ============================================================================
// Comprehensive tests inspired by Django's contenttypes tests
// ============================================================================
//
// See docs/IMPLEMENTATION_NOTES.md for unimplemented test categories and future additions

#[cfg(test)]
mod inspired_tests {
	use super::*;

	// Test helper: Setup function that clears registry before each test
	// Note: Each test creates its own registry instance to ensure isolation
	fn setup_registry() -> ContentTypeRegistry {
		let registry = ContentTypeRegistry::new();
		registry.clear();
		registry
	}

	// ========== ContentType Model Tests ==========

	/// Test: ContentType string representation
	#[test]
	fn test_content_type_str() {
		let ct = ContentType::new("contenttypes_tests", "site");
		assert_eq!(ct.qualified_name(), "contenttypes_tests.site");
	}

	/// Test: ContentType natural key retrieval
	#[test]
	fn test_content_type_natural_key_retrieval() {
		let ct = ContentType::new("auth", "user");
		let (app, model) = ct.natural_key();
		assert_eq!(app, "auth");
		assert_eq!(model, "user");
	}

	/// Test: Unknown model handling
	#[test]
	fn test_content_type_unknown_model() {
		let ct = ContentType::new("contenttypes_tests", "unknown");
		assert_eq!(ct.model, "unknown");
		assert_eq!(ct.qualified_name(), "contenttypes_tests.unknown");
	}

	// ========== Registry Cache Tests ==========

	/// Test: get_or_create with same params returns same instance
	/// Inspired by: test_get_for_models_creation
	#[test]
	fn test_get_or_create_returns_same_instance() {
		let registry = setup_registry();

		let ct1 = registry.get_or_create("app1", "Model1");
		let ct2 = registry.get_or_create("app1", "Model1");

		assert_eq!(ct1.id, ct2.id);
		assert_eq!(ct1.app_label, ct2.app_label);
		assert_eq!(ct1.model, ct2.model);
	}

	/// Test: Registry with empty initial state
	/// Inspired by: test_get_for_models_empty_cache
	#[test]
	fn test_registry_empty_initial_state() {
		let registry = setup_registry();

		let ct = registry.get_or_create("contenttypes", "contenttype");
		assert!(ct.id.is_some());
		assert_eq!(ct.app_label, "contenttypes");
		assert_eq!(ct.model, "contenttype");
	}

	/// Test: Registry with partial state
	/// Inspired by: test_get_for_models_partial_cache
	#[test]
	fn test_registry_partial_state() {
		let registry = setup_registry();

		// Pre-register one content type
		registry.get_or_create("app1", "Model1");

		// Get existing and create new
		let ct1 = registry.get("app1", "Model1");
		let ct2 = registry.get_or_create("app2", "Model2");

		assert!(ct1.is_some());
		assert!(ct2.id.is_some());
	}

	/// Test: Registry with full state
	/// Inspired by: test_get_for_models_full_cache
	#[test]
	fn test_registry_full_state() {
		let registry = setup_registry();

		// Pre-register all content types
		registry.get_or_create("contenttypes", "contenttype");
		registry.get_or_create("app1", "model1");

		// All should be available without creation
		let ct1 = registry.get("contenttypes", "contenttype");
		let ct2 = registry.get("app1", "model1");

		assert!(ct1.is_some());
		assert!(ct2.is_some());
	}

	/// Test: Create content type if it doesn't exist
	/// Inspired by: test_get_for_model_create_contenttype
	#[test]
	fn test_get_or_create_creates_if_missing() {
		let registry = setup_registry();

		let ct = registry.get_or_create("contenttypes_tests", "modelcreatedonthefly");
		assert_eq!(ct.app_label, "contenttypes_tests");
		assert_eq!(ct.model, "modelcreatedonthefly");
		assert!(ct.id.is_some());
	}

	/// Test: Separate registries don't share state
	/// Inspired by: test_cache_not_shared_between_managers
	#[test]
	fn test_registries_not_shared() {
		let registry1 = ContentTypeRegistry::new();
		let registry2 = ContentTypeRegistry::new();

		registry1.get_or_create("app1", "Model1");

		// registry2 should not have the content type from registry1
		let ct = registry2.get("app1", "Model1");
		assert!(ct.is_none());
	}

	/// Test: Missing model handling
	/// Inspired by: test_missing_model
	#[test]
	fn test_missing_model_display() {
		let registry = setup_registry();

		let ct = ContentType::new("contenttypes", "OldModel").with_id(999);
		let registered = registry.register(ct.clone());

		assert_eq!(registered.model, "OldModel");

		// Stale ContentTypes can be fetched by ID
		let ct_fetched = registry.get_by_id(999);
		assert!(ct_fetched.is_some());
	}

	/// Test: Missing model with existing model name in another app
	/// Inspired by: test_missing_model_with_existing_model_name
	#[test]
	fn test_missing_model_with_existing_name() {
		let registry = setup_registry();

		// Create a stale ContentType that matches name of existing model
		registry.register(ContentType::new("contenttypes", "author"));

		// get_or_create should work for different app
		let ct_author = registry.get_or_create("contenttypes_tests", "Author");

		assert_eq!(ct_author.app_label, "contenttypes_tests");
		assert_eq!(ct_author.model, "Author");
	}

	// ========== GenericForeignKey Tests ==========

	/// Test: GenericForeignKey respects deleted objects
	/// Inspired by: test_get_object_cache_respects_deleted_objects
	#[test]
	fn test_generic_foreign_key_respects_deletion() {
		let registry = setup_registry();
		let ct = registry.get_or_create("test", "Question");

		let mut gfk = GenericForeignKey::new();
		gfk.set(&ct, 42);

		assert!(gfk.is_set());
		assert_eq!(gfk.object_id, Some(42));

		// Clear simulates deletion
		gfk.clear();
		assert!(!gfk.is_set());
		assert_eq!(gfk.object_id, None);
	}

	/// Test: Clear cached generic relation
	/// Inspired by: test_clear_cached_generic_relation
	#[test]
	fn test_clear_cached_generic_relation() {
		let registry = setup_registry();
		let ct = registry.get_or_create("test", "Question");

		let mut gfk = GenericForeignKey::new();
		gfk.set(&ct, 100);

		let old_ct_id = gfk.content_type_id;

		// Clear and reset
		gfk.clear();
		gfk.set(&ct, 200);

		let new_ct_id = gfk.content_type_id;
		assert_eq!(old_ct_id, new_ct_id);
		assert_eq!(gfk.object_id, Some(200));
	}

	/// Test: GenericForeignKey get content type
	/// Inspired by: test_get_content_type_no_arguments
	/// ========== ContentType Operations Tests ==========
	/// Test: ContentType ID uniqueness
	#[test]
	fn test_content_type_id_uniqueness() {
		let registry = setup_registry();

		let ct1 = registry.get_or_create("app1", "Model1");
		let ct2 = registry.get_or_create("app2", "Model2");

		assert_ne!(ct1.id, ct2.id);
	}

	/// Test: ContentType registry all() method
	#[test]
	fn test_registry_all_listing() {
		let registry = setup_registry();

		registry.get_or_create("app1", "Model1");
		registry.get_or_create("app2", "Model2");
		registry.get_or_create("app3", "Model3");

		let all = registry.all();
		assert_eq!(all.len(), 3);
	}

	/// Test: ContentType with special characters
	#[test]
	fn test_content_type_special_characters() {
		let registry = setup_registry();

		let ct = registry.get_or_create("my_app", "My_Model");
		assert_eq!(ct.app_label, "my_app");
		assert_eq!(ct.model, "My_Model");
	}

	// ========== GenericRelationQuery Tests ==========

	/// Test: Generic relation query SQL generation
	#[test]
	fn test_generic_relation_query_sql() {
		let ct = ContentType::new("shop", "Product").with_id(5);
		let mut query = GenericRelationQuery::new(ct);

		query.add_object(10);
		query.add_object(20);
		query.add_object(30);

		let sql = query.to_sql("ratings");
		assert!(sql.contains("content_type_id = 5"));
		assert!(sql.contains("object_id IN (10, 20, 30)"));
		assert!(sql.contains("FROM ratings"));
	}

	/// Test: Generic relation query with empty objects
	#[test]
	fn test_generic_relation_query_empty() {
		let ct = ContentType::new("test", "Model").with_id(1);
		let query = GenericRelationQuery::new(ct);

		let sql = query.to_sql("items");
		assert!(sql.contains("content_type_id = 1"));
		assert!(sql.contains("object_id IN ()"));
	}

	/// Test: Generic relation query with single object
	#[test]
	fn test_generic_relation_query_single() {
		let ct = ContentType::new("blog", "Post").with_id(3);
		let mut query = GenericRelationQuery::new(ct);

		query.add_object(42);

		let sql = query.to_sql("comments");
		assert!(sql.contains("content_type_id = 3"));
		assert!(sql.contains("object_id IN (42)"));
	}

	// ========== Edge Cases and Error Conditions ==========

	/// Test: Registry clear removes all content types
	#[test]
	fn test_registry_clear_removes_all() {
		let registry = setup_registry();

		registry.get_or_create("app1", "Model1");
		registry.get_or_create("app2", "Model2");

		assert_eq!(registry.all().len(), 2);

		registry.clear();
		assert_eq!(registry.all().len(), 0);
	}

	/// Test: Get non-existent content type returns None
	#[test]
	fn test_get_nonexistent_returns_none() {
		let registry = setup_registry();

		let ct = registry.get("nonexistent", "Model");
		assert!(ct.is_none());
	}

	/// Test: Get by non-existent ID returns None
	#[test]
	fn test_get_by_nonexistent_id_returns_none() {
		let registry = setup_registry();

		let ct = registry.get_by_id(99999);
		assert!(ct.is_none());
	}

	/// Test: ContentType equality
	#[test]
	fn test_content_type_equality() {
		let ct1 = ContentType::new("app", "Model");
		let ct2 = ContentType::new("app", "Model");

		assert_eq!(ct1, ct2);
	}

	/// Test: ContentType inequality with different app
	#[test]
	fn test_content_type_inequality_different_app() {
		let ct1 = ContentType::new("app1", "Model");
		let ct2 = ContentType::new("app2", "Model");

		assert_ne!(ct1, ct2);
	}

	/// Test: ContentType inequality with different model
	#[test]
	fn test_content_type_inequality_different_model() {
		let ct1 = ContentType::new("app", "Model1");
		let ct2 = ContentType::new("app", "Model2");

		assert_ne!(ct1, ct2);
	}

	/// Test: GenericForeignKey default state
	#[test]
	fn test_generic_foreign_key_default() {
		let gfk = GenericForeignKey::default();

		assert!(!gfk.is_set());
		assert_eq!(gfk.content_type_id, None);
		assert_eq!(gfk.object_id, None);
	}

	/// Test: ContentType cloning
	#[test]
	fn test_content_type_clone() {
		let ct1 = ContentType::new("app", "Model").with_id(42);
		let ct2 = ct1.clone();

		assert_eq!(ct1, ct2);
		assert_eq!(ct1.id, ct2.id);
	}

	/// Test: Registry handles concurrent-like operations
	#[test]
	fn test_registry_multiple_operations() {
		let registry = setup_registry();

		// Simulate multiple operations
		let ct1 = registry.get_or_create("app1", "Model1");
		let ct2 = registry.get_or_create("app2", "Model2");
		let ct1_again = registry.get("app1", "Model1");
		let ct2_by_id = registry.get_by_id(ct2.id.unwrap());

		assert!(ct1_again.is_some());
		assert_eq!(ct1.id, ct1_again.unwrap().id);
		assert!(ct2_by_id.is_some());
		assert_eq!(ct2.id, ct2_by_id.unwrap().id);
	}

	/// Test: Case sensitivity in app and model names
	#[test]
	fn test_case_sensitivity() {
		let registry = setup_registry();

		let ct1 = registry.get_or_create("App", "Model");
		let ct2 = registry.get_or_create("app", "model");

		// They should be different
		assert_ne!(ct1.id, ct2.id);
	}
}