pub struct TestTransaction<C> { /* private fields */ }Expand description
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
ⓘ
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 backImplementations§
Source§impl<C> TestTransaction<C>
impl<C> TestTransaction<C>
Sourcepub fn commit_on_drop(self) -> Self
pub fn commit_on_drop(self) -> Self
Configure the transaction to commit on drop instead of rollback.
Use this with caution - it defeats the purpose of test isolation.
Sourcepub fn connection(&self) -> &C
pub fn connection(&self) -> &C
Get a reference to the underlying connection.
Sourcepub fn connection_mut(&mut self) -> &mut C
pub fn connection_mut(&mut self) -> &mut C
Get a mutable reference to the underlying connection.
Sourcepub fn into_inner(self) -> C
pub fn into_inner(self) -> C
Consume the transaction and return the underlying connection.
Note: This prevents automatic rollback/commit on drop.
Sourcepub fn mark_completed(&mut self)
pub fn mark_completed(&mut self)
Mark the transaction as completed (no rollback on drop).
Trait Implementations§
Source§impl<C: Debug> Debug for TestTransaction<C>
impl<C: Debug> Debug for TestTransaction<C>
Auto Trait Implementations§
impl<C> Freeze for TestTransaction<C>where
C: Freeze,
impl<C> RefUnwindSafe for TestTransaction<C>where
C: RefUnwindSafe,
impl<C> Send for TestTransaction<C>where
C: Send,
impl<C> Sync for TestTransaction<C>where
C: Sync,
impl<C> Unpin for TestTransaction<C>where
C: Unpin,
impl<C> UnsafeUnpin for TestTransaction<C>where
C: UnsafeUnpin,
impl<C> UnwindSafe for TestTransaction<C>where
C: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
impl<E> ServerFnErrorAssertions<E> for Ewhere
E: Debug,
Source§fn should_contain_message(&self, expected: &str)where
E: Display,
fn should_contain_message(&self, expected: &str)where
E: Display,
Assert that the error message contains the specified text.
Source§fn should_have_message(&self, expected: &str)where
E: Display,
fn should_have_message(&self, expected: &str)where
E: Display,
Assert that the error message matches exactly.