reinhardt-db 0.1.2

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
//! GenericForeignKey with database constraints support
//!
//! This module provides enhanced GenericForeignKey functionality with optional
//! database-level foreign key constraints, similar to Django's contenttypes framework.
//!
//! ## Features
//!
//! - Type-safe generic foreign keys
//! - Optional database constraints validation
//! - Integration with ContentType persistence
//!
//! ## Example
//!
//! ```rust
//! use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
//! use reinhardt_db::contenttypes::ContentType;
//!
//! let mut gfk = GenericForeignKeyField::new();
//! let ct = ContentType::new("blog", "Post").with_id(1);
//!
//! gfk.set(&ct, 42);
//! assert!(gfk.is_set());
//! assert_eq!(gfk.object_id(), Some(42));
//! ```

use serde::{Deserialize, Serialize};

use super::ContentType;

/// Enhanced GenericForeignKey field with constraint support
///
/// Provides a generic foreign key implementation with optional database constraint
/// validation. When database feature is enabled, can validate references against
/// actual database records.
///
/// ## Example
///
/// ```rust
/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
/// use reinhardt_db::contenttypes::ContentType;
///
/// let mut gfk = GenericForeignKeyField::new();
/// let ct = ContentType::new("auth", "User").with_id(5);
///
/// gfk.set(&ct, 123);
/// assert_eq!(gfk.content_type_id(), Some(5));
/// assert_eq!(gfk.object_id(), Some(123));
///
/// gfk.clear();
/// assert!(!gfk.is_set());
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct GenericForeignKeyField {
	content_type_id: Option<i64>,
	object_id: Option<i64>,
}

impl GenericForeignKeyField {
	/// Create a new unset GenericForeignKey field
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let gfk = GenericForeignKeyField::new();
	/// assert!(!gfk.is_set());
	/// ```
	pub fn new() -> Self {
		Self {
			content_type_id: None,
			object_id: None,
		}
	}

	/// Create a GenericForeignKey with values
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let gfk = GenericForeignKeyField::with_values(Some(1), Some(42));
	/// assert!(gfk.is_set());
	/// assert_eq!(gfk.content_type_id(), Some(1));
	/// assert_eq!(gfk.object_id(), Some(42));
	/// ```
	pub fn with_values(content_type_id: Option<i64>, object_id: Option<i64>) -> Self {
		Self {
			content_type_id,
			object_id,
		}
	}

	/// Set the GenericForeignKey to reference a specific object
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	/// use reinhardt_db::contenttypes::ContentType;
	///
	/// let mut gfk = GenericForeignKeyField::new();
	/// let ct = ContentType::new("shop", "Product").with_id(3);
	///
	/// gfk.set(&ct, 99);
	/// assert!(gfk.is_set());
	/// ```
	pub fn set(&mut self, content_type: &ContentType, object_id: i64) {
		self.content_type_id = content_type.id;
		self.object_id = Some(object_id);
	}

	/// Get the content type ID
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let gfk = GenericForeignKeyField::with_values(Some(5), Some(10));
	/// assert_eq!(gfk.content_type_id(), Some(5));
	/// ```
	pub fn content_type_id(&self) -> Option<i64> {
		self.content_type_id
	}

	/// Get the object ID
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let gfk = GenericForeignKeyField::with_values(Some(5), Some(10));
	/// assert_eq!(gfk.object_id(), Some(10));
	/// ```
	pub fn object_id(&self) -> Option<i64> {
		self.object_id
	}

	/// Check if the GenericForeignKey is set
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let gfk = GenericForeignKeyField::new();
	/// assert!(!gfk.is_set());
	///
	/// let gfk = GenericForeignKeyField::with_values(Some(1), Some(1));
	/// assert!(gfk.is_set());
	/// ```
	pub fn is_set(&self) -> bool {
		self.content_type_id.is_some() && self.object_id.is_some()
	}

	/// Clear the GenericForeignKey
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let mut gfk = GenericForeignKeyField::with_values(Some(1), Some(1));
	/// assert!(gfk.is_set());
	///
	/// gfk.clear();
	/// assert!(!gfk.is_set());
	/// ```
	pub fn clear(&mut self) {
		self.content_type_id = None;
		self.object_id = None;
	}

	/// Set content type ID directly
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let mut gfk = GenericForeignKeyField::new();
	/// gfk.set_content_type_id(Some(7));
	/// assert_eq!(gfk.content_type_id(), Some(7));
	/// ```
	pub fn set_content_type_id(&mut self, id: Option<i64>) {
		self.content_type_id = id;
	}

	/// Set object ID directly
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	///
	/// let mut gfk = GenericForeignKeyField::new();
	/// gfk.set_object_id(Some(42));
	/// assert_eq!(gfk.object_id(), Some(42));
	/// ```
	pub fn set_object_id(&mut self, id: Option<i64>) {
		self.object_id = id;
	}

	/// Get the content type from registry
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::contenttypes::generic_fk::GenericForeignKeyField;
	/// use reinhardt_db::contenttypes::{ContentType, CONTENT_TYPE_REGISTRY};
	///
	/// // Register a content type
	/// let ct = CONTENT_TYPE_REGISTRY.register(ContentType::new("blog", "Comment"));
	///
	/// let mut gfk = GenericForeignKeyField::new();
	/// gfk.set(&ct, 100);
	///
	/// let retrieved_ct = gfk.get_content_type();
	/// assert!(retrieved_ct.is_some());
	/// assert_eq!(retrieved_ct.unwrap().model, "Comment");
	/// ```
	pub fn get_content_type(&self) -> Option<ContentType> {
		self.content_type_id
			.and_then(|id| super::CONTENT_TYPE_REGISTRY.get_by_id(id))
	}
}

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

/// Database constraint validation for GenericForeignKey
///
/// When the database feature is enabled, this trait provides methods to validate
/// that GenericForeignKey references point to existing database records.
#[cfg(feature = "database")]
pub mod constraints {
	use super::*;
	use crate::contenttypes::persistence::{
		ContentTypePersistence, ContentTypePersistenceBackend, PersistenceError,
	};

	/// Trait for validating GenericForeignKey constraints
	#[async_trait::async_trait]
	pub trait GenericForeignKeyConstraints {
		/// Validate that the GenericForeignKey references an existing content type
		async fn validate_content_type(
			&self,
			persistence: &ContentTypePersistence,
		) -> Result<bool, PersistenceError>;

		/// Get the validated content type from database
		async fn get_validated_content_type(
			&self,
			persistence: &ContentTypePersistence,
		) -> Result<Option<ContentType>, PersistenceError>;
	}

	#[async_trait::async_trait]
	impl GenericForeignKeyConstraints for GenericForeignKeyField {
		async fn validate_content_type(
			&self,
			persistence: &ContentTypePersistence,
		) -> Result<bool, PersistenceError> {
			if let Some(ct_id) = self.content_type_id {
				let ct = persistence.get_by_id(ct_id).await?;
				Ok(ct.is_some())
			} else {
				Ok(false)
			}
		}

		async fn get_validated_content_type(
			&self,
			persistence: &ContentTypePersistence,
		) -> Result<Option<ContentType>, PersistenceError> {
			if let Some(ct_id) = self.content_type_id {
				persistence.get_by_id(ct_id).await
			} else {
				Ok(None)
			}
		}
	}
}

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

	#[test]
	fn test_generic_fk_field_new() {
		let gfk = GenericForeignKeyField::new();
		assert!(!gfk.is_set());
		assert_eq!(gfk.content_type_id(), None);
		assert_eq!(gfk.object_id(), None);
	}

	#[test]
	fn test_generic_fk_field_with_values() {
		let gfk = GenericForeignKeyField::with_values(Some(5), Some(42));
		assert!(gfk.is_set());
		assert_eq!(gfk.content_type_id(), Some(5));
		assert_eq!(gfk.object_id(), Some(42));
	}

	#[test]
	fn test_generic_fk_field_set() {
		let mut gfk = GenericForeignKeyField::new();
		let ct = ContentType::new("blog", "Post").with_id(3);

		gfk.set(&ct, 99);
		assert!(gfk.is_set());
		assert_eq!(gfk.content_type_id(), Some(3));
		assert_eq!(gfk.object_id(), Some(99));
	}

	#[test]
	fn test_generic_fk_field_clear() {
		let mut gfk = GenericForeignKeyField::with_values(Some(1), Some(1));
		assert!(gfk.is_set());

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

	#[test]
	fn test_generic_fk_field_setters() {
		let mut gfk = GenericForeignKeyField::new();

		gfk.set_content_type_id(Some(10));
		assert_eq!(gfk.content_type_id(), Some(10));
		assert!(!gfk.is_set()); // Not fully set yet

		gfk.set_object_id(Some(20));
		assert_eq!(gfk.object_id(), Some(20));
		assert!(gfk.is_set()); // Now fully set
	}

	#[test]
	fn test_generic_fk_field_default() {
		let gfk = GenericForeignKeyField::default();
		assert!(!gfk.is_set());
	}

	#[test]
	fn test_generic_fk_field_get_content_type() {
		use crate::contenttypes::CONTENT_TYPE_REGISTRY;

		// Clear registry first
		CONTENT_TYPE_REGISTRY.clear();

		let ct = CONTENT_TYPE_REGISTRY.register(ContentType::new("test", "Model"));
		let mut gfk = GenericForeignKeyField::new();

		gfk.set(&ct, 42);
		let retrieved = gfk.get_content_type();

		assert!(retrieved.is_some());
		assert_eq!(retrieved.unwrap().model, "Model");

		// Clean up
		CONTENT_TYPE_REGISTRY.clear();
	}

	#[test]
	fn test_generic_fk_field_partial_set() {
		let mut gfk = GenericForeignKeyField::new();

		// Only content type ID set
		gfk.set_content_type_id(Some(1));
		assert!(!gfk.is_set());

		// Only object ID set
		let mut gfk2 = GenericForeignKeyField::new();
		gfk2.set_object_id(Some(1));
		assert!(!gfk2.is_set());

		// Both set
		gfk.set_object_id(Some(1));
		assert!(gfk.is_set());
	}

	#[test]
	fn test_generic_fk_field_serialization() {
		let gfk = GenericForeignKeyField::with_values(Some(5), Some(10));

		// Serialize
		let serialized = serde_json::to_string(&gfk).unwrap();
		assert!(serialized.contains("5"));
		assert!(serialized.contains("10"));

		// Deserialize
		let deserialized: GenericForeignKeyField = serde_json::from_str(&serialized).unwrap();
		assert_eq!(deserialized, gfk);
	}

	#[test]
	fn test_generic_fk_field_equality() {
		let gfk1 = GenericForeignKeyField::with_values(Some(1), Some(2));
		let gfk2 = GenericForeignKeyField::with_values(Some(1), Some(2));
		let gfk3 = GenericForeignKeyField::with_values(Some(1), Some(3));

		assert_eq!(gfk1, gfk2);
		assert_ne!(gfk1, gfk3);
	}
}

#[cfg(all(test, feature = "database"))]
mod database_tests {
	use super::*;
	use crate::contenttypes::persistence::{ContentTypePersistence, ContentTypePersistenceBackend};
	use constraints::GenericForeignKeyConstraints;
	use std::sync::{Arc, Once};

	static INIT_DRIVERS: Once = Once::new();

	fn init_drivers() {
		INIT_DRIVERS.call_once(|| {
			sqlx::any::install_default_drivers();
		});
	}

	async fn create_test_persistence() -> ContentTypePersistence {
		init_drivers();

		// Use in-memory SQLite with shared cache mode and single connection
		let db_url = "sqlite::memory:?mode=rwc&cache=shared";

		// Create persistence with minimal connection pool for tests
		use sqlx::pool::PoolOptions;
		let pool = PoolOptions::new()
			.min_connections(1)
			.max_connections(1)
			.connect(db_url)
			.await
			.expect("Failed to connect to test database");

		let persistence = ContentTypePersistence::from_pool(Arc::new(pool), db_url);

		persistence
			.create_table()
			.await
			.expect("Failed to create table");
		persistence
	}

	#[tokio::test]
	async fn test_validate_content_type() {
		let persistence = create_test_persistence().await;

		// Create a content type in database
		let ct = persistence
			.save(&ContentType::new("blog", "Post"))
			.await
			.expect("Failed to save");
		let ct_id = ct.id.unwrap();

		// Create GFK pointing to it
		let gfk = GenericForeignKeyField::with_values(Some(ct_id), Some(42));

		// Should validate successfully
		let valid = gfk
			.validate_content_type(&persistence)
			.await
			.expect("Failed to validate");
		assert!(valid);

		// Create GFK with non-existent content type
		let invalid_gfk = GenericForeignKeyField::with_values(Some(9999), Some(42));
		let valid = invalid_gfk
			.validate_content_type(&persistence)
			.await
			.expect("Failed to validate");
		assert!(!valid);
	}

	#[tokio::test]
	async fn test_get_validated_content_type() {
		let persistence = create_test_persistence().await;

		// Create a content type in database
		let ct = persistence
			.save(&ContentType::new("auth", "User"))
			.await
			.expect("Failed to save");
		let ct_id = ct.id.unwrap();

		// Create GFK pointing to it
		let gfk = GenericForeignKeyField::with_values(Some(ct_id), Some(123));

		// Should retrieve the content type
		let validated_ct = gfk
			.get_validated_content_type(&persistence)
			.await
			.expect("Failed to get validated content type");

		let validated_ct = validated_ct.unwrap();
		assert_eq!(validated_ct.app_label, "auth");
		assert_eq!(validated_ct.model, "User");
	}

	#[tokio::test]
	async fn test_validate_unset_gfk() {
		let persistence = create_test_persistence().await;

		let gfk = GenericForeignKeyField::new();

		// Unset GFK should not validate
		let valid = gfk
			.validate_content_type(&persistence)
			.await
			.expect("Failed to validate");
		assert!(!valid);

		// Should return None
		let ct = gfk
			.get_validated_content_type(&persistence)
			.await
			.expect("Failed to get");
		assert!(ct.is_none());
	}
}