test(Schema): add test cases for Schema::create_enum_from_entity
& Schema::create_enum_from_active_enum
This commit is contained in:
parent
09fd9ba725
commit
96a776ae9d
@ -686,4 +686,32 @@ mod tests {
|
|||||||
.join(" ")
|
.join(" ")
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn create_enum_from() {
|
||||||
|
use sea_orm::{Schema, Statement};
|
||||||
|
|
||||||
|
let db_postgres = DbBackend::Postgres;
|
||||||
|
let schema = Schema::new(db_postgres);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
schema
|
||||||
|
.create_enum_from_entity(active_enum::Entity)
|
||||||
|
.iter()
|
||||||
|
.map(|stmt| db_postgres.build(stmt))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
vec![Statement::from_string(
|
||||||
|
db_postgres,
|
||||||
|
r#"CREATE TYPE "tea" AS ENUM ('EverydayTea', 'BreakfastTea')"#.to_owned()
|
||||||
|
),]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
db_postgres.build(&schema.create_enum_from_active_enum::<Tea>()),
|
||||||
|
Statement::from_string(
|
||||||
|
db_postgres,
|
||||||
|
r#"CREATE TYPE "tea" AS ENUM ('EverydayTea', 'BreakfastTea')"#.to_owned()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ use super::*;
|
|||||||
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,
|
ExecResult, Schema,
|
||||||
};
|
};
|
||||||
use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement};
|
use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement};
|
||||||
|
|
||||||
@ -19,10 +19,18 @@ pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
|||||||
|
|
||||||
let create_enum_stmts = match db_backend {
|
let create_enum_stmts = match db_backend {
|
||||||
DbBackend::MySql | DbBackend::Sqlite => Vec::new(),
|
DbBackend::MySql | DbBackend::Sqlite => Vec::new(),
|
||||||
DbBackend::Postgres => vec![Type::create()
|
DbBackend::Postgres => {
|
||||||
|
let schema = Schema::new(db_backend);
|
||||||
|
let enum_create_stmt = Type::create()
|
||||||
.as_enum(Alias::new("tea"))
|
.as_enum(Alias::new("tea"))
|
||||||
.values(vec![Alias::new("EverydayTea"), Alias::new("BreakfastTea")])
|
.values(vec![Alias::new("EverydayTea"), Alias::new("BreakfastTea")])
|
||||||
.to_owned()],
|
.to_owned();
|
||||||
|
assert_eq!(
|
||||||
|
db_backend.build(&enum_create_stmt),
|
||||||
|
db_backend.build(&schema.create_enum_from_active_enum::<Tea>())
|
||||||
|
);
|
||||||
|
vec![enum_create_stmt]
|
||||||
|
}
|
||||||
};
|
};
|
||||||
create_enum(db, &create_enum_stmts, ActiveEnum).await?;
|
create_enum(db, &create_enum_stmts, ActiveEnum).await?;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user