pub struct MockConnection { /* private fields */ }Expand description
Mock 数据库连接
用于单元测试,预设 SQL 查询的预期结果,无需真实数据库。
§事务模拟
MockConnection 会跟踪 begin/commit/rollback 调用状态,
可通过 was_committed() / was_rolled_back() 断言事务行为。
Implementations§
Source§impl MockConnection
impl MockConnection
Sourcepub fn with_fallback(self, behavior: FallbackBehavior) -> Self
pub fn with_fallback(self, behavior: FallbackBehavior) -> Self
设置无匹配期望时的行为
Sourcepub fn expect_query(
&mut self,
sql: impl Into<String>,
) -> QueryExpectationBuilder<'_>
pub fn expect_query( &mut self, sql: impl Into<String>, ) -> QueryExpectationBuilder<'_>
Sourcepub fn expect_any(&mut self) -> QueryExpectationBuilder<'_>
pub fn expect_any(&mut self) -> QueryExpectationBuilder<'_>
预设一个匹配任意 SQL 的 fallback 期望
Sourcepub fn expect_execute(&mut self, sql: impl Into<String>, rows_affected: u64)
pub fn expect_execute(&mut self, sql: impl Into<String>, rows_affected: u64)
预设 execute 的影响行数
Sourcepub fn in_transaction(&self) -> bool
pub fn in_transaction(&self) -> bool
是否处于事务中
Sourcepub fn was_committed(&self) -> bool
pub fn was_committed(&self) -> bool
是否已提交
Sourcepub fn was_rolled_back(&self) -> bool
pub fn was_rolled_back(&self) -> bool
是否已回滚
Sourcepub fn executed_sql(&self) -> &[String]
pub fn executed_sql(&self) -> &[String]
获取已执行的 SQL 列表(用于断言)
Sourcepub fn assert_executed_count(&self, sql: &str, count: usize)
pub fn assert_executed_count(&self, sql: &str, count: usize)
断言某 SQL 被执行了恰好 n 次
Trait Implementations§
Source§impl Connection for MockConnection
impl Connection for MockConnection
fn execute<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Future<Output = Result<u64, DbError>> + Send + 'a>>
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>>
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
fn begin_transaction<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>
fn commit<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>
fn rollback<'a>( &'a mut self, ) -> Pin<Box<dyn Future<Output = Result<(), DbError>> + Send + 'a>>
fn is_connected(&self) -> bool
fn ping<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = bool> + Send + 'a>>
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>>
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>>
fn query_values<'a>( &'a mut self, sql: &'a str, ) -> Pin<Box<dyn Future<Output = Result<QueryValues, DbError>> + Send + 'a>>
位置式查询(SELECT):返回
(列名, 按列顺序的值矩阵) Read moreSource§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>>
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>>
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>>
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 moreAuto Trait Implementations§
impl Freeze for MockConnection
impl RefUnwindSafe for MockConnection
impl Send for MockConnection
impl Sync for MockConnection
impl Unpin for MockConnection
impl UnsafeUnpin for MockConnection
impl UnwindSafe for MockConnection
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
Mutably borrows from an owned value. Read more
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>
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 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>
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