reinhardt-testkit 0.2.0-rc.1

Core testing infrastructure for Reinhardt framework (no functional crate dependencies)
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
//! Base test case with common setup and assertions
//!
//! Similar to DRF's APITestCase

use crate::client::APIClient;
use crate::resource::AsyncTestResource;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::RwLock;

/// Error types that can occur during test teardown
#[derive(Debug, Error)]
pub enum TeardownError {
	/// Failed to rollback one or more active transactions
	#[error("Failed to rollback transactions: {0}")]
	TransactionRollbackFailed(String),

	/// Failed to close database connection
	#[error("Failed to close database connection: {0}")]
	ConnectionCloseFailed(String),

	/// Failed to cleanup client state
	#[error("Failed to cleanup client state: {0}")]
	ClientCleanupFailed(String),
}

/// Handle for tracking active test transactions
///
/// This struct tracks transaction state for monitoring and cleanup purposes.
/// Actual transaction management is handled by sqlx's Transaction type,
/// which automatically rolls back uncommitted transactions when dropped.
#[cfg(feature = "testcontainers")]
#[derive(Debug, Clone)]
pub struct TransactionHandle {
	/// Unique identifier for the transaction
	id: String,
	/// Whether the transaction has been committed
	committed: bool,
}

#[cfg(feature = "testcontainers")]
impl TransactionHandle {
	/// Create a new transaction handle with a unique ID
	pub fn new() -> Self {
		Self {
			id: uuid::Uuid::now_v7().to_string(),
			committed: false,
		}
	}

	/// Get the transaction ID
	pub fn id(&self) -> &str {
		&self.id
	}

	/// Check if the transaction has been committed
	pub fn is_committed(&self) -> bool {
		self.committed
	}

	/// Mark the transaction as committed
	pub fn mark_committed(&mut self) {
		self.committed = true;
	}
}

#[cfg(feature = "testcontainers")]
impl Default for TransactionHandle {
	fn default() -> Self {
		Self::new()
	}
}

/// Base test case for API testing
///
/// Provides:
/// - Pre-configured APIClient
/// - Automatic setup/teardown via AsyncTestResource
/// - Assertion helpers
/// - Optional TestContainer database integration
///
/// # Example
/// ```rust,no_run
/// # #[tokio::main]
/// # async fn main() {
/// use reinhardt_testkit::testcase::APITestCase;
/// use reinhardt_testkit::resource::AsyncTeardownGuard;
/// use rstest::*;
///
/// #[fixture]
/// async fn api_test() -> AsyncTeardownGuard<APITestCase> {
///     AsyncTeardownGuard::new().await
/// }
///
/// #[rstest]
/// #[tokio::test]
/// async fn test_list_users(#[future] api_test: AsyncTeardownGuard<APITestCase>) {
///     let case = api_test.await;
///     let response = case.client().await.get("/api/users/").await.unwrap();
///     response.assert_ok();
/// }
/// # }
/// ```
pub struct APITestCase {
	client: Arc<RwLock<APIClient>>,
	#[cfg(feature = "testcontainers")]
	database_url: Arc<RwLock<Option<String>>>,
	#[cfg(feature = "testcontainers")]
	db_connection: Arc<RwLock<Option<sqlx::AnyPool>>>,
	#[cfg(feature = "testcontainers")]
	active_transactions: Arc<RwLock<Vec<TransactionHandle>>>,
}

impl APITestCase {
	/// Get the database connection URL (if configured)
	#[cfg(feature = "testcontainers")]
	pub async fn database_url(&self) -> Option<String> {
		self.database_url.read().await.clone()
	}

	/// Get the test client
	pub async fn client(&self) -> tokio::sync::RwLockReadGuard<'_, APIClient> {
		self.client.read().await
	}

	/// Get mutable access to the test client
	pub async fn client_mut(&self) -> tokio::sync::RwLockWriteGuard<'_, APIClient> {
		self.client.write().await
	}

	/// Set the database URL (useful for TestContainers integration)
	#[cfg(feature = "testcontainers")]
	pub async fn set_database_url(&self, url: String) {
		let mut db_url = self.database_url.write().await;
		*db_url = Some(url);
	}

	/// Set the database connection pool
	///
	/// This method allows setting a pre-configured database connection pool
	/// for use in tests. The pool will be properly closed during teardown.
	///
	/// # Example
	/// ```rust,ignore
	/// use sqlx::AnyPool;
	///
	/// let pool = AnyPool::connect("postgres://localhost/test").await?;
	/// test_case.set_database_connection(pool).await;
	/// ```
	#[cfg(feature = "testcontainers")]
	pub async fn set_database_connection(&self, pool: sqlx::AnyPool) {
		let mut conn = self.db_connection.write().await;
		*conn = Some(pool);
	}

	/// Get the database connection pool (if configured)
	#[cfg(feature = "testcontainers")]
	pub async fn db_connection(&self) -> Option<sqlx::AnyPool> {
		self.db_connection.read().await.clone()
	}

	/// Begin a new tracked transaction
	///
	/// This method registers a new transaction handle for tracking purposes.
	/// The actual sqlx::Transaction should be obtained from the pool directly.
	/// The handle is used to track whether transactions are properly committed
	/// or rolled back during teardown.
	///
	/// # Returns
	/// A TransactionHandle that can be used to track the transaction state.
	///
	/// # Example
	/// ```rust,ignore
	/// let handle = test_case.begin_transaction().await;
	/// // ... perform database operations with sqlx::Transaction ...
	/// handle.mark_committed(); // Mark as committed if successful
	/// ```
	#[cfg(feature = "testcontainers")]
	pub async fn begin_transaction(&self) -> TransactionHandle {
		let handle = TransactionHandle::new();
		let mut transactions = self.active_transactions.write().await;
		transactions.push(handle.clone());
		handle
	}

	/// Mark a transaction as committed by its ID
	///
	/// This removes the transaction from the active list, indicating
	/// it was successfully committed and doesn't need rollback.
	#[cfg(feature = "testcontainers")]
	pub async fn commit_transaction(&self, transaction_id: &str) {
		let mut transactions = self.active_transactions.write().await;
		if let Some(pos) = transactions.iter().position(|t| t.id() == transaction_id) {
			let mut handle = transactions.remove(pos);
			handle.mark_committed();
		}
	}

	/// Get the count of active (uncommitted) transactions
	#[cfg(feature = "testcontainers")]
	pub async fn active_transaction_count(&self) -> usize {
		self.active_transactions.read().await.len()
	}
}

#[async_trait::async_trait]
impl AsyncTestResource for APITestCase {
	async fn setup() -> Self {
		Self {
			client: Arc::new(RwLock::new(APIClient::new())),
			#[cfg(feature = "testcontainers")]
			database_url: Arc::new(RwLock::new(None)),
			#[cfg(feature = "testcontainers")]
			db_connection: Arc::new(RwLock::new(None)),
			#[cfg(feature = "testcontainers")]
			active_transactions: Arc::new(RwLock::new(Vec::new())),
		}
	}

	async fn teardown(self) {
		// Step 1: Clean up HTTP client state
		{
			let client = self.client.write().await;
			client.cleanup().await;
		}

		// Step 2: Handle database cleanup (testcontainers feature only)
		#[cfg(feature = "testcontainers")]
		{
			// Log any uncommitted transactions (they will be rolled back when pool closes)
			let transactions = self.active_transactions.read().await;
			let uncommitted_count = transactions.iter().filter(|t| !t.is_committed()).count();
			if uncommitted_count > 0 {
				// Uncommitted transactions will be automatically rolled back by sqlx
				// when the pool is closed
				tracing::debug!(
					"Rolling back {} uncommitted transaction(s) during teardown",
					uncommitted_count
				);
			}
			drop(transactions);

			// Close the database connection pool
			let mut pool_guard = self.db_connection.write().await;
			if let Some(pool) = pool_guard.take() {
				// Close the pool gracefully - this will rollback any uncommitted transactions
				pool.close().await;
			}
		}

		// Step 3: Drop the client
		drop(self.client);
	}
}

/// Helper macro for defining test cases with automatic setup/teardown
///
/// # Example
/// ```rust,no_run
/// # #[tokio::main]
/// # async fn main() {
/// # use reinhardt_testkit::test_case;
/// test_case! {
///     async fn test_get_users(case: &APITestCase) {
///         let client = case.client().await;
///         let response = client.get("/api/users/").await.unwrap();
///         response.assert_ok();
///     }
/// }
/// # }
/// ```
#[macro_export]
macro_rules! test_case {
	(
        async fn $name:ident($case:ident: &APITestCase) $body:block
    ) => {
		#[rstest::rstest]
		#[tokio::test]
		async fn $name() {
			use $crate::resource::AsyncTeardownGuard;
			use $crate::testcase::APITestCase;

			let guard = AsyncTeardownGuard::<APITestCase>::new().await;
			let $case = &*guard;

			// Run test
			$body

			// guard is dropped here, teardown() is automatically called
		}
	};
}

/// Helper macro for defining authenticated test cases
#[macro_export]
macro_rules! authenticated_test_case {
    (
        async fn $name:ident($case:ident: &APITestCase, $user:ident: serde_json::Value) $body:block
    ) => {
        #[rstest::rstest]
        #[tokio::test]
        async fn $name() {
            use $crate::resource::AsyncTeardownGuard;
            use $crate::testcase::APITestCase;

            let guard = AsyncTeardownGuard::<APITestCase>::new().await;
            let $case = &*guard;

            // Setup authentication
            let $user = serde_json::json!({
                "id": 1,
                "username": "testuser",
            });

            // Run test
            $body

            // guard is dropped here, teardown() is automatically called
        }
    };
}

/// Helper macro for defining test cases with database containers
///
/// Requires `testcontainers` feature to be enabled.
///
/// This macro automatically sets up a PostgreSQL or MySQL container via TestContainers,
/// initializes an `APITestCase` with the database URL, and ensures proper cleanup.
///
/// # Examples
///
/// ## PostgreSQL Example
///
/// ```rust,ignore
/// use reinhardt_testkit::test_case_with_db;
/// use reinhardt_testkit::testcase::APITestCase;
///
/// test_case_with_db! {
///     postgres,
///     async fn test_users_with_postgres(case: &APITestCase) {
///         let db_url = case.database_url().await.unwrap();
///         // Database URL is automatically set
///         assert!(db_url.starts_with("postgres://"));
///
///         // Perform database operations...
///     }
/// }
/// ```
///
/// ## MySQL Example
///
/// ```rust,ignore
/// use reinhardt_testkit::test_case_with_db;
/// use reinhardt_testkit::testcase::APITestCase;
///
/// test_case_with_db! {
///     mysql,
///     async fn test_users_with_mysql(case: &APITestCase) {
///         let db_url = case.database_url().await.unwrap();
///         // Database URL is automatically set
///         assert!(db_url.starts_with("mysql://"));
///
///         // Perform database operations...
///     }
/// }
/// ```
#[cfg(feature = "testcontainers")]
#[macro_export]
macro_rules! test_case_with_db {
    (
        postgres,
        async fn $name:ident($case:ident: &APITestCase) $body:block
    ) => {
        #[rstest::rstest]
        #[tokio::test]
        async fn $name() {
            use $crate::containers::{with_postgres, PostgresContainer};
            use $crate::resource::AsyncTeardownGuard;
            use $crate::testcase::APITestCase;

            with_postgres(|db| async move {
                let guard = AsyncTeardownGuard::<APITestCase>::new().await;
                let $case = &*guard;
                $case.set_database_url(db.connection_url()).await;

                // Run test
                $body

                // guard is dropped here, teardown() is automatically called
                Ok(())
            })
            .await
            .unwrap();
        }
    };
    (
        mysql,
        async fn $name:ident($case:ident: &APITestCase) $body:block
    ) => {
        #[rstest::rstest]
        #[tokio::test]
        async fn $name() {
            use $crate::containers::{with_mysql, MySqlContainer};
            use $crate::resource::AsyncTeardownGuard;
            use $crate::testcase::APITestCase;

            with_mysql(|db| async move {
                let guard = AsyncTeardownGuard::<APITestCase>::new().await;
                let $case = &*guard;
                $case.set_database_url(db.connection_url()).await;

                // Run test
                $body

                // guard is dropped here, teardown() is automatically called
                Ok(())
            })
            .await
            .unwrap();
        }
    };
}

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

	// ========================================================================
	// TeardownError Display tests
	// ========================================================================

	#[rstest]
	fn test_teardown_error_transaction_rollback_display() {
		// Arrange
		let error = TeardownError::TransactionRollbackFailed("tx-123 failed".to_string());

		// Act
		let display = format!("{}", error);

		// Assert
		assert_eq!(display, "Failed to rollback transactions: tx-123 failed");
	}

	#[rstest]
	fn test_teardown_error_connection_close_display() {
		// Arrange
		let error = TeardownError::ConnectionCloseFailed("connection refused".to_string());

		// Act
		let display = format!("{}", error);

		// Assert
		assert_eq!(
			display,
			"Failed to close database connection: connection refused"
		);
	}

	#[rstest]
	fn test_teardown_error_client_cleanup_display() {
		// Arrange
		let error = TeardownError::ClientCleanupFailed("timeout".to_string());

		// Act
		let display = format!("{}", error);

		// Assert
		assert_eq!(display, "Failed to cleanup client state: timeout");
	}

	#[rstest]
	fn test_teardown_error_debug() {
		// Arrange
		let error = TeardownError::TransactionRollbackFailed("debug test".to_string());

		// Act
		let debug = format!("{:?}", error);

		// Assert
		assert!(
			debug.contains("debug test"),
			"Debug output should contain the message, got: {}",
			debug
		);
	}

	// ========================================================================
	// APITestCase tests
	// ========================================================================

	#[rstest]
	#[tokio::test]
	async fn test_api_test_case_setup_creates_client() {
		// Arrange & Act
		let test_case = APITestCase::setup().await;

		// Assert
		let client = test_case.client().await;
		// Verify we can access the client (read guard obtained successfully)
		drop(client);
	}

	#[rstest]
	#[tokio::test]
	async fn test_api_test_case_client_read_access() {
		// Arrange
		let test_case = APITestCase::setup().await;

		// Act
		let client = test_case.client().await;

		// Assert
		// Successfully obtained read guard - the client is accessible
		assert!(
			std::mem::size_of_val(&*client) > 0,
			"Client should have non-zero size"
		);
	}

	#[rstest]
	#[tokio::test]
	async fn test_api_test_case_teardown_completes() {
		// Arrange
		let test_case = APITestCase::setup().await;

		// Act & Assert
		// teardown should complete without panicking
		test_case.teardown().await;
	}

	#[rstest]
	#[tokio::test]
	async fn test_api_test_case_multiple_reads() {
		// Arrange
		let test_case = APITestCase::setup().await;

		// Act
		let client1 = test_case.client().await;
		let client2 = test_case.client().await;

		// Assert
		// Both read guards should be held concurrently without deadlock
		assert!(
			std::mem::size_of_val(&*client1) > 0,
			"First client read should succeed"
		);
		assert!(
			std::mem::size_of_val(&*client2) > 0,
			"Second client read should succeed"
		);
	}

	// ========================================================================
	// TransactionHandle tests (testcontainers feature)
	// ========================================================================

	#[cfg(feature = "testcontainers")]
	mod testcontainers_tests {
		use super::*;
		use rstest::rstest;

		#[rstest]
		fn test_transaction_handle_new() {
			// Arrange & Act
			let handle = TransactionHandle::new();

			// Assert
			assert!(!handle.id().is_empty(), "ID should not be empty");
			assert!(!handle.is_committed(), "New handle should not be committed");
		}

		#[rstest]
		fn test_transaction_handle_mark_committed() {
			// Arrange
			let mut handle = TransactionHandle::new();

			// Act
			handle.mark_committed();

			// Assert
			assert!(handle.is_committed());
		}

		#[rstest]
		fn test_transaction_handle_default() {
			// Arrange & Act
			let handle = TransactionHandle::default();

			// Assert
			assert!(!handle.id().is_empty(), "Default ID should not be empty");
			assert!(
				!handle.is_committed(),
				"Default handle should not be committed"
			);
		}

		#[rstest]
		fn test_transaction_handle_id_is_uuid() {
			// Arrange & Act
			let handle = TransactionHandle::new();

			// Assert
			let id = handle.id();
			// UUID v4 format: 8-4-4-4-12 hex characters
			let parts: Vec<&str> = id.split('-').collect();
			assert_eq!(
				parts.len(),
				5,
				"UUID should have 5 parts separated by hyphens, got: {}",
				id
			);
			assert_eq!(parts[0].len(), 8);
			assert_eq!(parts[1].len(), 4);
			assert_eq!(parts[2].len(), 4);
			assert_eq!(parts[3].len(), 4);
			assert_eq!(parts[4].len(), 12);
		}
	}
}