reinhardt-apps 0.2.1

Application registry and management 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
//! Global model registry
//!
//! This module provides a global registry for models, allowing models to be
//! discovered and registered at compile time using the `linkme` crate.
//!
//! # Examples
//!
//! ```rust
//! use reinhardt_apps::registry::{ModelMetadata, get_registered_models};
//!
//! // Register a model (typically done via derive macro)
//! #[linkme::distributed_slice(reinhardt_apps::registry::MODELS)]
//! static MY_MODEL: ModelMetadata = ModelMetadata {
//!     app_label: "myapp",
//!     model_name: "User",
//!     table_name: "users",
//! };
//!
//! // Access registered models
//! let models = get_registered_models();
//! // Note: In doc tests, the model may not be visible due to linkme limitations
//! ```

use linkme::distributed_slice;
use std::collections::HashMap;
use std::sync::{OnceLock, PoisonError, RwLock};

/// Metadata for a registered model
///
/// This structure contains essential information about a model that has been
/// registered in the global model registry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModelMetadata {
	/// The label of the application this model belongs to
	pub app_label: &'static str,

	/// The name of the model (e.g., "User", "Post")
	pub model_name: &'static str,

	/// The database table name for this model
	pub table_name: &'static str,
}

impl ModelMetadata {
	/// Create a new model metadata instance
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_apps::registry::ModelMetadata;
	///
	/// let metadata = ModelMetadata::new("myapp", "User", "users");
	/// assert_eq!(metadata.app_label, "myapp");
	/// assert_eq!(metadata.model_name, "User");
	/// assert_eq!(metadata.table_name, "users");
	/// ```
	pub const fn new(
		app_label: &'static str,
		model_name: &'static str,
		table_name: &'static str,
	) -> Self {
		Self {
			app_label,
			model_name,
			table_name,
		}
	}

	/// Get the fully qualified model name (app_label.model_name)
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_apps::registry::ModelMetadata;
	///
	/// let metadata = ModelMetadata::new("myapp", "User", "users");
	/// assert_eq!(metadata.qualified_name(), "myapp.User");
	/// ```
	pub fn qualified_name(&self) -> String {
		format!("{}.{}", self.app_label, self.model_name)
	}
}

/// Global distributed slice for model registration
///
/// This is the global registry where all models are collected at link time.
/// Models can be registered by adding items to this slice using the `#[distributed_slice]`
/// attribute from the `linkme` crate.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::{MODELS, ModelMetadata};
///
/// #[linkme::distributed_slice(MODELS)]
/// static MY_MODEL: ModelMetadata = ModelMetadata {
///     app_label: "myapp",
///     model_name: "User",
///     table_name: "users",
/// };
/// ```
#[distributed_slice]
pub static MODELS: [ModelMetadata];

/// Cache for model lookups by app label.
/// Lazily initialized on first access and immutable thereafter.
static MODEL_CACHE: OnceLock<HashMap<&'static str, Vec<&'static ModelMetadata>>> = OnceLock::new();

/// Returns the cached model metadata indexed by app label.
fn model_cache() -> &'static HashMap<&'static str, Vec<&'static ModelMetadata>> {
	MODEL_CACHE.get_or_init(|| {
		let mut cache: HashMap<&'static str, Vec<&'static ModelMetadata>> = HashMap::new();
		for model in MODELS.iter() {
			cache.entry(model.app_label).or_default().push(model);
		}
		cache
	})
}

/// Get all registered models
///
/// This function returns a slice of all models that have been registered
/// in the global model registry.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::get_registered_models;
///
/// let models = get_registered_models();
/// println!("Found {} registered models", models.len());
/// ```
pub fn get_registered_models() -> &'static [ModelMetadata] {
	&MODELS
}

/// Get models for a specific application.
///
/// This function returns all models that belong to the specified application label.
/// Results are cached for performance on subsequent calls.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::get_models_for_app;
///
/// let auth_models = get_models_for_app("auth");
/// for model in auth_models {
///     println!("Model: {}", model.model_name);
/// }
/// ```
pub fn get_models_for_app(app_label: &str) -> Vec<&'static ModelMetadata> {
	model_cache().get(app_label).cloned().unwrap_or_default()
}

/// Find a model by its qualified name (app_label.model_name)
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::find_model;
///
/// if let Some(model) = find_model("myapp.User") {
///     println!("Found model: {}", model.model_name);
/// } else {
///     println!("Model not found");
/// }
/// ```
pub fn find_model(qualified_name: &str) -> Option<&'static ModelMetadata> {
	let parts: Vec<&str> = qualified_name.split('.').collect();
	if parts.len() != 2 {
		return None;
	}

	let (app_label, model_name) = (parts[0], parts[1]);
	MODELS
		.iter()
		.find(|m| m.app_label == app_label && m.model_name == model_name)
}

/// Metadata for a forward relationship
///
/// This structure contains information about a relationship field defined in a model.
/// It is populated at compile time via derive macros and stored in the global RELATIONSHIPS slice.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationshipMetadata {
	/// The model that owns this relationship (e.g., "blog.Post")
	pub from_model: &'static str,

	/// The target model of this relationship (e.g., "auth.User")
	pub to_model: &'static str,

	/// The type of relationship
	pub relationship_type: RelationshipType,

	/// The field name in the source model (e.g., "author", "tags")
	pub field_name: &'static str,

	/// The related name for reverse access (e.g., "posts", "authored_posts")
	/// If None, will be auto-generated as "{model_name}_set"
	pub related_name: Option<&'static str>,

	/// The database column name (for ForeignKey fields)
	/// None for ManyToMany relationships
	pub db_column: Option<&'static str>,

	/// The through table name (for ManyToMany relationships)
	/// None for ForeignKey and OneToOne relationships
	pub through_table: Option<&'static str>,
}

/// Type of relationship between models
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelationshipType {
	/// Foreign key relationship (Many-to-One from the perspective of the field)
	ForeignKey,
	/// Many-to-Many relationship
	ManyToMany,
	/// One-to-One relationship
	OneToOne,
}

impl RelationshipMetadata {
	/// Create a new relationship metadata instance
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_apps::registry::{RelationshipMetadata, RelationshipType};
	///
	/// let relationship = RelationshipMetadata::new(
	///     "blog.Post",
	///     "auth.User",
	///     RelationshipType::ForeignKey,
	///     "author",
	///     Some("posts"),
	///     Some("author_id"),
	///     None,
	/// );
	/// assert_eq!(relationship.field_name, "author");
	/// ```
	pub const fn new(
		from_model: &'static str,
		to_model: &'static str,
		relationship_type: RelationshipType,
		field_name: &'static str,
		related_name: Option<&'static str>,
		db_column: Option<&'static str>,
		through_table: Option<&'static str>,
	) -> Self {
		Self {
			from_model,
			to_model,
			relationship_type,
			field_name,
			related_name,
			db_column,
			through_table,
		}
	}

	/// Get the source model name without app label
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_apps::registry::{RelationshipMetadata, RelationshipType};
	///
	/// let relationship = RelationshipMetadata::new(
	///     "blog.Post",
	///     "auth.User",
	///     RelationshipType::ForeignKey,
	///     "author",
	///     None,
	///     None,
	///     None,
	/// );
	/// assert_eq!(relationship.from_model_name(), "Post");
	/// ```
	pub fn from_model_name(&self) -> &str {
		self.from_model
			.split('.')
			.next_back()
			.unwrap_or(self.from_model)
	}

	/// Get the target model name without app label
	///
	/// # Examples
	///
	/// ```rust
	/// use reinhardt_apps::registry::{RelationshipMetadata, RelationshipType};
	///
	/// let relationship = RelationshipMetadata::new(
	///     "blog.Post",
	///     "auth.User",
	///     RelationshipType::ForeignKey,
	///     "author",
	///     None,
	///     None,
	///     None,
	/// );
	/// assert_eq!(relationship.to_model_name(), "User");
	/// ```
	pub fn to_model_name(&self) -> &str {
		self.to_model
			.split('.')
			.next_back()
			.unwrap_or(self.to_model)
	}
}

/// Global distributed slice for relationship registration
///
/// This is the global registry where all relationships are collected at compile time.
/// Relationships can be registered by adding items to this slice using the `#[distributed_slice]`
/// attribute from the `linkme` crate.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::{RELATIONSHIPS, RelationshipMetadata, RelationshipType};
///
/// #[linkme::distributed_slice(RELATIONSHIPS)]
/// static POST_AUTHOR: RelationshipMetadata = RelationshipMetadata {
///     from_model: "blog.Post",
///     to_model: "auth.User",
///     relationship_type: RelationshipType::ForeignKey,
///     field_name: "author",
///     related_name: Some("posts"),
///     db_column: Some("author_id"),
///     through_table: None,
/// };
/// ```
#[distributed_slice]
pub static RELATIONSHIPS: [RelationshipMetadata];

/// Cache for relationship lookups by model.
/// Lazily initialized on first access and immutable thereafter.
static RELATIONSHIP_CACHE: OnceLock<HashMap<&'static str, Vec<&'static RelationshipMetadata>>> =
	OnceLock::new();

/// Returns the cached relationship metadata indexed by source model.
fn relationship_cache() -> &'static HashMap<&'static str, Vec<&'static RelationshipMetadata>> {
	RELATIONSHIP_CACHE.get_or_init(|| {
		let mut cache: HashMap<&'static str, Vec<&'static RelationshipMetadata>> = HashMap::new();
		for rel in RELATIONSHIPS.iter() {
			cache.entry(rel.from_model).or_default().push(rel);
		}
		cache
	})
}

/// Get all registered relationships
///
/// This function returns a slice of all relationships that have been registered
/// in the global relationship registry.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::get_registered_relationships;
///
/// let relationships = get_registered_relationships();
/// println!("Found {} registered relationships", relationships.len());
/// ```
pub fn get_registered_relationships() -> &'static [RelationshipMetadata] {
	&RELATIONSHIPS
}

/// Get all relationships originating from a specific model.
pub fn get_relationships_for_model(model: &str) -> Vec<&'static RelationshipMetadata> {
	relationship_cache().get(model).cloned().unwrap_or_default()
}

/// Find relationships by target model
///
/// This function returns all relationships that point to the specified target model.
/// Useful for discovering reverse relationships.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::get_relationships_to_model;
///
/// let user_reverse_rels = get_relationships_to_model("auth.User");
/// for rel in user_reverse_rels {
///     println!("Reverse relationship from {}.{}", rel.from_model, rel.field_name);
/// }
/// ```
pub fn get_relationships_to_model(target_model: &str) -> Vec<&'static RelationshipMetadata> {
	RELATIONSHIPS
		.iter()
		.filter(|r| r.to_model == target_model)
		.collect()
}

/// Metadata for a reverse relation
///
/// This structure represents a reverse relation that has been dynamically
/// registered for lazy loading or eager loading via select_related.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReverseRelationMetadata {
	/// The model this reverse relation belongs to (e.g., "User")
	pub on_model: &'static str,

	/// The accessor name (e.g., "posts" or "post_set")
	pub accessor_name: String,

	/// The related model (e.g., "Post")
	pub related_model: &'static str,

	/// The type of reverse relation
	pub relation_type: ReverseRelationType,

	/// The original field name in the related model
	pub through_field: &'static str,
}

/// Type of reverse relationship
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReverseRelationType {
	/// Reverse of OneToMany (collection)
	ReverseOneToMany,
	/// Reverse of ManyToMany (collection)
	ReverseManyToMany,
	/// Reverse of OneToOne (single object)
	ReverseOneToOne,
}

impl ReverseRelationMetadata {
	/// Create a new reverse relation metadata instance
	pub fn new(
		on_model: &'static str,
		accessor_name: String,
		related_model: &'static str,
		relation_type: ReverseRelationType,
		through_field: &'static str,
	) -> Self {
		Self {
			on_model,
			accessor_name,
			related_model,
			relation_type,
			through_field,
		}
	}
}

/// Builder storage used during initialization phase only.
/// Reverse relations are added here before finalization.
static REVERSE_RELATIONS_BUILDER: RwLock<Vec<ReverseRelationMetadata>> = RwLock::new(Vec::new());

/// Finalized map indexed by model name (read-only after initialization).
/// This is populated by `finalize_reverse_relations()` and accessed by
/// `get_reverse_relations_for_model()`.
static REVERSE_RELATIONS: OnceLock<HashMap<String, Vec<ReverseRelationMetadata>>> = OnceLock::new();

/// Registers a reverse relation during the initialization phase.
///
/// Must be called before `finalize_reverse_relations()`.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::{register_reverse_relation, ReverseRelationMetadata, ReverseRelationType};
///
/// let reverse_relation = ReverseRelationMetadata::new(
///     "User",
///     "posts".to_string(),
///     "Post",
///     ReverseRelationType::ReverseOneToMany,
///     "author",
/// );
/// register_reverse_relation(reverse_relation).unwrap();
/// ```
///
/// # Errors
///
/// Returns [`crate::AppError::RegistryState`] if called after `finalize_reverse_relations()`.
pub fn register_reverse_relation(relation: ReverseRelationMetadata) -> Result<(), crate::AppError> {
	if REVERSE_RELATIONS.get().is_some() {
		return Err(crate::AppError::RegistryState(
			"Cannot register reverse relations after finalization".to_string(),
		));
	}
	// Recover from poisoned lock to prevent cascading panics
	let mut builder = REVERSE_RELATIONS_BUILDER
		.write()
		.unwrap_or_else(PoisonError::into_inner);
	builder.push(relation);
	Ok(())
}

/// Finalizes the reverse relations, making them immutable.
///
/// This function should be called at the end of `Apps::populate()` after all
/// reverse relations have been registered. After this call, `register_reverse_relation()`
/// will panic if called again.
pub fn finalize_reverse_relations() {
	if REVERSE_RELATIONS.get().is_some() {
		return;
	}
	// Recover from poisoned lock to prevent cascading panics
	let builder = REVERSE_RELATIONS_BUILDER
		.read()
		.unwrap_or_else(PoisonError::into_inner);
	let mut indexed = HashMap::new();
	for relation in builder.iter() {
		indexed
			.entry(relation.on_model.to_string())
			.or_insert_with(Vec::new)
			.push(relation.clone());
	}
	let _ = REVERSE_RELATIONS.set(indexed);
}

/// Returns all reverse relations for a specific model.
///
/// # Examples
///
/// ```rust
/// use reinhardt_apps::registry::get_reverse_relations_for_model;
///
/// let relations = get_reverse_relations_for_model("User");
/// for relation in relations {
///     println!("Reverse relation: {}", relation.accessor_name);
/// }
/// ```
pub fn get_reverse_relations_for_model(model_name: &str) -> Vec<ReverseRelationMetadata> {
	REVERSE_RELATIONS
		.get()
		.and_then(|m| m.get(model_name))
		.cloned()
		.unwrap_or_default()
}

/// Resets all global registry caches for test isolation.
///
/// This clears the `MODEL_CACHE`, `RELATIONSHIP_CACHE`, and `REVERSE_RELATIONS`
/// `OnceLock` instances so that each test can start with a clean slate.
/// Also clears the `REVERSE_RELATIONS_BUILDER` `RwLock` vec.
///
/// # Safety
///
/// This function replaces static `OnceLock` values using `std::ptr::write`.
/// It is only safe to call from a single-threaded test context (e.g., with
/// `#[serial]`) where no other thread is concurrently reading these statics.
#[cfg(any(test, feature = "testing"))]
pub fn reset_global_registry() {
	use std::sync::PoisonError;

	// Clear the builder vec
	let mut builder = REVERSE_RELATIONS_BUILDER
		.write()
		.unwrap_or_else(PoisonError::into_inner);
	builder.clear();
	drop(builder);

	// SAFETY: We replace each OnceLock in-place with a fresh instance.
	// This is safe only when called from a single-threaded test context
	// (enforced by #[serial]) where no concurrent readers exist.
	unsafe {
		let model_cache_ptr = std::ptr::addr_of!(MODEL_CACHE)
			as *mut OnceLock<HashMap<&'static str, Vec<&'static ModelMetadata>>>;
		std::ptr::write(model_cache_ptr, OnceLock::new());

		let rel_cache_ptr = std::ptr::addr_of!(RELATIONSHIP_CACHE)
			as *mut OnceLock<HashMap<&'static str, Vec<&'static RelationshipMetadata>>>;
		std::ptr::write(rel_cache_ptr, OnceLock::new());

		let rev_rel_ptr = std::ptr::addr_of!(REVERSE_RELATIONS)
			as *mut OnceLock<HashMap<String, Vec<ReverseRelationMetadata>>>;
		std::ptr::write(rev_rel_ptr, OnceLock::new());
	}
}

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

	// Test model registrations
	#[distributed_slice(MODELS)]
	static TEST_USER_MODEL: ModelMetadata = ModelMetadata {
		app_label: "auth",
		model_name: "User",
		table_name: "auth_users",
	};

	#[distributed_slice(MODELS)]
	static TEST_POST_MODEL: ModelMetadata = ModelMetadata {
		app_label: "blog",
		model_name: "Post",
		table_name: "blog_posts",
	};

	#[distributed_slice(MODELS)]
	static TEST_COMMENT_MODEL: ModelMetadata = ModelMetadata {
		app_label: "blog",
		model_name: "Comment",
		table_name: "blog_comments",
	};

	#[test]
	fn test_model_metadata_new() {
		let metadata = ModelMetadata::new("myapp", "MyModel", "my_table");
		assert_eq!(metadata.app_label, "myapp");
		assert_eq!(metadata.model_name, "MyModel");
		assert_eq!(metadata.table_name, "my_table");
	}

	#[test]
	fn test_qualified_name() {
		let metadata = ModelMetadata::new("auth", "User", "users");
		assert_eq!(metadata.qualified_name(), "auth.User");
	}

	#[test]
	fn test_find_model_invalid_format() {
		let model = find_model("InvalidFormat");
		assert!(model.is_none());

		let model = find_model("too.many.parts");
		assert!(model.is_none());
	}

	#[test]
	fn test_model_metadata_equality() {
		let meta1 = ModelMetadata::new("app", "Model", "table");
		let meta2 = ModelMetadata::new("app", "Model", "table");
		let meta3 = ModelMetadata::new("app", "Other", "table");

		assert_eq!(meta1, meta2);
		assert_ne!(meta1, meta3);
	}

	#[test]
	fn test_reverse_relation_metadata_new() {
		let relation = ReverseRelationMetadata::new(
			"User",
			"posts".to_string(),
			"Post",
			ReverseRelationType::ReverseOneToMany,
			"author",
		);

		assert_eq!(relation.on_model, "User");
		assert_eq!(relation.accessor_name, "posts");
		assert_eq!(relation.related_model, "Post");
		assert_eq!(
			relation.relation_type,
			ReverseRelationType::ReverseOneToMany
		);
		assert_eq!(relation.through_field, "author");
	}

	#[rstest]
	fn test_get_reverse_relations_for_nonexistent_model() {
		// Before finalization, returns empty vec
		let relations = get_reverse_relations_for_model("NonExistent");
		assert!(relations.is_empty());
	}

	#[test]
	fn test_reverse_relation_types() {
		assert_eq!(
			ReverseRelationType::ReverseOneToMany,
			ReverseRelationType::ReverseOneToMany
		);
		assert_ne!(
			ReverseRelationType::ReverseOneToMany,
			ReverseRelationType::ReverseManyToMany
		);
		assert_ne!(
			ReverseRelationType::ReverseOneToMany,
			ReverseRelationType::ReverseOneToOne
		);
	}

	// Test relationship metadata
	#[distributed_slice(RELATIONSHIPS)]
	static TEST_POST_AUTHOR: RelationshipMetadata = RelationshipMetadata {
		from_model: "blog.Post",
		to_model: "auth.User",
		relationship_type: RelationshipType::ForeignKey,
		field_name: "author",
		related_name: Some("posts"),
		db_column: Some("author_id"),
		through_table: None,
	};

	#[distributed_slice(RELATIONSHIPS)]
	static TEST_POST_TAGS: RelationshipMetadata = RelationshipMetadata {
		from_model: "blog.Post",
		to_model: "blog.Tag",
		relationship_type: RelationshipType::ManyToMany,
		field_name: "tags",
		related_name: Some("posts"),
		db_column: None,
		through_table: Some("blog_post_tags"),
	};

	#[test]
	fn test_relationship_metadata_new() {
		let relationship = RelationshipMetadata::new(
			"blog.Post",
			"auth.User",
			RelationshipType::ForeignKey,
			"author",
			Some("posts"),
			Some("author_id"),
			None,
		);

		assert_eq!(relationship.from_model, "blog.Post");
		assert_eq!(relationship.to_model, "auth.User");
		assert_eq!(relationship.relationship_type, RelationshipType::ForeignKey);
		assert_eq!(relationship.field_name, "author");
		assert_eq!(relationship.related_name, Some("posts"));
		assert_eq!(relationship.db_column, Some("author_id"));
		assert_eq!(relationship.through_table, None);
	}

	#[test]
	fn test_relationship_metadata_model_names() {
		let relationship = RelationshipMetadata::new(
			"blog.Post",
			"auth.User",
			RelationshipType::ForeignKey,
			"author",
			None,
			None,
			None,
		);

		assert_eq!(relationship.from_model_name(), "Post");
		assert_eq!(relationship.to_model_name(), "User");
	}

	#[test]
	fn test_relationship_types() {
		assert_eq!(RelationshipType::ForeignKey, RelationshipType::ForeignKey);
		assert_ne!(RelationshipType::ForeignKey, RelationshipType::ManyToMany);
		assert_ne!(RelationshipType::ForeignKey, RelationshipType::OneToOne);
	}

	#[test]
	fn test_relationship_metadata_equality() {
		let rel1 = RelationshipMetadata::new(
			"app.Model",
			"app.Other",
			RelationshipType::ForeignKey,
			"field",
			None,
			None,
			None,
		);
		let rel2 = RelationshipMetadata::new(
			"app.Model",
			"app.Other",
			RelationshipType::ForeignKey,
			"field",
			None,
			None,
			None,
		);
		let rel3 = RelationshipMetadata::new(
			"app.Model",
			"app.Other",
			RelationshipType::ManyToMany,
			"field",
			None,
			None,
			None,
		);

		assert_eq!(rel1, rel2);
		assert_ne!(rel1, rel3);
	}

	#[rstest]
	fn test_rwlock_poison_recovery_write() {
		// Arrange
		let lock = RwLock::new(vec![1, 2, 3]);

		// Poison the lock by panicking inside a write guard
		let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
			let _guard = lock.write().unwrap();
			panic!("intentional panic to poison the lock");
		}));

		// Act: recover from poisoned lock using PoisonError::into_inner
		let mut guard = lock.write().unwrap_or_else(PoisonError::into_inner);
		guard.push(4);

		// Assert: data is accessible and intact after recovery
		assert_eq!(*guard, vec![1, 2, 3, 4]);
	}

	#[rstest]
	fn test_rwlock_poison_recovery_read() {
		// Arrange
		let lock = RwLock::new(vec![10, 20, 30]);

		// Poison the lock by panicking inside a write guard
		let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
			let _guard = lock.write().unwrap();
			panic!("intentional panic to poison the lock");
		}));

		// Act: recover from poisoned lock using PoisonError::into_inner
		let guard = lock.read().unwrap_or_else(PoisonError::into_inner);

		// Assert: data is readable after recovery
		assert_eq!(*guard, vec![10, 20, 30]);
	}
}