pub struct TestDb { /* private fields */ }Expand description
A database for one test; see the module docs.
Implementations§
Source§impl TestDb
impl TestDb
Sourcepub fn sqlite() -> TestDbBuilder
Available on crate feature model-sqlite and (crate features model-mysql or model-postgres or model-sqlite) only.
pub fn sqlite() -> TestDbBuilder
model-sqlite and (crate features model-mysql or model-postgres or model-sqlite) only.A fresh SQLite in-memory database.
Sourcepub fn postgres(url: &str) -> TestDbBuilder
Available on crate feature model-postgres and (crate features model-mysql or model-postgres or model-sqlite) only.
pub fn postgres(url: &str) -> TestDbBuilder
model-postgres and (crate features model-mysql or model-postgres or model-sqlite) only.The PostgreSQL database at url (postgres://user:password@host/db,
sqlx’s URL syntax), each test inside a transaction that’s rolled back.
Sourcepub fn mysql(url: &str) -> TestDbBuilder
Available on crate feature model-mysql and (crate features model-mysql or model-postgres or model-sqlite) only.
pub fn mysql(url: &str) -> TestDbBuilder
model-mysql and (crate features model-mysql or model-postgres or model-sqlite) only.The MySQL database at url (mysql://user:password@host/db), each
test inside a transaction that’s rolled back.
Sourcepub fn postgres_from_env() -> Option<TestDbBuilder>
Available on crate feature model-postgres and (crate features model-mysql or model-postgres or model-sqlite) only.
pub fn postgres_from_env() -> Option<TestDbBuilder>
model-postgres and (crate features model-mysql or model-postgres or model-sqlite) only.TestDb::postgres with the URL in RWS_TEST_POSTGRES_URL; None
(after printing that the test is skipped) when it isn’t set, so a
test can return early on a machine without the server:
use rust_web_server::test_client::TestDb;
let Some(db) = TestDb::postgres_from_env() else { return Ok(()) };
let db = db.migrations("migrations").await?;Sourcepub fn mysql_from_env() -> Option<TestDbBuilder>
Available on crate feature model-mysql and (crate features model-mysql or model-postgres or model-sqlite) only.
pub fn mysql_from_env() -> Option<TestDbBuilder>
model-mysql and (crate features model-mysql or model-postgres or model-sqlite) only.TestDb::mysql with the URL in RWS_TEST_MYSQL_URL; None (after
printing that the test is skipped) when it isn’t set.
Sourcepub fn pool(&self) -> DbPool
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub fn pool(&self) -> DbPool
model-mysql or model-postgres or model-sqlite only.A clone of the pool (cheap: same connections, same database), for the app’s state.
Sourcepub fn backend(&self) -> Backend
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub fn backend(&self) -> Backend
model-mysql or model-postgres or model-sqlite only.Which backend this database is.
Sourcepub fn is_transactional(&self) -> bool
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub fn is_transactional(&self) -> bool
model-mysql or model-postgres or model-sqlite only.true for PostgreSQL / MySQL, whose writes are rolled back; false
for SQLite, whose database is dropped instead.
Methods from Deref<Target = DbPool>§
Sourcepub fn backend(&self) -> Backend
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub fn backend(&self) -> Backend
model-mysql or model-postgres or model-sqlite only.Which backend this pool talks to.
Sourcepub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn execute(&self, sql: &str, params: &[Value]) -> Result<u64, DbError>
model-mysql or model-postgres or model-sqlite only.Execute a SQL statement (INSERT / UPDATE / DELETE / DDL).
Returns the number of rows affected.
Sourcepub async fn query_rows(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<ModelRow>, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn query_rows( &self, sql: &str, params: &[Value], ) -> Result<Vec<ModelRow>, DbError>
model-mysql or model-postgres or model-sqlite only.Execute a SQL query and return untyped rows.
Sourcepub async fn query<T>(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<T>, DbError>where
T: Model,
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn query<T>(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<T>, DbError>where
T: Model,
model-mysql or model-postgres or model-sqlite only.Execute a SQL query and deserialise results into T: Model.
Sourcepub async fn query_raw(
&self,
sql: &str,
params: &[Value],
) -> Result<Vec<ModelRow>, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn query_raw( &self, sql: &str, params: &[Value], ) -> Result<Vec<ModelRow>, DbError>
model-mysql or model-postgres or model-sqlite only.Execute a SQL query returning untyped rows (alias for query_rows).
Sourcepub async fn begin(&self) -> Result<DbTransaction, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn begin(&self) -> Result<DbTransaction, DbError>
model-mysql or model-postgres or model-sqlite only.Begin a transaction. Call DbTransaction::commit to commit or let it
drop to automatically roll back.
Sourcepub async fn transaction<F, T, Fut>(&self, f: F) -> Result<T, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn transaction<F, T, Fut>(&self, f: F) -> Result<T, DbError>
model-mysql or model-postgres or model-sqlite only.Run a closure in a transaction.
The closure owns the transaction: call tx.commit() before returning
Ok. A transaction that isn’t committed — the closure returns Err,
or Ok without committing — is dropped, which rolls it back.
use rust_web_server::model::{DbPool, Value};
let pool = DbPool::memory().await?;
let result = pool.transaction(|mut tx| async move {
tx.execute("INSERT INTO t (v) VALUES (?)", &[Value::Text("hi".into())]).await?;
tx.commit().await?;
Ok(42i32)
}).await?;
assert_eq!(42, result);Sourcepub async fn migrate(&self, dir: &str) -> Result<(), DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn migrate(&self, dir: &str) -> Result<(), DbError>
model-mysql or model-postgres or model-sqlite only.Run pending migrations from SQL files in dir.
Files are applied in lexicographic order. Already-applied versions are
tracked in a _schema_migrations table (created if absent). Idempotent.
Sourcepub async fn migration_status(
&self,
dir: &str,
) -> Result<Vec<MigrationStatus>, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn migration_status( &self, dir: &str, ) -> Result<Vec<MigrationStatus>, DbError>
model-mysql or model-postgres or model-sqlite only.Return the status (applied / pending) for each SQL file in dir.
Sourcepub async fn rollback_last(&self, dir: &str) -> Result<Option<String>, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn rollback_last(&self, dir: &str) -> Result<Option<String>, DbError>
model-mysql or model-postgres or model-sqlite only.Roll back the most recently applied migration in dir.
Requires a companion <version-stem>.down.sql file next to the up
migration it undoes (e.g. 0001_x.sql → 0001_x.down.sql); returns
Err if that file is missing. Returns Ok(None) if nothing is
currently applied.
Sourcepub async fn rollback(
&self,
dir: &str,
n: usize,
) -> Result<Vec<String>, DbError>
Available on crate features model-mysql or model-postgres or model-sqlite only.
pub async fn rollback( &self, dir: &str, n: usize, ) -> Result<Vec<String>, DbError>
model-mysql or model-postgres or model-sqlite only.Roll back the last n applied migrations, most recently applied
first. Returns the versions actually rolled back — fewer than n if
fewer than n migrations were applied. Stops at the first missing
down file or SQL error; migrations already rolled back in earlier
iterations stay rolled back.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for TestDb
impl !UnwindSafe for TestDb
impl Freeze for TestDb
impl Send for TestDb
impl Sync for TestDb
impl Unpin for TestDb
impl UnsafeUnpin for TestDb
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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> ⓘ
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
map-response-body only.Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
trace only.Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
trace only.