Skip to content

Commit

Permalink
fixed tests, adjusted code position and completed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
darkmmon committed Jun 27, 2023
1 parent af071f9 commit 2b69c2a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
14 changes: 9 additions & 5 deletions sea-orm-macros/src/derives/value_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ impl DeriveValueType {
}) => Some(unnamed),
_ => None,
};
if fields.clone().expect("hello").into_iter().count() != 1 {
panic!()
};

let ty = fields
.expect("not a struct")
.expect("This derive accept only struct")
.first()
.expect("empty type")
.expect("The struct should contain one value field")
.to_owned()
.ty;
let name = input.ident;
Expand All @@ -29,11 +33,11 @@ impl DeriveValueType {
}

fn expand(&self) -> syn::Result<TokenStream> {
let expanded_impl_entity_name: TokenStream = self.impl_entity_name();
Ok(expanded_impl_entity_name)
let expanded_impl_value_type: TokenStream = self.impl_value_type();
Ok(expanded_impl_value_type)
}

fn impl_entity_name(&self) -> TokenStream {
fn impl_value_type(&self) -> TokenStream {
let name = &self.name;
let ty = &self.ty;

Expand Down
7 changes: 5 additions & 2 deletions tests/common/features/custom_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ use sea_orm::entity::prelude::*;
use sea_orm_macros::DeriveValueType;

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_vec")]
#[sea_orm(table_name = "custom_wrapper")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub str_vec: StringVec,
pub number: Integer,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

#[derive(Clone, Debug, PartialEq, Eq, DeriveValueType)]
pub struct Integer(pub i32);

#[derive(Clone, Debug, PartialEq, Eq, DeriveValueType)]
pub struct StringVec(pub Vec<String>);
21 changes: 21 additions & 0 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_binary_table(db).await?;
create_bits_table(db).await?;
create_dyn_table_name_lazy_static_table(db).await?;
create_custom_wrapper_table(db).await?;

if DbBackend::Postgres == db_backend {
create_collection_table(db).await?;
Expand Down Expand Up @@ -634,3 +635,23 @@ pub async fn create_dyn_table_name_lazy_static_table(db: &DbConn) -> Result<(),

Ok(())
}

pub async fn create_custom_wrapper_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(custom_wrapper::Entity)
.col(
ColumnDef::new(custom_wrapper::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(custom_wrapper::Column::Number)
.integer()
.not_null(),
)
.to_owned();

create_table(db, &stmt, custom_wrapper::Entity).await
}
36 changes: 26 additions & 10 deletions tests/custom_wrapper_type_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ pub mod common;

pub use common::{
features::{
custom_wrapper::{Model, StringVec},
custom_wrapper::{Integer, Model, StringVec},
*,
},
setup::*,
TestContext,
};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, DatabaseConnection};
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
use sea_query::ValueType;

#[sea_orm_macros::test]
#[cfg(all(feature = "sqlx-postgres", feature = "postgres-array"))]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("custom_wrapper_tests").await;
create_tables(&ctx.db).await?;
Expand All @@ -23,15 +27,10 @@ async fn main() -> Result<(), DbErr> {
Ok(())
}

pub async fn insert_value(_db: &DatabaseConnection) -> Result<(), DbErr> {
pub async fn insert_value(db: &DatabaseConnection) -> Result<(), DbErr> {
assert_eq!(StringVec::type_name(), "StringVec");

let model = Model {
id: 1,
str_vec: StringVec(vec!["ab".to_string(), "cd".to_string()]),
};

let string = Value::from(model.str_vec);
let string = Value::from(StringVec(vec!["ab".to_string(), "cd".to_string()]));
assert_eq!(
string,
Value::Array(
Expand All @@ -43,5 +42,22 @@ pub async fn insert_value(_db: &DatabaseConnection) -> Result<(), DbErr> {
)
);

let random_testing_int = 523;
let value_random_testing_int = sea_query::Value::Int(Some(523));

let direct_int = Integer(random_testing_int);
let unwrap_int = Integer::unwrap(value_random_testing_int);

assert_eq!(direct_int, unwrap_int);

let model = Model {
id: 1,
number: Integer(48),
};

let result = model.clone().into_active_model().insert(db).await?;

assert_eq!(result, model);

Ok(())
}

0 comments on commit 2b69c2a

Please sign in to comment.