pub struct TestDatabaseInstance<B>{
pub backend: B,
pub pool: B::Pool,
pub db_name: DatabaseName,
pub connection_pool: Option<Arc<Mutex<Vec<B::Connection>>>>,
}
Expand description
A test database that handles setup, connections, and cleanup TODO: Create a TestManager that can handle connection pooling and cleanup
Fields§
§backend: B
The database backend
pool: B::Pool
The connection pool
db_name: DatabaseName
The database name
connection_pool: Option<Arc<Mutex<Vec<B::Connection>>>>
The connection pool
Implementations§
Source§impl<B> TestDatabaseInstance<B>
impl<B> TestDatabaseInstance<B>
Sourcepub async fn new(backend: B, config: DatabaseConfig) -> Result<Self, B::Error>
pub async fn new(backend: B, config: DatabaseConfig) -> Result<Self, B::Error>
Create a new test database with the given backend
Sourcepub async fn new_with_name(
backend: B,
config: DatabaseConfig,
db_name: DatabaseName,
) -> Result<Self, B::Error>
pub async fn new_with_name( backend: B, config: DatabaseConfig, db_name: DatabaseName, ) -> Result<Self, B::Error>
Create a new test database with the given backend and specific name
Sourcepub fn name(&self) -> &DatabaseName
pub fn name(&self) -> &DatabaseName
Returns a reference to the database name
Sourcepub async fn connect(&self) -> Result<B::Connection, B::Error>
pub async fn connect(&self) -> Result<B::Connection, B::Error>
Create a single connection to the database without using the pool This is useful for cases where a single connection is needed for a specific operation
Sourcepub async fn with_connection<F, R, E>(
&self,
operation: F,
) -> Result<R, B::Error>
pub async fn with_connection<F, R, E>( &self, operation: F, ) -> Result<R, B::Error>
Execute a function with a one-off connection and automatically close it after use This is the most efficient way to perform a one-off database operation
Sourcepub async fn acquire_connection(
&self,
) -> Result<<B::Pool as DatabasePool>::Connection, B::Error>
pub async fn acquire_connection( &self, ) -> Result<<B::Pool as DatabasePool>::Connection, B::Error>
Get a connection from the pool or acquire a new one
Sourcepub async fn release_connection(
&self,
conn: <B::Pool as DatabasePool>::Connection,
) -> Result<(), B::Error>
pub async fn release_connection( &self, conn: <B::Pool as DatabasePool>::Connection, ) -> Result<(), B::Error>
Release a connection back to the pool
Sourcepub async fn setup<F, Fut>(&self, setup_fn: F) -> Result<(), B::Error>where
F: FnOnce(&mut <B::Pool as DatabasePool>::Connection) -> Fut + Send,
Fut: Future<Output = Result<(), B::Error>> + Send,
pub async fn setup<F, Fut>(&self, setup_fn: F) -> Result<(), B::Error>where
F: FnOnce(&mut <B::Pool as DatabasePool>::Connection) -> Fut + Send,
Fut: Future<Output = Result<(), B::Error>> + Send,
Setup the database with a function The connection handling approach needs to match the expected B::Connection type
Trait Implementations§
Source§impl<B> Clone for TestDatabaseInstance<B>where
B: DatabaseBackend + 'static + Clone + Debug + Send + Sync + Clone,
B::Pool: Clone,
B::Connection: Clone,
impl<B> Clone for TestDatabaseInstance<B>where
B: DatabaseBackend + 'static + Clone + Debug + Send + Sync + Clone,
B::Pool: Clone,
B::Connection: Clone,
Source§fn clone(&self) -> TestDatabaseInstance<B>
fn clone(&self) -> TestDatabaseInstance<B>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more