Skip to content

Commit

Permalink
Give up and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 28, 2022
1 parent 0ce0f49 commit eb85b79
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
13 changes: 1 addition & 12 deletions sea-orm-macros/src/derives/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
#[automatically_derived]
impl std::str::FromStr for #ident {
Expand All @@ -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> {
match s {
#(#columns),*,
_ => Err(sea_orm::ColumnFromStrErr{ string: s.to_owned(), entity: #entity_name }),
_ => Err(sea_orm::ColumnFromStrErr(s.to_owned())),
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/database/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)))
}
}

Expand All @@ -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(),
)))
}
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mod tests {
));
assert!(matches!(
fruit::Column::from_str("does_not_exist"),
Err(crate::ColumnFromStrErr { .. })
Err(crate::ColumnFromStrErr(_))
));
}

Expand Down
9 changes: 2 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
10 changes: 5 additions & 5 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,20 +816,20 @@ 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));
}
}

0 comments on commit eb85b79

Please sign in to comment.