pub struct TestDatabase { /* private fields */ }Expand description
Test database wrapper that provides isolated database environments
Each TestDatabase creates a fresh in-memory SQLite database with
migrations applied. The database is automatically registered in the
test container, so any code using DB::connection() or #[inject] db: Database
will receive this test database.
When the TestDatabase is dropped, the test container is cleared,
ensuring complete isolation between tests.
§Example
ⓘ
use ferro_rs::testing::TestDatabase;
use ferro_rs::migrations::Migrator;
#[tokio::test]
async fn test_user_creation() {
let db = TestDatabase::fresh::<Migrator>().await.unwrap();
// Actions using DB::connection() automatically get this test database
let action = CreateUserAction::new();
let user = action.execute("test@example.com").await.unwrap();
// Query directly using db.conn()
let found = users::Entity::find_by_id(user.id)
.one(db.conn())
.await
.unwrap();
assert!(found.is_some());
}Implementations§
Source§impl TestDatabase
impl TestDatabase
Sourcepub async fn fresh<M: MigratorTrait>() -> Result<Self, FrameworkError>
pub async fn fresh<M: MigratorTrait>() -> Result<Self, FrameworkError>
Create a fresh test database with migrations applied
This creates an in-memory SQLite database, runs all migrations, and registers the connection in the test container.
§Type Parameters
M- The migrator type implementingMigratorTrait. Typically this iscrate::migrations::Migratorfrom your application.
§Errors
Returns an error if:
- Database connection fails
- Migration execution fails
§Example
ⓘ
use ferro_rs::testing::TestDatabase;
use ferro_rs::migrations::Migrator;
#[tokio::test]
async fn test_example() {
let db = TestDatabase::fresh::<Migrator>().await.unwrap();
// ...
}Sourcepub fn conn(&self) -> &DatabaseConnection
pub fn conn(&self) -> &DatabaseConnection
Sourcepub fn db(&self) -> &DbConnection
pub fn db(&self) -> &DbConnection
Get the DbConnection wrapper
Use this when you need the full DbConnection type.
Auto Trait Implementations§
impl Freeze for TestDatabase
impl !RefUnwindSafe for TestDatabase
impl Send for TestDatabase
impl Sync for TestDatabase
impl Unpin for TestDatabase
impl UnsafeUnpin for TestDatabase
impl !UnwindSafe for TestDatabase
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 more