Give up and fix tests

This commit is contained in:
Chris Tsang 2022-08-28 14:24:24 +08:00
parent 0ce0f49551
commit 85533a3bb3
7 changed files with 30 additions and 34 deletions

View File

@ -91,17 +91,6 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream>
) )
}); });
let entity_name = data_enum
.variants
.first()
.map(|column| {
let column_iden = column.ident.clone();
quote!(
#ident::#column_iden.entity_name().to_string()
)
})
.unwrap();
Ok(quote!( Ok(quote!(
#[automatically_derived] #[automatically_derived]
impl std::str::FromStr for #ident { impl std::str::FromStr for #ident {
@ -110,7 +99,7 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream>
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s { match s {
#(#columns),*, #(#columns),*,
_ => Err(sea_orm::ColumnFromStrErr{ string: s.to_owned(), entity: #entity_name }), _ => Err(sea_orm::ColumnFromStrErr(s.to_owned())),
} }
} }
} }

View File

@ -102,7 +102,9 @@ impl MockDatabaseTrait for MockDatabase {
result: ExecResultHolder::Mock(std::mem::take(&mut self.exec_results[counter])), result: ExecResultHolder::Mock(std::mem::take(&mut self.exec_results[counter])),
}) })
} else { } else {
Err(DbErr::Query("`exec_results` buffer is empty.".to_owned())) Err(DbErr::Query(RuntimeErr::Internal(
"`exec_results` buffer is empty.".to_owned(),
)))
} }
} }
@ -121,7 +123,9 @@ impl MockDatabaseTrait for MockDatabase {
}) })
.collect()) .collect())
} else { } else {
Err(DbErr::Query("`query_results` buffer is empty.".to_owned())) Err(DbErr::Query(RuntimeErr::Internal(
"`query_results` buffer is empty.".to_owned(),
)))
} }
} }
@ -176,7 +180,7 @@ impl MockRow {
where where
T: ValueType, T: ValueType,
{ {
T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Query(e.to_string())) T::try_from(self.values.get(col).unwrap().clone()).map_err(|e| DbErr::Type(e.to_string()))
} }
/// An iterator over the keys and values of a mock row /// An iterator over the keys and values of a mock row

View File

@ -521,7 +521,7 @@ mod tests {
)); ));
assert!(matches!( assert!(matches!(
fruit::Column::from_str("does_not_exist"), fruit::Column::from_str("does_not_exist"),
Err(crate::ColumnFromStrErr { .. }) Err(crate::ColumnFromStrErr(_))
)); ));
} }

View File

@ -76,10 +76,5 @@ impl Eq for DbErr {}
/// Error during `impl FromStr for Entity::Column` /// Error during `impl FromStr for Entity::Column`
#[derive(Error, Debug)] #[derive(Error, Debug)]
#[error("Failed to match \"{string}\" as Column for `{entity}`")] #[error("Failed to match \"{0}\" as Column")]
pub struct ColumnFromStrErr { pub struct ColumnFromStrErr(pub String);
/// Source of error
pub string: String,
/// Entity this column belongs to
pub entity: String,
}

View File

@ -816,20 +816,22 @@ try_from_u64_err!(uuid::Uuid);
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::TryGetError; use super::TryGetError;
use crate::error::DbErr; use crate::error::*;
#[test] #[test]
fn from_try_get_error() { fn from_try_get_error() {
// TryGetError::DbErr // TryGetError::DbErr
let try_get_error = TryGetError::DbErr(DbErr::Query("expected error message".to_owned())); let try_get_error = TryGetError::DbErr(DbErr::Query(RuntimeErr::Internal(
"expected error message".to_owned(),
)));
assert_eq!( assert_eq!(
DbErr::from(try_get_error), DbErr::from(try_get_error),
DbErr::Query("expected error message".to_owned()) DbErr::Query(RuntimeErr::Internal("expected error message".to_owned()))
); );
// TryGetError::Null // TryGetError::Null
let try_get_error = TryGetError::Null("column".to_owned()); let try_get_error = TryGetError::Null("column".to_owned());
let expected = "error occurred while decoding column: Null".to_owned(); let expected = "A null value was encountered while decoding column".to_owned();
assert_eq!(DbErr::from(try_get_error), DbErr::Query(expected)); assert_eq!(DbErr::from(try_get_error), DbErr::Type(expected));
} }
} }

View File

@ -1,6 +1,6 @@
pub use super::*; pub use super::*;
use rust_decimal_macros::dec; use rust_decimal_macros::dec;
use sea_orm::DbErr; use sea_orm::error::*;
#[cfg(any( #[cfg(any(
feature = "sqlx-mysql", feature = "sqlx-mysql",
feature = "sqlx-sqlite", feature = "sqlx-sqlite",
@ -32,7 +32,7 @@ pub async fn test_cake_error_sqlx(db: &DbConn) {
#[cfg(any(feature = "sqlx-mysql", feature = "sqlx-sqlite"))] #[cfg(any(feature = "sqlx-mysql", feature = "sqlx-sqlite"))]
match error { match error {
DbErr::ExecSqlX(sqlx_error) => match sqlx_error { DbErr::Exec(RuntimeErr::SqlxError(error)) => match error {
Error::Database(e) => { Error::Database(e) => {
#[cfg(feature = "sqlx-mysql")] #[cfg(feature = "sqlx-mysql")]
assert_eq!(e.code().unwrap(), "23000"); assert_eq!(e.code().unwrap(), "23000");
@ -45,7 +45,7 @@ pub async fn test_cake_error_sqlx(db: &DbConn) {
} }
#[cfg(feature = "sqlx-postgres")] #[cfg(feature = "sqlx-postgres")]
match error { match error {
DbErr::QuerySqlX(sqlx_error) => match sqlx_error { DbErr::Query(RuntimeErr::SqlxError(error)) => match error {
Error::Database(e) => { Error::Database(e) => {
assert_eq!(e.code().unwrap(), "23505"); assert_eq!(e.code().unwrap(), "23505");
} }

View File

@ -508,7 +508,9 @@ pub async fn transaction_nested() {
assert_eq!(bakeries.len(), 4); assert_eq!(bakeries.len(), 4);
if true { if true {
Err(DbErr::Query("Force Rollback!".to_owned())) Err(DbErr::Query(RuntimeErr::Internal(
"Force Rollback!".to_owned(),
)))
} else { } else {
Ok(()) Ok(())
} }
@ -633,7 +635,9 @@ pub async fn transaction_nested() {
assert_eq!(bakeries.len(), 7); assert_eq!(bakeries.len(), 7);
if true { if true {
Err(DbErr::Query("Force Rollback!".to_owned())) Err(DbErr::Query(RuntimeErr::Internal(
"Force Rollback!".to_owned(),
)))
} else { } else {
Ok(()) Ok(())
} }
@ -652,7 +656,9 @@ pub async fn transaction_nested() {
assert_eq!(bakeries.len(), 6); assert_eq!(bakeries.len(), 6);
if true { if true {
Err(DbErr::Query("Force Rollback!".to_owned())) Err(DbErr::Query(RuntimeErr::Internal(
"Force Rollback!".to_owned(),
)))
} else { } else {
Ok(()) Ok(())
} }