pub struct TestDatabase { /* private fields */ }Expand description
Database access for tests.
Test database configuration is intentionally explicit to prevent accidental use of production databases.
§Examples
ⓘ
// Option 1: Embedded Postgres (requires embedded-db feature)
// Run with: cargo test --features embedded-db
let db = TestDatabase::embedded().await?;
// Option 2: Explicit URL
let db = TestDatabase::from_url("postgres://localhost/test_db").await?;
// Option 3: From TEST_DATABASE_URL env var
let db = TestDatabase::from_env().await?;Implementations§
Source§impl TestDatabase
impl TestDatabase
Sourcepub async fn from_url(url: &str) -> Result<TestDatabase, ForgeError>
pub async fn from_url(url: &str) -> Result<TestDatabase, ForgeError>
Connect to database at the given URL.
Use this for explicit database configuration in tests.
Sourcepub async fn from_env() -> Result<TestDatabase, ForgeError>
pub async fn from_env() -> Result<TestDatabase, ForgeError>
Connect using TEST_DATABASE_URL environment variable.
Note: This reads TEST_DATABASE_URL (not DATABASE_URL) to prevent accidental use of production databases in tests.
Sourcepub async fn execute(&self, sql: &str) -> Result<(), ForgeError>
pub async fn execute(&self, sql: &str) -> Result<(), ForgeError>
Run raw SQL to set up test data or schema.
Sourcepub async fn isolated(
&self,
test_name: &str,
) -> Result<IsolatedTestDb, ForgeError>
pub async fn isolated( &self, test_name: &str, ) -> Result<IsolatedTestDb, ForgeError>
Creates a dedicated database for a single test, providing full isolation.
Each call creates a new database with a unique name. Use this when tests modify data and could interfere with each other.
Auto Trait Implementations§
impl Freeze for TestDatabase
impl !RefUnwindSafe for TestDatabase
impl Send for TestDatabase
impl Sync for TestDatabase
impl Unpin 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