Test integer enum
This commit is contained in:
parent
f1ef7d9c47
commit
868a469de0
@ -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: active_enum::Category::Big,
|
||||
category_opt: None,
|
||||
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: Some(Category::Big),
|
||||
color: Some(Color::Black),
|
||||
// tea: Some(Tea::EverydayTea),
|
||||
}
|
||||
);
|
||||
|
||||
|
@ -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)]
|
||||
@ -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,
|
||||
}
|
||||
|
@ -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?;
|
||||
@ -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
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user