pub struct TestDatabase { /* private fields */ }Expand description
A fresh SQLite database owned by one test.
Constructing one per test avoids shared files, global cleanup, and test ordering dependencies. Dropping it discards all rows.
Implementations§
Source§impl TestDatabase
impl TestDatabase
Sourcepub async fn new(models: ModelSet) -> Result<Self, DatabaseError>
pub async fn new(models: ModelSet) -> Result<Self, DatabaseError>
Create a fresh in-memory database and initialize the model schema.
§Errors
Returns an error when database or schema initialization fails.
Sourcepub fn into_database(self) -> Database
pub fn into_database(self) -> Database
Consume the fixture and return its database handle.
Methods from Deref<Target = Database>§
Sourcepub async fn initialize_schema(&self) -> Result<(), DatabaseError>
pub async fn initialize_schema(&self) -> Result<(), DatabaseError>
Create all Toasty model tables in a new, empty database.
§Errors
Returns an error when Toasty cannot apply the compiled model schema.
Sourcepub fn toasty_mut(&mut self) -> &mut Db
pub fn toasty_mut(&mut self) -> &mut Db
Mutably borrow the native Toasty database handle.
Methods from Deref<Target = Db>§
Sourcepub async fn connection(&self) -> Result<Connection, Error>
pub async fn connection(&self) -> Result<Connection, Error>
Acquire a dedicated connection from the pool.
The returned Connection implements Executor and pins all
operations to the same physical connection. This is useful when
multiple statements must share connection-level state such as
temporary tables or session variables.
When the Connection is dropped it is returned to the pool for reuse.
Sourcepub async fn push_schema(&self) -> Result<(), Error>
pub async fn push_schema(&self) -> Result<(), Error>
Creates tables and indices defined in the schema on the database.
Sourcepub async fn reset_db(&self) -> Result<(), Error>
pub async fn reset_db(&self) -> Result<(), Error>
Drops the entire database and recreates an empty one without applying migrations.
Sourcepub fn driver(&self) -> &(dyn Driver + 'static)
pub fn driver(&self) -> &(dyn Driver + 'static)
Returns a reference to the underlying database driver.
Sourcepub fn capability(&self) -> &Capability
pub fn capability(&self) -> &Capability
Returns the capability flags reported by the driver.
The query engine uses these to decide which operation types to generate (e.g., SQL vs. key-value).
Sourcepub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
pub async fn transaction(&mut self) -> Result<Transaction<'_>, Error>
Begin a transaction, acquiring a connection from the pool.
Takes &mut self so the Db handle is exclusively borrowed while the
transaction is open. This prevents accidentally running a statement
against the pool — which would execute on a separate connection and
bypass the transaction — when you meant to run it against &mut tx.
If you need a second handle while the transaction is open, clone the
Db before calling this method. Clones share the same pool.
Sourcepub fn transaction_builder(&mut self) -> TransactionBuilder<'_>
pub fn transaction_builder(&mut self) -> TransactionBuilder<'_>
Returns a TransactionBuilder that will acquire a connection from
the pool when begin is called.
Like transaction, this takes &mut self so the
Db handle stays locked for the lifetime of the transaction. Clone
the Db beforehand if you need a separate handle.