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> {
|
pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
active_enum::ActiveModel {
|
use active_enum::*;
|
||||||
category: Set(active_enum::Category::Big),
|
|
||||||
|
let am = ActiveModel {
|
||||||
|
category: Set(None),
|
||||||
|
color: Set(None),
|
||||||
|
// tea: Set(None),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.insert(db)
|
.insert(db)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
active_enum::Entity::find().one(db).await?.unwrap(),
|
Entity::find().one(db).await?.unwrap(),
|
||||||
active_enum::Model {
|
Model {
|
||||||
id: 1,
|
id: 1,
|
||||||
category: active_enum::Category::Big,
|
category: None,
|
||||||
category_opt: 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 {
|
pub struct Model {
|
||||||
#[sea_orm(primary_key)]
|
#[sea_orm(primary_key)]
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub category: Category,
|
pub category: Option<Category>,
|
||||||
pub category_opt: Option<Category>,
|
pub color: Option<Color>,
|
||||||
|
// pub tea: Option<Tea>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||||
@ -22,3 +23,21 @@ pub enum Category {
|
|||||||
#[sea_orm(string_value = "S")]
|
#[sea_orm(string_value = "S")]
|
||||||
Small,
|
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 super::*;
|
||||||
use crate::common::setup::create_table;
|
use crate::common::setup::create_table;
|
||||||
use sea_orm::{error::*, sea_query, DatabaseConnection, DbConn, ExecResult};
|
use sea_orm::{ConnectionTrait, DatabaseConnection, DbConn, ExecResult, Statement, error::*, sea_query};
|
||||||
use sea_query::ColumnDef;
|
use sea_query::{Alias, ColumnDef};
|
||||||
|
|
||||||
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||||
create_log_table(db).await?;
|
create_log_table(db).await?;
|
||||||
@ -87,12 +87,9 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
|
|||||||
.auto_increment()
|
.auto_increment()
|
||||||
.primary_key(),
|
.primary_key(),
|
||||||
)
|
)
|
||||||
.col(
|
.col(ColumnDef::new(active_enum::Column::Category).string_len(1))
|
||||||
ColumnDef::new(active_enum::Column::Category)
|
.col(ColumnDef::new(active_enum::Column::Color).integer())
|
||||||
.string_len(1)
|
// .col(ColumnDef::new(active_enum::Column::Tea).custom(Alias::new("tea")))
|
||||||
.not_null(),
|
|
||||||
)
|
|
||||||
.col(ColumnDef::new(active_enum::Column::CategoryOpt).string_len(1))
|
|
||||||
.to_owned();
|
.to_owned();
|
||||||
|
|
||||||
create_table(db, &stmt, ActiveEnum).await
|
create_table(db, &stmt, ActiveEnum).await
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
|
use pretty_assertions::assert_eq;
|
||||||
use sea_orm::{
|
use sea_orm::{
|
||||||
ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DbBackend, DbConn, DbErr,
|
ConnectionTrait, Database, DatabaseBackend, DatabaseConnection, DbBackend, DbConn, DbErr,
|
||||||
EntityTrait, ExecResult, Schema, Statement,
|
EntityTrait, ExecResult, Schema, Statement,
|
||||||
};
|
};
|
||||||
|
|
||||||
use sea_query::{Alias, Table, TableCreateStatement};
|
use sea_query::{Alias, Table, TableCreateStatement};
|
||||||
|
|
||||||
pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
|
pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user