Testing & fixup

This commit is contained in:
Billy Chan 2021-11-16 12:04:21 +08:00
parent 31fae07bc1
commit 36886e74cd
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
3 changed files with 96 additions and 5 deletions

View File

@ -170,11 +170,11 @@ where
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{sea_query::*, tests_cfg::*, DbBackend, Schema}; use crate::{sea_query::*, tests_cfg::*, DbBackend, EntityName, Schema};
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
#[test] #[test]
fn test_create_table_from_entity() { fn test_mysql_create_table_from_entity() {
let schema = Schema::new(DbBackend::MySql); let schema = Schema::new(DbBackend::MySql);
assert_eq!( assert_eq!(
schema schema
@ -217,4 +217,94 @@ mod tests {
.to_string(MysqlQueryBuilder) .to_string(MysqlQueryBuilder)
); );
} }
#[test]
fn test_postgres_create_table_from_entity() {
let schema = Schema::new(DbBackend::Postgres);
assert_eq!(
schema
.create_table_from_entity(CakeFillingPrice)
.to_string(PostgresQueryBuilder),
Table::create()
.table(CakeFillingPrice.table_ref())
.col(
ColumnDef::new(cake_filling_price::Column::CakeId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::FillingId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::Price)
.decimal()
.not_null()
)
.primary_key(
Index::create()
.name("pk-cake_filling_price")
.col(cake_filling_price::Column::CakeId)
.col(cake_filling_price::Column::FillingId)
.primary()
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-cake_filling_price-cake_filling")
.from_tbl(CakeFillingPrice)
.from_col(cake_filling_price::Column::CakeId)
.from_col(cake_filling_price::Column::FillingId)
.to_tbl(CakeFilling)
.to_col(cake_filling::Column::CakeId)
.to_col(cake_filling::Column::FillingId)
)
.to_string(PostgresQueryBuilder)
);
}
#[test]
fn test_sqlite_create_table_from_entity() {
let schema = Schema::new(DbBackend::Sqlite);
assert_eq!(
schema
.create_table_from_entity(CakeFillingPrice)
.to_string(SqliteQueryBuilder),
Table::create()
.table(CakeFillingPrice)
.col(
ColumnDef::new(cake_filling_price::Column::CakeId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::FillingId)
.integer()
.not_null()
)
.col(
ColumnDef::new(cake_filling_price::Column::Price)
.decimal()
.not_null()
)
.primary_key(
Index::create()
.name("pk-cake_filling_price")
.col(cake_filling_price::Column::CakeId)
.col(cake_filling_price::Column::FillingId)
.primary()
)
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-cake_filling_price-cake_filling")
.from_tbl(CakeFillingPrice)
.from_col(cake_filling_price::Column::CakeId)
.from_col(cake_filling_price::Column::FillingId)
.to_tbl(CakeFilling)
.to_col(cake_filling::Column::CakeId)
.to_col(cake_filling::Column::FillingId)
)
.to_string(SqliteQueryBuilder)
);
}
} }

View File

@ -1,7 +1,7 @@
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "active_enum")] #[sea_orm(schema_name = "public", table_name = "active_enum")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
pub id: i32, pub id: i32,

View File

@ -3,7 +3,8 @@ pub use super::super::bakery_chain::*;
use super::*; 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, ExecResult, error::*, sea_query, ConnectionTrait, DatabaseConnection, DbBackend, DbConn, EntityName,
ExecResult,
}; };
use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement}; use sea_query::{extension::postgres::Type, Alias, ColumnDef, ForeignKeyCreateStatement};
@ -146,7 +147,7 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result<ExecResult, DbErr>
DbBackend::Postgres => tea_col.custom(tea_enum), DbBackend::Postgres => tea_col.custom(tea_enum),
}; };
let create_table_stmt = sea_query::Table::create() let create_table_stmt = sea_query::Table::create()
.table(active_enum::Entity) .table(active_enum::Entity.table_ref())
.col( .col(
ColumnDef::new(active_enum::Column::Id) ColumnDef::new(active_enum::Column::Id)
.integer() .integer()