Add Tests And Fix Build Issue For "Rework active enum" PR (#1900)
* Add Tests For Root JSON Arrays And Active Enum Vectors * Fix Build Issue When Using `DeriveActiveEnum`
This commit is contained in:
parent
b2cb51380a
commit
201d6fe9f9
@ -342,7 +342,7 @@ impl ActiveEnum {
|
|||||||
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, index: I) -> std::result::Result<Vec<Self>, sea_orm::TryGetError> {
|
fn try_get_by<I: sea_orm::ColIdx>(res: &sea_orm::QueryResult, index: I) -> std::result::Result<Vec<Self>, sea_orm::TryGetError> {
|
||||||
<<Self as sea_orm::ActiveEnum>::Value as sea_orm::ActiveEnumValue>::try_get_vec_by(res, index)?
|
<<Self as sea_orm::ActiveEnum>::Value as sea_orm::ActiveEnumValue>::try_get_vec_by(res, index)?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|value| Self::try_from_value(&value).map_err(Into::into))
|
.map(|value| <Self as sea_orm::ActiveEnum>::try_from_value(&value).map_err(Into::into))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,11 @@ async fn main() -> Result<(), DbErr> {
|
|||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
insert_active_enum(&ctx.db).await?;
|
insert_active_enum(&ctx.db).await?;
|
||||||
insert_active_enum_child(&ctx.db).await?;
|
insert_active_enum_child(&ctx.db).await?;
|
||||||
|
|
||||||
|
if cfg!(feature = "sqlx-postgres") {
|
||||||
|
insert_active_enum_vec(&ctx.db).await?;
|
||||||
|
}
|
||||||
|
|
||||||
find_related_active_enum(&ctx.db).await?;
|
find_related_active_enum(&ctx.db).await?;
|
||||||
find_linked_active_enum(&ctx.db).await?;
|
find_linked_active_enum(&ctx.db).await?;
|
||||||
ctx.delete().await;
|
ctx.delete().await;
|
||||||
@ -205,6 +210,72 @@ pub async fn insert_active_enum_child(db: &DatabaseConnection) -> Result<(), DbE
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn insert_active_enum_vec(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
|
use categories::*;
|
||||||
|
|
||||||
|
let model = Model {
|
||||||
|
id: 1,
|
||||||
|
categories: None,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
model,
|
||||||
|
ActiveModel {
|
||||||
|
id: Set(1),
|
||||||
|
categories: Set(None),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.insert(db)
|
||||||
|
.await?
|
||||||
|
);
|
||||||
|
assert_eq!(model, Entity::find().one(db).await?.unwrap());
|
||||||
|
assert_eq!(
|
||||||
|
model,
|
||||||
|
Entity::find()
|
||||||
|
.filter(Column::Id.is_not_null())
|
||||||
|
.filter(Column::Categories.is_null())
|
||||||
|
.one(db)
|
||||||
|
.await?
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
|
let _ = ActiveModel {
|
||||||
|
id: Set(1),
|
||||||
|
categories: Set(Some(vec![Category::Big, Category::Small])),
|
||||||
|
..model.into_active_model()
|
||||||
|
}
|
||||||
|
.save(db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let model = Entity::find().one(db).await?.unwrap();
|
||||||
|
assert_eq!(
|
||||||
|
model,
|
||||||
|
Model {
|
||||||
|
id: 1,
|
||||||
|
categories: Some(vec![Category::Big, Category::Small]),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
model,
|
||||||
|
Entity::find()
|
||||||
|
.filter(Column::Id.eq(1))
|
||||||
|
.filter(Expr::cust_with_values(
|
||||||
|
r#"$1 = ANY("categories")"#,
|
||||||
|
vec![Category::Big]
|
||||||
|
))
|
||||||
|
.one(db)
|
||||||
|
.await?
|
||||||
|
.unwrap()
|
||||||
|
);
|
||||||
|
|
||||||
|
let res = model.delete(db).await?;
|
||||||
|
|
||||||
|
assert_eq!(res.rows_affected, 1);
|
||||||
|
assert_eq!(Entity::find().one(db).await?, None);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn find_related_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn find_related_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
active_enum::Model {
|
active_enum::Model {
|
||||||
|
16
tests/common/features/active_enum_vec.rs
Normal file
16
tests/common/features/active_enum_vec.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
use super::sea_orm_active_enums::*;
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
|
#[cfg_attr(feature = "sqlx-postgres", sea_orm(schema_name = "public"))]
|
||||||
|
#[sea_orm(table_name = "active_enum")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
pub categories: Option<Vec<Category>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
15
tests/common/features/categories.rs
Normal file
15
tests/common/features/categories.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
use super::sea_orm_active_enums::*;
|
||||||
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
|
#[sea_orm(table_name = "categories")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key, auto_increment = false)]
|
||||||
|
pub id: i32,
|
||||||
|
pub categories: Option<Vec<Category>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
@ -1,19 +1,49 @@
|
|||||||
use sea_orm::entity::prelude::*;
|
pub mod json_string_vec {
|
||||||
use sea_orm::FromJsonQueryResult;
|
use sea_orm::entity::prelude::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use sea_orm::FromJsonQueryResult;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
#[sea_orm(table_name = "json_vec")]
|
#[sea_orm(table_name = "json_vec")]
|
||||||
pub struct Model {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub str_vec: Option<StringVec>,
|
pub str_vec: Option<StringVec>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
||||||
|
pub struct StringVec(pub Vec<String>);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
pub mod json_struct_vec {
|
||||||
pub enum Relation {}
|
use sea_orm::entity::prelude::*;
|
||||||
|
use sea_orm_macros::FromJsonQueryResult;
|
||||||
|
use sea_query::with_array::NotU8;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
||||||
|
pub struct JsonColumn {
|
||||||
|
pub value: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
|
impl NotU8 for JsonColumn {}
|
||||||
pub struct StringVec(pub Vec<String>);
|
|
||||||
|
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
|
||||||
|
#[sea_orm(table_name = "json_struct_vec")]
|
||||||
|
pub struct Model {
|
||||||
|
#[sea_orm(primary_key)]
|
||||||
|
pub id: i32,
|
||||||
|
#[sea_orm(column_type = "JsonBinary")]
|
||||||
|
pub struct_vec: Vec<JsonColumn>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
pub mod active_enum;
|
pub mod active_enum;
|
||||||
pub mod active_enum_child;
|
pub mod active_enum_child;
|
||||||
|
pub mod active_enum_vec;
|
||||||
pub mod applog;
|
pub mod applog;
|
||||||
pub mod binary;
|
pub mod binary;
|
||||||
pub mod bits;
|
pub mod bits;
|
||||||
pub mod byte_primary_key;
|
pub mod byte_primary_key;
|
||||||
|
pub mod categories;
|
||||||
pub mod collection;
|
pub mod collection;
|
||||||
pub mod collection_expanded;
|
pub mod collection_expanded;
|
||||||
pub mod custom_active_model;
|
pub mod custom_active_model;
|
||||||
@ -28,10 +30,12 @@ pub mod value_type;
|
|||||||
|
|
||||||
pub use active_enum::Entity as ActiveEnum;
|
pub use active_enum::Entity as ActiveEnum;
|
||||||
pub use active_enum_child::Entity as ActiveEnumChild;
|
pub use active_enum_child::Entity as ActiveEnumChild;
|
||||||
|
pub use active_enum_vec::Entity as ActiveEnumVec;
|
||||||
pub use applog::Entity as Applog;
|
pub use applog::Entity as Applog;
|
||||||
pub use binary::Entity as Binary;
|
pub use binary::Entity as Binary;
|
||||||
pub use bits::Entity as Bits;
|
pub use bits::Entity as Bits;
|
||||||
pub use byte_primary_key::Entity as BytePrimaryKey;
|
pub use byte_primary_key::Entity as BytePrimaryKey;
|
||||||
|
pub use categories::Entity as Categories;
|
||||||
pub use collection::Entity as Collection;
|
pub use collection::Entity as Collection;
|
||||||
pub use collection_expanded::Entity as CollectionExpanded;
|
pub use collection_expanded::Entity as CollectionExpanded;
|
||||||
pub use dyn_table_name_lazy_static::Entity as DynTableNameLazyStatic;
|
pub use dyn_table_name_lazy_static::Entity as DynTableNameLazyStatic;
|
||||||
@ -40,6 +44,7 @@ pub use event_trigger::Entity as EventTrigger;
|
|||||||
pub use insert_default::Entity as InsertDefault;
|
pub use insert_default::Entity as InsertDefault;
|
||||||
pub use json_struct::Entity as JsonStruct;
|
pub use json_struct::Entity as JsonStruct;
|
||||||
pub use json_vec::Entity as JsonVec;
|
pub use json_vec::Entity as JsonVec;
|
||||||
|
pub use json_vec_derive::json_struct_vec::Entity as JsonStructVec;
|
||||||
pub use metadata::Entity as Metadata;
|
pub use metadata::Entity as Metadata;
|
||||||
pub use pi::Entity as Pi;
|
pub use pi::Entity as Pi;
|
||||||
pub use repository::Entity as Repository;
|
pub use repository::Entity as Repository;
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::common::features::json_vec_derive::json_struct_vec;
|
||||||
use crate::common::setup::{create_enum, create_table, create_table_without_asserts};
|
use crate::common::setup::{create_enum, create_table, create_table_without_asserts};
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
|
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
|
||||||
ExecResult, Schema,
|
ExecResult, Schema,
|
||||||
};
|
};
|
||||||
use sea_query::{
|
use sea_query::{
|
||||||
extension::postgres::Type, Alias, BlobSize, ColumnDef, ForeignKeyCreateStatement, IntoIden,
|
extension::postgres::Type, Alias, BlobSize, ColumnDef, ColumnType, ForeignKeyCreateStatement,
|
||||||
|
IntoIden,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
@ -18,7 +20,6 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
create_byte_primary_key_table(db).await?;
|
create_byte_primary_key_table(db).await?;
|
||||||
create_satellites_table(db).await?;
|
create_satellites_table(db).await?;
|
||||||
create_transaction_log_table(db).await?;
|
create_transaction_log_table(db).await?;
|
||||||
create_json_vec_table(db).await?;
|
|
||||||
create_json_struct_table(db).await?;
|
create_json_struct_table(db).await?;
|
||||||
|
|
||||||
let create_enum_stmts = match db_backend {
|
let create_enum_stmts = match db_backend {
|
||||||
@ -54,6 +55,9 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
create_value_type_postgres_table(db).await?;
|
create_value_type_postgres_table(db).await?;
|
||||||
create_collection_table(db).await?;
|
create_collection_table(db).await?;
|
||||||
create_event_trigger_table(db).await?;
|
create_event_trigger_table(db).await?;
|
||||||
|
create_json_vec_table(db).await?;
|
||||||
|
create_json_struct_vec_table(db).await?;
|
||||||
|
create_categories_table(db).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -319,6 +323,26 @@ pub async fn create_json_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|||||||
create_table(db, &create_table_stmt, JsonVec).await
|
create_table(db, &create_table_stmt, JsonVec).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_json_struct_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let create_table_stmt = sea_query::Table::create()
|
||||||
|
.table(json_struct_vec::Entity.table_ref())
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(json_struct_vec::Column::Id)
|
||||||
|
.integer()
|
||||||
|
.not_null()
|
||||||
|
.auto_increment()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(json_struct_vec::Column::StructVec)
|
||||||
|
.json_binary()
|
||||||
|
.not_null(),
|
||||||
|
)
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
create_table(db, &create_table_stmt, JsonStructVec).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_json_struct_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
let stmt = sea_query::Table::create()
|
let stmt = sea_query::Table::create()
|
||||||
.table(json_struct::Entity)
|
.table(json_struct::Entity)
|
||||||
@ -521,6 +545,21 @@ pub async fn create_teas_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
|||||||
create_table(db, &create_table_stmt, Teas).await
|
create_table(db, &create_table_stmt, Teas).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn create_categories_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
|
let create_table_stmt = sea_query::Table::create()
|
||||||
|
.table(categories::Entity.table_ref())
|
||||||
|
.col(
|
||||||
|
ColumnDef::new(categories::Column::Id)
|
||||||
|
.integer()
|
||||||
|
.not_null()
|
||||||
|
.primary_key(),
|
||||||
|
)
|
||||||
|
.col(ColumnDef::new(categories::Column::Categories).array(ColumnType::String(Some(1))))
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
create_table(db, &create_table_stmt, Categories).await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn create_binary_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
pub async fn create_binary_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||||
let create_table_stmt = sea_query::Table::create()
|
let create_table_stmt = sea_query::Table::create()
|
||||||
.table(binary::Entity.table_ref())
|
.table(binary::Entity.table_ref())
|
||||||
|
@ -5,16 +5,14 @@ use pretty_assertions::assert_eq;
|
|||||||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
#[cfg(any(
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
feature = "sqlx-mysql",
|
|
||||||
feature = "sqlx-sqlite",
|
|
||||||
feature = "sqlx-postgres"
|
|
||||||
))]
|
|
||||||
async fn main() -> Result<(), DbErr> {
|
async fn main() -> Result<(), DbErr> {
|
||||||
let ctx = TestContext::new("json_vec_tests").await;
|
let ctx = TestContext::new("json_vec_tests").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
insert_json_vec(&ctx.db).await?;
|
insert_json_vec(&ctx.db).await?;
|
||||||
insert_json_vec_derive(&ctx.db).await?;
|
|
||||||
|
insert_json_string_vec_derive(&ctx.db).await?;
|
||||||
|
|
||||||
ctx.delete().await;
|
ctx.delete().await;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -44,10 +42,10 @@ pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn insert_json_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn insert_json_string_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
let json_vec = json_vec_derive::Model {
|
let json_vec = json_vec_derive::json_string_vec::Model {
|
||||||
id: 2,
|
id: 2,
|
||||||
str_vec: Some(json_vec_derive::StringVec(vec![
|
str_vec: Some(json_vec_derive::json_string_vec::StringVec(vec![
|
||||||
"4".to_string(),
|
"4".to_string(),
|
||||||
"5".to_string(),
|
"5".to_string(),
|
||||||
"6".to_string(),
|
"6".to_string(),
|
||||||
@ -58,8 +56,37 @@ pub async fn insert_json_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr
|
|||||||
|
|
||||||
assert_eq!(result, json_vec);
|
assert_eq!(result, json_vec);
|
||||||
|
|
||||||
let model = json_vec_derive::Entity::find()
|
let model = json_vec_derive::json_string_vec::Entity::find()
|
||||||
.filter(json_vec_derive::Column::Id.eq(json_vec.id))
|
.filter(json_vec_derive::json_string_vec::Column::Id.eq(json_vec.id))
|
||||||
|
.one(db)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
assert_eq!(model, Some(json_vec));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn insert_json_struct_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
|
let json_vec = json_vec_derive::json_struct_vec::Model {
|
||||||
|
id: 2,
|
||||||
|
struct_vec: vec![
|
||||||
|
json_vec_derive::json_struct_vec::JsonColumn {
|
||||||
|
value: "4".to_string(),
|
||||||
|
},
|
||||||
|
json_vec_derive::json_struct_vec::JsonColumn {
|
||||||
|
value: "5".to_string(),
|
||||||
|
},
|
||||||
|
json_vec_derive::json_struct_vec::JsonColumn {
|
||||||
|
value: "6".to_string(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = json_vec.clone().into_active_model().insert(db).await?;
|
||||||
|
assert_eq!(result, json_vec);
|
||||||
|
|
||||||
|
let model = json_vec_derive::json_struct_vec::Entity::find()
|
||||||
|
.filter(json_vec_derive::json_struct_vec::Column::Id.eq(json_vec.id))
|
||||||
.one(db)
|
.one(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user