From 36886e74cd1dd80024fb9f14c97ed38de87ced6f Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 12:04:21 +0800 Subject: [PATCH] Testing & fixup --- src/schema/entity.rs | 94 +++++++++++++++++++++++++++- tests/common/features/active_enum.rs | 2 +- tests/common/features/schema.rs | 5 +- 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/src/schema/entity.rs b/src/schema/entity.rs index 58b168de..a78625cc 100644 --- a/src/schema/entity.rs +++ b/src/schema/entity.rs @@ -170,11 +170,11 @@ where #[cfg(test)] mod tests { - use crate::{sea_query::*, tests_cfg::*, DbBackend, Schema}; + use crate::{sea_query::*, tests_cfg::*, DbBackend, EntityName, Schema}; use pretty_assertions::assert_eq; #[test] - fn test_create_table_from_entity() { + fn test_mysql_create_table_from_entity() { let schema = Schema::new(DbBackend::MySql); assert_eq!( schema @@ -217,4 +217,94 @@ mod tests { .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) + ); + } } diff --git a/tests/common/features/active_enum.rs b/tests/common/features/active_enum.rs index 5285c5d9..9df91b45 100644 --- a/tests/common/features/active_enum.rs +++ b/tests/common/features/active_enum.rs @@ -1,7 +1,7 @@ use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel)] -#[sea_orm(table_name = "active_enum")] +#[sea_orm(schema_name = "public", table_name = "active_enum")] pub struct Model { #[sea_orm(primary_key)] pub id: i32, diff --git a/tests/common/features/schema.rs b/tests/common/features/schema.rs index 94277604..1504b285 100644 --- a/tests/common/features/schema.rs +++ b/tests/common/features/schema.rs @@ -3,7 +3,8 @@ pub use super::super::bakery_chain::*; use super::*; use crate::common::setup::{create_enum, create_table, create_table_without_asserts}; 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}; @@ -146,7 +147,7 @@ pub async fn create_active_enum_table(db: &DbConn) -> Result DbBackend::Postgres => tea_col.custom(tea_enum), }; let create_table_stmt = sea_query::Table::create() - .table(active_enum::Entity) + .table(active_enum::Entity.table_ref()) .col( ColumnDef::new(active_enum::Column::Id) .integer()