Skip to content

Commit

Permalink
Test integer enum
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Oct 20, 2021
1 parent f1ef7d9 commit 868a469
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 17 deletions.
36 changes: 30 additions & 6 deletions tests/active_enum_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,43 @@ async fn main() -> Result<(), DbErr> {
}

pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
active_enum::ActiveModel {
category: Set(active_enum::Category::Big),
use active_enum::*;

let am = ActiveModel {
category: Set(None),
color: Set(None),
// tea: Set(None),
..Default::default()
}
.insert(db)
.await?;

assert_eq!(
active_enum::Entity::find().one(db).await?.unwrap(),
active_enum::Model {
Entity::find().one(db).await?.unwrap(),
Model {
id: 1,
category: None,
color: None,
// tea: None,
}
);

ActiveModel {
category: Set(Some(Category::Big)),
color: Set(Some(Color::Black)),
// tea: Set(Some(Tea::EverydayTea)),
..am
}
.save(db)
.await?;

assert_eq!(
Entity::find().one(db).await?.unwrap(),
Model {
id: 1,
category: active_enum::Category::Big,
category_opt: None,
category: Some(Category::Big),
color: Some(Color::Black),
// tea: Some(Tea::EverydayTea),
}
);

Expand Down
23 changes: 21 additions & 2 deletions tests/common/features/active_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use sea_orm::entity::prelude::*;
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub category: Category,
pub category_opt: Option<Category>,
pub category: Option<Category>,
pub color: Option<Color>,
// pub tea: Option<Tea>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand All @@ -22,3 +23,21 @@ pub enum Category {
#[sea_orm(string_value = "S")]
Small,
}

#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)]
#[sea_orm(rs_type = "i32", db_type = r#"Integer"#)]
pub enum Color {
#[sea_orm(num_value = 0)]
Black,
#[sea_orm(num_value = 1)]
White,
}

#[derive(Debug, Clone, PartialEq, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = r#"Custom("tea".to_owned())"#)]
pub enum Tea {
#[sea_orm(string_value = "EverydayTea")]
EverydayTea,
#[sea_orm(string_value = "BreakfastTea")]
BreakfastTea,
}
13 changes: 5 additions & 8 deletions tests/common/features/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ pub use super::super::bakery_chain::*;

use super::*;
use crate::common::setup::create_table;
use sea_orm::{error::*, sea_query, DatabaseConnection, DbConn, ExecResult};
use sea_query::ColumnDef;
use sea_orm::{ConnectionTrait, DatabaseConnection, DbConn, ExecResult, Statement, error::*, sea_query};
use sea_query::{Alias, ColumnDef};

pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
create_log_table(db).await?;
Expand Down Expand Up @@ -87,12 +87,9 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(active_enum::Column::Category)
.string_len(1)
.not_null(),
)
.col(ColumnDef::new(active_enum::Column::CategoryOpt).string_len(1))
.col(ColumnDef::new(active_enum::Column::Category).string_len(1))
.col(ColumnDef::new(active_enum::Column::Color).integer())
// .col(ColumnDef::new(active_enum::Column::Tea).custom(Alias::new("tea")))
.to_owned();

create_table(db, &stmt, ActiveEnum).await
Expand Down
2 changes: 1 addition & 1 deletion tests/common/setup/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use pretty_assertions::assert_eq;
use sea_orm::{
ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DbBackend, DbConn, DbErr,
EntityTrait, ExecResult, Schema, Statement,
};

use sea_query::{Alias, Table, TableCreateStatement};

pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
Expand Down

0 comments on commit 868a469

Please sign in to comment.