Refactor test case

This commit is contained in:
Billy Chan 2021-11-18 18:07:09 +08:00
parent b2985a97c8
commit f72f65a4d4
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7

View File

@ -174,137 +174,61 @@ mod tests {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
#[test] #[test]
fn test_mysql_create_table_from_entity() { fn test_create_table_from_entity_table_ref() {
let schema = Schema::new(DbBackend::MySql); for builder in [DbBackend::MySql, DbBackend::Postgres, DbBackend::Sqlite] {
assert_eq!( let schema = Schema::new(builder);
schema assert_eq!(
.create_table_from_entity(CakeFillingPrice) builder.build(&schema.create_table_from_entity(CakeFillingPrice)),
.to_string(MysqlQueryBuilder), builder.build(&get_stmt().table(CakeFillingPrice.table_ref()).to_owned())
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(MysqlQueryBuilder)
);
} }
#[test] #[test]
fn test_postgres_create_table_from_entity() { fn test_create_table_from_entity() {
let schema = Schema::new(DbBackend::Postgres); for builder in [DbBackend::MySql, DbBackend::Sqlite] {
assert_eq!( let schema = Schema::new(builder);
schema assert_eq!(
.create_table_from_entity(CakeFillingPrice) builder.build(&schema.create_table_from_entity(CakeFillingPrice)),
.to_string(PostgresQueryBuilder), builder.build(&get_stmt().table(CakeFillingPrice).to_owned())
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 get_stmt() -> TableCreateStatement {
fn test_sqlite_create_table_from_entity() { Table::create()
let schema = Schema::new(DbBackend::Sqlite); .col(
assert_eq!( ColumnDef::new(cake_filling_price::Column::CakeId)
schema .integer()
.create_table_from_entity(CakeFillingPrice) .not_null(),
.to_string(SqliteQueryBuilder), )
Table::create() .col(
.table(CakeFillingPrice) ColumnDef::new(cake_filling_price::Column::FillingId)
.col( .integer()
ColumnDef::new(cake_filling_price::Column::CakeId) .not_null(),
.integer() )
.not_null() .col(
) ColumnDef::new(cake_filling_price::Column::Price)
.col( .decimal()
ColumnDef::new(cake_filling_price::Column::FillingId) .not_null(),
.integer() )
.not_null() .primary_key(
) Index::create()
.col( .name("pk-cake_filling_price")
ColumnDef::new(cake_filling_price::Column::Price) .col(cake_filling_price::Column::CakeId)
.decimal() .col(cake_filling_price::Column::FillingId)
.not_null() .primary(),
) )
.primary_key( .foreign_key(
Index::create() ForeignKeyCreateStatement::new()
.name("pk-cake_filling_price") .name("fk-cake_filling_price-cake_filling")
.col(cake_filling_price::Column::CakeId) .from_tbl(CakeFillingPrice)
.col(cake_filling_price::Column::FillingId) .from_col(cake_filling_price::Column::CakeId)
.primary() .from_col(cake_filling_price::Column::FillingId)
) .to_tbl(CakeFilling)
.foreign_key( .to_col(cake_filling::Column::CakeId)
ForeignKeyCreateStatement::new() .to_col(cake_filling::Column::FillingId),
.name("fk-cake_filling_price-cake_filling") )
.from_tbl(CakeFillingPrice) .to_owned()
.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)
);
} }
} }