From 85533a3bb3f9cfdab92212ce6c8d3ad1ac468dd3 Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sun, 28 Aug 2022 14:24:24 +0800 Subject: [PATCH] Give up and fix tests --- sea-orm-macros/src/derives/column.rs | 13 +------------ src/database/mock.rs | 10 +++++++--- src/entity/column.rs | 2 +- src/error.rs | 9 ++------- src/executor/query.rs | 12 +++++++----- tests/crud/error.rs | 6 +++--- tests/transaction_tests.rs | 12 +++++++++--- 7 files changed, 30 insertions(+), 34 deletions(-) diff --git a/sea-orm-macros/src/derives/column.rs b/sea-orm-macros/src/derives/column.rs index c60a91d69..3a0f9314f 100644 --- a/sea-orm-macros/src/derives/column.rs +++ b/sea-orm-macros/src/derives/column.rs @@ -91,17 +91,6 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result ) }); - 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!( #[automatically_derived] impl std::str::FromStr for #ident { @@ -110,7 +99,7 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result fn from_str(s: &str) -> std::result::Result { match s { #(#columns),*, - _ => Err(sea_orm::ColumnFromStrErr{ string: s.to_owned(), entity: #entity_name }), + _ => Err(sea_orm::ColumnFromStrErr(s.to_owned())), } } } diff --git a/src/database/mock.rs b/src/database/mock.rs index a1dbc795b..2c5b8d38e 100644 --- a/src/database/mock.rs +++ b/src/database/mock.rs @@ -102,7 +102,9 @@ impl MockDatabaseTrait for MockDatabase { result: ExecResultHolder::Mock(std::mem::take(&mut self.exec_results[counter])), }) } 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()) } 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 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 diff --git a/src/entity/column.rs b/src/entity/column.rs index 6f946f51b..8dbff6ea4 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -521,7 +521,7 @@ mod tests { )); assert!(matches!( fruit::Column::from_str("does_not_exist"), - Err(crate::ColumnFromStrErr { .. }) + Err(crate::ColumnFromStrErr(_)) )); } diff --git a/src/error.rs b/src/error.rs index 26bd5ddc2..34a828fb6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -76,10 +76,5 @@ impl Eq for DbErr {} /// Error during `impl FromStr for Entity::Column` #[derive(Error, Debug)] -#[error("Failed to match \"{string}\" as Column for `{entity}`")] -pub struct ColumnFromStrErr { - /// Source of error - pub string: String, - /// Entity this column belongs to - pub entity: String, -} +#[error("Failed to match \"{0}\" as Column")] +pub struct ColumnFromStrErr(pub String); diff --git a/src/executor/query.rs b/src/executor/query.rs index f27b1a2d9..90697fb33 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -816,20 +816,22 @@ try_from_u64_err!(uuid::Uuid); #[cfg(test)] mod tests { use super::TryGetError; - use crate::error::DbErr; + use crate::error::*; #[test] fn from_try_get_error() { // 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!( DbErr::from(try_get_error), - DbErr::Query("expected error message".to_owned()) + DbErr::Query(RuntimeErr::Internal("expected error message".to_owned())) ); // TryGetError::Null let try_get_error = TryGetError::Null("column".to_owned()); - let expected = "error occurred while decoding column: Null".to_owned(); - assert_eq!(DbErr::from(try_get_error), DbErr::Query(expected)); + let expected = "A null value was encountered while decoding column".to_owned(); + assert_eq!(DbErr::from(try_get_error), DbErr::Type(expected)); } } diff --git a/tests/crud/error.rs b/tests/crud/error.rs index 8dfab3faf..4828d9cdc 100644 --- a/tests/crud/error.rs +++ b/tests/crud/error.rs @@ -1,6 +1,6 @@ pub use super::*; use rust_decimal_macros::dec; -use sea_orm::DbErr; +use sea_orm::error::*; #[cfg(any( feature = "sqlx-mysql", feature = "sqlx-sqlite", @@ -32,7 +32,7 @@ pub async fn test_cake_error_sqlx(db: &DbConn) { #[cfg(any(feature = "sqlx-mysql", feature = "sqlx-sqlite"))] match error { - DbErr::ExecSqlX(sqlx_error) => match sqlx_error { + DbErr::Exec(RuntimeErr::SqlxError(error)) => match error { Error::Database(e) => { #[cfg(feature = "sqlx-mysql")] assert_eq!(e.code().unwrap(), "23000"); @@ -45,7 +45,7 @@ pub async fn test_cake_error_sqlx(db: &DbConn) { } #[cfg(feature = "sqlx-postgres")] match error { - DbErr::QuerySqlX(sqlx_error) => match sqlx_error { + DbErr::Query(RuntimeErr::SqlxError(error)) => match error { Error::Database(e) => { assert_eq!(e.code().unwrap(), "23505"); } diff --git a/tests/transaction_tests.rs b/tests/transaction_tests.rs index 1059da8ec..9faab72a9 100644 --- a/tests/transaction_tests.rs +++ b/tests/transaction_tests.rs @@ -508,7 +508,9 @@ pub async fn transaction_nested() { assert_eq!(bakeries.len(), 4); if true { - Err(DbErr::Query("Force Rollback!".to_owned())) + Err(DbErr::Query(RuntimeErr::Internal( + "Force Rollback!".to_owned(), + ))) } else { Ok(()) } @@ -633,7 +635,9 @@ pub async fn transaction_nested() { assert_eq!(bakeries.len(), 7); if true { - Err(DbErr::Query("Force Rollback!".to_owned())) + Err(DbErr::Query(RuntimeErr::Internal( + "Force Rollback!".to_owned(), + ))) } else { Ok(()) } @@ -652,7 +656,9 @@ pub async fn transaction_nested() { assert_eq!(bakeries.len(), 6); if true { - Err(DbErr::Query("Force Rollback!".to_owned())) + Err(DbErr::Query(RuntimeErr::Internal( + "Force Rollback!".to_owned(), + ))) } else { Ok(()) }