Skip to content

Commit

Permalink
Add Tests For Root JSON Arrays And Active Enum Vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
anshap1719 committed Oct 8, 2023
1 parent b2cb513 commit 59368d7
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 28 deletions.
71 changes: 71 additions & 0 deletions tests/active_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ async fn main() -> Result<(), DbErr> {
create_tables(&ctx.db).await?;
insert_active_enum(&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_linked_active_enum(&ctx.db).await?;
ctx.delete().await;
Expand Down Expand Up @@ -205,6 +210,72 @@ pub async fn insert_active_enum_child(db: &DatabaseConnection) -> Result<(), DbE
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> {
assert_eq!(
active_enum::Model {
Expand Down
16 changes: 16 additions & 0 deletions tests/common/features/active_enum_vec.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/common/features/categories.rs
Original file line number Diff line number Diff line change
@@ -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 {}
60 changes: 45 additions & 15 deletions tests/common/features/json_vec_derive.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,49 @@
use sea_orm::entity::prelude::*;
use sea_orm::FromJsonQueryResult;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_vec")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub str_vec: Option<StringVec>,
pub mod json_string_vec {
use sea_orm::entity::prelude::*;
use sea_orm::FromJsonQueryResult;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_vec")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
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 enum Relation {}
pub mod json_struct_vec {
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)]
pub struct StringVec(pub Vec<String>);
impl NotU8 for JsonColumn {}

#[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 {}
}
5 changes: 5 additions & 0 deletions tests/common/features/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pub mod active_enum;
pub mod active_enum_child;
pub mod active_enum_vec;
pub mod applog;
pub mod binary;
pub mod bits;
pub mod byte_primary_key;
pub mod categories;
pub mod collection;
pub mod collection_expanded;
pub mod custom_active_model;
Expand All @@ -28,10 +30,12 @@ pub mod value_type;

pub use active_enum::Entity as ActiveEnum;
pub use active_enum_child::Entity as ActiveEnumChild;
pub use active_enum_vec::Entity as ActiveEnumVec;
pub use applog::Entity as Applog;
pub use binary::Entity as Binary;
pub use bits::Entity as Bits;
pub use byte_primary_key::Entity as BytePrimaryKey;
pub use categories::Entity as Categories;
pub use collection::Entity as Collection;
pub use collection_expanded::Entity as CollectionExpanded;
pub use dyn_table_name_lazy_static::Entity as DynTableNameLazyStatic;
Expand All @@ -40,6 +44,7 @@ pub use event_trigger::Entity as EventTrigger;
pub use insert_default::Entity as InsertDefault;
pub use json_struct::Entity as JsonStruct;
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 pi::Entity as Pi;
pub use repository::Entity as Repository;
Expand Down
43 changes: 41 additions & 2 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
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 sea_orm::{
error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
ExecResult, Schema,
};
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> {
Expand All @@ -18,7 +20,6 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_byte_primary_key_table(db).await?;
create_satellites_table(db).await?;
create_transaction_log_table(db).await?;
create_json_vec_table(db).await?;
create_json_struct_table(db).await?;

let create_enum_stmts = match db_backend {
Expand Down Expand Up @@ -54,6 +55,9 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_value_type_postgres_table(db).await?;
create_collection_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(())
Expand Down Expand Up @@ -319,6 +323,26 @@ pub async fn create_json_vec_table(db: &DbConn) -> Result<ExecResult, DbErr> {
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> {
let stmt = sea_query::Table::create()
.table(json_struct::Entity)
Expand Down Expand Up @@ -521,6 +545,21 @@ pub async fn create_teas_table(db: &DbConn) -> Result<ExecResult, DbErr> {
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> {
let create_table_stmt = sea_query::Table::create()
.table(binary::Entity.table_ref())
Expand Down
49 changes: 38 additions & 11 deletions tests/json_vec_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection};

#[sea_orm_macros::test]
#[cfg(any(
feature = "sqlx-mysql",
feature = "sqlx-sqlite",
feature = "sqlx-postgres"
))]
#[cfg(feature = "sqlx-postgres")]
async fn main() -> Result<(), DbErr> {
let ctx = TestContext::new("json_vec_tests").await;
create_tables(&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;

Ok(())
Expand Down Expand Up @@ -44,10 +42,10 @@ pub async fn insert_json_vec(db: &DatabaseConnection) -> Result<(), DbErr> {
Ok(())
}

pub async fn insert_json_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
let json_vec = json_vec_derive::Model {
pub async fn insert_json_string_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr> {
let json_vec = json_vec_derive::json_string_vec::Model {
id: 2,
str_vec: Some(json_vec_derive::StringVec(vec![
str_vec: Some(json_vec_derive::json_string_vec::StringVec(vec![
"4".to_string(),
"5".to_string(),
"6".to_string(),
Expand All @@ -58,8 +56,37 @@ pub async fn insert_json_vec_derive(db: &DatabaseConnection) -> Result<(), DbErr

assert_eq!(result, json_vec);

let model = json_vec_derive::Entity::find()
.filter(json_vec_derive::Column::Id.eq(json_vec.id))
let model = json_vec_derive::json_string_vec::Entity::find()
.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)
.await?;

Expand Down

0 comments on commit 59368d7

Please sign in to comment.