Skip to main content

MockConnection

Struct MockConnection 

Source
pub struct MockConnection { /* private fields */ }
Expand description

Mock 数据库连接

用于单元测试,预设 SQL 查询的预期结果,无需真实数据库。

§事务模拟

MockConnection 会跟踪 begin/commit/rollback 调用状态, 可通过 was_committed() / was_rolled_back() 断言事务行为。

Implementations§

Source§

impl MockConnection

Source

pub fn new() -> Self

创建新的空 MockConnection

Source

pub fn with_fallback(self, behavior: FallbackBehavior) -> Self

设置无匹配期望时的行为

Source

pub fn expect_query( &mut self, sql: impl Into<String>, ) -> QueryExpectationBuilder<'_>

预设一个查询期望

返回 QueryExpectationBuilder 用于链式配置结果。

§示例
mock.expect_query("SELECT * FROM users WHERE id = ?")
    .with_rows(vec![vec![("id", Value::from(1i64))]]);
Source

pub fn expect_any(&mut self) -> QueryExpectationBuilder<'_>

预设一个匹配任意 SQL 的 fallback 期望

Source

pub fn expect_execute(&mut self, sql: impl Into<String>, rows_affected: u64)

预设 execute 的影响行数

Source

pub fn in_transaction(&self) -> bool

是否处于事务中

Source

pub fn was_committed(&self) -> bool

是否已提交

Source

pub fn was_rolled_back(&self) -> bool

是否已回滚

Source

pub fn executed_sql(&self) -> &[String]

获取已执行的 SQL 列表(用于断言)

Source

pub fn assert_executed_count(&self, sql: &str, count: usize)

断言某 SQL 被执行了恰好 n 次

Trait Implementations§

Source§

impl Connection for MockConnection

Source§

fn execute<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'a>>

Source§

fn query<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Future<Output = Result<QueryRows, DbError>> + Send + 'a>>

Source§

fn query_with_params<'a>( &'a mut self, sql: &'a str, _params: &'a [Value], ) -> Pin<Box<dyn Future<Output = Result<QueryRows, DbError>> + Send + 'a>>

参数绑定查询(SELECT) Read more
Source§

fn begin_transaction<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>

Source§

fn commit<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>

Source§

fn rollback<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>

Source§

fn is_connected(&self) -> bool

Source§

fn ping<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>>

Source§

fn close<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>

Source§

fn execute_with_params<'a>( &'a mut self, sql: &'a str, params: &'a [Value], ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'a>>

参数绑定执行(INSERT/UPDATE/DELETE) Read more
Source§

fn query_values<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Future<Output = Result<QueryValues, DbError>> + Send + 'a>>

位置式查询(SELECT):返回 (列名, 按列顺序的值矩阵) Read more
Source§

fn query_values_with_params<'a>( &'a mut self, sql: &'a str, params: &'a [Value], ) -> Pin<Box<dyn Future<Output = Result<QueryValues, DbError>> + Send + 'a>>

参数绑定位置式查询(SELECT):叠加 prepared statement + 位置式映射双重优化 Read more
Source§

fn query_stream<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Stream<Item = QueryStreamItem> + Send + 'a>>

流式查询:返回逐行结果流 Read more
Source§

fn query_stream_cursor<'a>( &'a mut self, sql: &'a str, _batch_size: usize, ) -> Pin<Box<dyn Stream<Item = QueryStreamItem> + Send + 'a>>

游标式流式查询(P1-2):按 batch_size 分批拉取,避免大结果集内存峰值。 Read more
Source§

fn execute_batch<'a>( &'a mut self, sqls: &'a [String], ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'a>>

批量执行多条 SQL(按顺序执行,返回累计影响行数) Read more
Source§

fn execute_batch_params<'a>( &'a mut self, sql: &'a str, params_batch: &'a [Vec<Value>], ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'a>>

批量插入(单条 SQL 多次参数绑定执行) Read more
Source§

impl Default for MockConnection

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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