Skip to main content

TestDatabase

Struct TestDatabase 

Source
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

Source

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.

Source

pub fn into_database(self) -> Database

Consume the fixture and return its database handle.

Methods from Deref<Target = Database>§

Source

pub fn backend(&self) -> Backend

Return the configured database backend.

Source

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.

Source

pub fn toasty(&self) -> &Db

Borrow the native Toasty database handle.

Source

pub fn toasty_mut(&mut self) -> &mut Db

Mutably borrow the native Toasty database handle.

Methods from Deref<Target = Db>§

Source

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.

Source

pub async fn push_schema(&self) -> Result<(), Error>

Creates tables and indices defined in the schema on the database.

Source

pub async fn reset_db(&self) -> Result<(), Error>

Drops the entire database and recreates an empty one without applying migrations.

Source

pub fn driver(&self) -> &(dyn Driver + 'static)

Returns a reference to the underlying database driver.

Source

pub fn schema(&self) -> &Arc<Schema>

Returns the compiled schema used by this database handle.

Source

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).

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for TestDatabase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for TestDatabase

Source§

type Target = Database

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for TestDatabase

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more