Fix again

This commit is contained in:
Chris Tsang 2023-01-09 15:20:01 +08:00
parent 5c93253877
commit d14e4a2160
2 changed files with 19 additions and 21 deletions

View File

@ -204,7 +204,7 @@ impl MockDatabaseTrait for MockDatabase {
impl MockRow {
/// Get a value from the [MockRow]
pub fn try_get_by<T, I: crate::ColIdx>(&self, index: I) -> Result<T, DbErr>
pub fn try_get<T, I: crate::ColIdx>(&self, index: I) -> Result<T, DbErr>
where
T: ValueType,
{
@ -212,14 +212,12 @@ impl MockRow {
T::try_from(self.values.get(index).unwrap().clone())
.map_err(|e| DbErr::Type(e.to_string()))
} else if let Some(index) = index.as_usize() {
let (_, value) =
self.values
.iter()
.nth(*index)
.ok_or(DbErr::Query(RuntimeErr::Internal(format!(
"Column at index {} not found",
index
))))?;
let (_, value) = self.values.iter().nth(*index).ok_or_else(|| {
DbErr::Query(RuntimeErr::Internal(format!(
"Column at index {} not found",
index
)))
})?;
T::try_from(value.clone()).map_err(|e| DbErr::Type(e.to_string()))
} else {
unreachable!("Missing ColIdx implementation for MockRow");
@ -701,10 +699,10 @@ mod tests {
);
let mocked_row = row.into_mock_row();
let a_id = mocked_row.try_get::<i32>("A_id");
let a_id = mocked_row.try_get::<i32, _>("A_id");
assert!(a_id.is_ok());
assert_eq!(1, a_id.unwrap());
let b_id = mocked_row.try_get::<i32>("B_id");
let b_id = mocked_row.try_get::<i32, _>("B_id");
assert!(b_id.is_ok());
assert_eq!(2, b_id.unwrap());
}

View File

@ -258,7 +258,7 @@ macro_rules! try_getable_all {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -296,7 +296,7 @@ macro_rules! try_getable_unsigned {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -331,7 +331,7 @@ macro_rules! try_getable_mysql {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -377,7 +377,7 @@ macro_rules! try_getable_date_time {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -476,7 +476,7 @@ impl TryGetable for Decimal {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -527,7 +527,7 @@ impl TryGetable for BigDecimal {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -571,7 +571,7 @@ impl TryGetable for u32 {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -614,7 +614,7 @@ mod postgres_array {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -702,7 +702,7 @@ mod postgres_array {
}
#[cfg(feature = "mock")]
#[allow(unused_variables)]
QueryResultRow::Mock(row) => row.try_get_by(idx).map_err(|e| {
QueryResultRow::Mock(row) => row.try_get(idx).map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)
}),
@ -994,7 +994,7 @@ where
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => row
.try_get_by::<serde_json::Value, I>(idx)
.try_get::<serde_json::Value, I>(idx)
.map_err(|e| {
debug_print!("{:#?}", e.to_string());
err_null_idx_col(idx)