reinhardt-testkit 0.1.1

Core testing infrastructure for Reinhardt framework (no functional crate dependencies)
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
//! Transaction management utilities for server function testing.
//!
//! This module provides utilities for managing database transactions in tests,
//! including automatic rollback to ensure test isolation.
//!
//! # Example
//!
//! ```rust,ignore
//! use reinhardt_testkit::server_fn::transaction::TestTransaction;
//!
//! #[rstest]
//! #[tokio::test]
//! async fn test_with_rollback(postgres_suite: SuiteGuard<PostgresSuiteResource>) {
//!     let tx = TestTransaction::begin(postgres_suite.pool()).await.unwrap();
//!
//!     // Perform database operations using tx.connection()
//!     // All changes will be rolled back when tx is dropped
//! }
//! ```

#![cfg(native)]

use std::ops::Deref;

use async_trait::async_trait;

/// A test transaction that automatically rolls back on drop.
///
/// This provides a way to run database tests in isolation without
/// affecting other tests or leaving test data in the database.
///
/// # Behavior
///
/// When `TestTransaction` is dropped, it will automatically rollback
/// all changes made during the test. This ensures that:
/// - Tests don't affect each other
/// - No test data is left in the database
/// - Tests can be run in parallel without conflicts
///
/// # Example
///
/// ```rust,ignore
/// let tx = TestTransaction::begin(&pool).await?;
///
/// // All operations here happen within the transaction
/// sqlx::query("INSERT INTO users (name) VALUES ('test')")
///     .execute(tx.connection())
///     .await?;
///
/// // When tx goes out of scope, all changes are rolled back
/// ```
#[derive(Debug)]
pub struct TestTransaction<C> {
	/// The underlying connection or transaction handle.
	connection: Option<C>,
	/// Whether to commit instead of rollback.
	commit_on_drop: bool,
	/// Whether the transaction has been explicitly completed.
	completed: bool,
}

impl<C> TestTransaction<C> {
	/// Create a new test transaction wrapper.
	pub fn new(connection: C) -> Self {
		Self {
			connection: Some(connection),
			commit_on_drop: false,
			completed: false,
		}
	}

	/// Configure the transaction to commit on drop instead of rollback.
	///
	/// Use this with caution - it defeats the purpose of test isolation.
	pub fn commit_on_drop(mut self) -> Self {
		self.commit_on_drop = true;
		self
	}

	/// Get a reference to the underlying connection.
	pub fn connection(&self) -> &C {
		self.connection
			.as_ref()
			.expect("connection already consumed")
	}

	/// Get a mutable reference to the underlying connection.
	pub fn connection_mut(&mut self) -> &mut C {
		self.connection
			.as_mut()
			.expect("connection already consumed")
	}

	/// Consume the transaction and return the underlying connection.
	///
	/// Note: This prevents automatic rollback/commit on drop.
	pub fn into_inner(mut self) -> C {
		self.completed = true;
		let connection = self.connection.take().expect("connection already consumed");
		std::mem::forget(self);
		connection
	}

	/// Mark the transaction as completed (no rollback on drop).
	pub fn mark_completed(&mut self) {
		self.completed = true;
	}
}

impl<C> Deref for TestTransaction<C> {
	type Target = C;

	fn deref(&self) -> &Self::Target {
		self.connection
			.as_ref()
			.expect("connection already consumed")
	}
}

/// Trait for types that can be used as test transaction connections.
#[async_trait]
pub trait TestConnectionExt: Sized {
	/// The error type.
	type Error;

	/// Begin a new transaction that will rollback on drop.
	async fn begin_test_transaction(self) -> Result<TestTransaction<Self>, Self::Error>;

	/// Commit the transaction explicitly.
	async fn commit_transaction(self) -> Result<(), Self::Error>;

	/// Rollback the transaction explicitly.
	async fn rollback_transaction(self) -> Result<(), Self::Error>;
}

/// Wrapper for managing savepoints within a test.
///
/// Savepoints allow you to create checkpoints within a transaction
/// and rollback to them without rolling back the entire transaction.
#[derive(Debug)]
pub struct TestSavepoint {
	/// The savepoint name.
	pub name: String,
	/// Whether the savepoint has been released.
	released: bool,
}

impl TestSavepoint {
	/// Create a new savepoint with the given name.
	pub fn new(name: impl Into<String>) -> Self {
		Self {
			name: name.into(),
			released: false,
		}
	}

	/// Generate a unique savepoint name.
	pub fn generate() -> Self {
		Self::new(format!("sp_{}", uuid::Uuid::now_v7().simple()))
	}

	/// Mark the savepoint as released.
	pub fn mark_released(&mut self) {
		self.released = true;
	}

	/// Check if the savepoint has been released.
	pub fn is_released(&self) -> bool {
		self.released
	}
}

// Fixes #872: Migrate SQL utility functions to use reinhardt-query instead of
// raw string interpolation to prevent SQL injection.
/// Test database utilities for common operations.
pub mod utils {
	use reinhardt_query::{
		Alias, ColumnRef, Expr, Iden, PostgresQueryBuilder, Query, QueryStatementBuilder,
	};

	/// Quote an identifier for PostgreSQL using `reinhardt_query::Iden`.
	fn quote_ident(name: &str) -> String {
		let alias = Alias::new(name);
		let mut buf = String::new();
		alias.quoted('"', &mut buf);
		buf
	}

	/// Truncate all tables in the given list.
	///
	/// This is useful for cleaning up between tests when not using
	/// transaction rollback.
	///
	/// Note: reinhardt-query does not natively support TRUNCATE, so this uses
	/// properly quoted identifiers via `Alias`.
	pub fn truncate_tables_sql(tables: &[&str]) -> String {
		if tables.is_empty() {
			return String::new();
		}

		let quoted_tables: Vec<String> = tables
			.iter()
			.map(|t| {
				// Use a SELECT query to get the properly quoted identifier
				let query = Query::select()
					.column(ColumnRef::asterisk())
					.from(Alias::new(*t))
					.to_string(PostgresQueryBuilder);
				// Extract quoted table name from "SELECT * FROM <table>"
				query
					.strip_prefix("SELECT * FROM ")
					.unwrap_or(t)
					.to_string()
			})
			.collect();

		format!(
			"TRUNCATE TABLE {} RESTART IDENTITY CASCADE",
			quoted_tables.join(", ")
		)
	}

	/// Generate a DELETE statement for cleaning up a table.
	pub fn delete_from_sql(table: &str, where_clause: Option<&str>) -> String {
		let mut query = Query::delete();
		query.from_table(Alias::new(table));

		if let Some(clause) = where_clause {
			query.and_where(Expr::cust(clause.to_string()));
		}

		query.to_string(PostgresQueryBuilder)
	}

	/// Generate an INSERT statement for test data.
	///
	/// Values are inserted as raw SQL expressions (e.g., `'Alice'`, `NOW()`),
	/// so they are NOT parameterised. Table and column names are properly quoted
	/// via `reinhardt_query::Iden`.
	pub fn insert_test_data_sql(table: &str, columns: &[&str], values: &[&str]) -> String {
		let quoted_table = quote_ident(table);
		let quoted_cols: Vec<String> = columns.iter().map(|c| quote_ident(c)).collect();
		format!(
			"INSERT INTO {} ({}) VALUES ({})",
			quoted_table,
			quoted_cols.join(", "),
			values.join(", ")
		)
	}
}

/// Configuration for test database behavior.
#[derive(Debug, Clone)]
pub struct TestDatabaseConfig {
	/// Tables to truncate before each test (if not using transactions).
	pub truncate_tables: Vec<String>,
	/// Whether to use transactions for test isolation.
	pub use_transactions: bool,
	/// Maximum number of connections for the test pool.
	pub max_connections: u32,
	/// Connection timeout in seconds.
	pub connection_timeout_secs: u64,
}

impl Default for TestDatabaseConfig {
	fn default() -> Self {
		Self {
			truncate_tables: Vec::new(),
			use_transactions: true,
			max_connections: 5,
			connection_timeout_secs: 30,
		}
	}
}

impl TestDatabaseConfig {
	/// Create a new configuration.
	pub fn new() -> Self {
		Self::default()
	}

	/// Add a table to truncate.
	pub fn truncate(mut self, table: impl Into<String>) -> Self {
		self.truncate_tables.push(table.into());
		self
	}

	/// Disable transaction-based isolation.
	pub fn without_transactions(mut self) -> Self {
		self.use_transactions = false;
		self
	}

	/// Set the maximum number of connections.
	pub fn max_connections(mut self, count: u32) -> Self {
		self.max_connections = count;
		self
	}

	/// Set the connection timeout.
	pub fn connection_timeout(mut self, secs: u64) -> Self {
		self.connection_timeout_secs = secs;
		self
	}
}

/// Helper for seeding test data.
///
/// This provides a fluent interface for inserting test data
/// that will be available during the test.
#[derive(Debug, Default)]
pub struct TestDataSeeder {
	/// SQL statements to execute for seeding.
	statements: Vec<String>,
}

impl TestDataSeeder {
	/// Create a new seeder.
	pub fn new() -> Self {
		Self::default()
	}

	/// Add a raw SQL statement.
	pub fn sql(mut self, statement: impl Into<String>) -> Self {
		self.statements.push(statement.into());
		self
	}

	/// Add an INSERT statement.
	pub fn insert(self, table: &str, columns: &[&str], values: &[&str]) -> Self {
		self.sql(utils::insert_test_data_sql(table, columns, values))
	}

	/// Get all statements to execute.
	pub fn statements(&self) -> &[String] {
		&self.statements
	}

	/// Build the combined SQL for all seed operations.
	pub fn build(&self) -> String {
		self.statements.join(";\n")
	}
}

/// Guard that ensures cleanup runs at the end of a test.
///
/// This is useful for tests that need to clean up resources
/// even if the test panics.
pub struct CleanupGuard<F: FnOnce()> {
	cleanup: Option<F>,
}

impl<F: FnOnce()> CleanupGuard<F> {
	/// Create a new cleanup guard.
	pub fn new(cleanup: F) -> Self {
		Self {
			cleanup: Some(cleanup),
		}
	}

	/// Disarm the guard (don't run cleanup on drop).
	pub fn disarm(&mut self) {
		self.cleanup = None;
	}
}

impl<F: FnOnce()> Drop for CleanupGuard<F> {
	fn drop(&mut self) {
		if let Some(cleanup) = self.cleanup.take() {
			cleanup();
		}
	}
}

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

	#[test]
	fn test_truncate_tables_sql() {
		let sql = utils::truncate_tables_sql(&["users", "posts"]);
		assert!(sql.contains("TRUNCATE TABLE"));
		assert!(sql.contains("\"users\""));
		assert!(sql.contains("\"posts\""));
		assert!(sql.contains("CASCADE"));
	}

	#[test]
	fn test_truncate_tables_sql_empty() {
		let sql = utils::truncate_tables_sql(&[]);
		assert!(sql.is_empty());
	}

	#[test]
	fn test_delete_from_sql() {
		let sql = utils::delete_from_sql("users", None);
		assert_eq!(sql, "DELETE FROM \"users\"");

		let sql_with_where = utils::delete_from_sql("users", Some("id = 1"));
		assert_eq!(sql_with_where, "DELETE FROM \"users\" WHERE id = 1");
	}

	#[test]
	fn test_insert_test_data_sql() {
		let sql = utils::insert_test_data_sql(
			"users",
			&["name", "email"],
			&["'Alice'", "'alice@example.com'"],
		);
		assert!(sql.contains("INSERT INTO \"users\""));
		assert!(sql.contains("\"name\""));
		assert!(sql.contains("\"email\""));
		assert!(sql.contains("'Alice'"));
	}

	#[test]
	fn test_database_config() {
		let config = TestDatabaseConfig::new()
			.truncate("users")
			.truncate("posts")
			.max_connections(10)
			.connection_timeout(60);

		assert_eq!(config.truncate_tables.len(), 2);
		assert_eq!(config.max_connections, 10);
		assert_eq!(config.connection_timeout_secs, 60);
	}

	#[test]
	fn test_data_seeder() {
		let seeder = TestDataSeeder::new()
			.insert("users", &["name"], &["'Alice'"])
			.insert("posts", &["title", "user_id"], &["'Hello'", "1"]);

		assert_eq!(seeder.statements().len(), 2);
	}

	#[test]
	fn test_savepoint() {
		let sp = TestSavepoint::generate();
		assert!(sp.name.starts_with("sp_"));
		assert!(!sp.is_released());

		let mut sp2 = TestSavepoint::new("my_savepoint");
		sp2.mark_released();
		assert!(sp2.is_released());
	}

	#[test]
	fn test_cleanup_guard() {
		use std::cell::RefCell;
		use std::rc::Rc;

		let cleaned = Rc::new(RefCell::new(false));
		let cleaned_clone = cleaned.clone();

		{
			let _guard = CleanupGuard::new(move || {
				*cleaned_clone.borrow_mut() = true;
			});
		}

		assert!(*cleaned.borrow());
	}

	#[test]
	fn test_cleanup_guard_disarm() {
		use std::cell::RefCell;
		use std::rc::Rc;

		let cleaned = Rc::new(RefCell::new(false));
		let cleaned_clone = cleaned.clone();

		{
			let mut guard = CleanupGuard::new(move || {
				*cleaned_clone.borrow_mut() = true;
			});
			guard.disarm();
		}

		assert!(!*cleaned.borrow());
	}
}