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,125 +174,50 @@ 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] {
let schema = Schema::new(builder);
assert_eq!( assert_eq!(
schema builder.build(&schema.create_table_from_entity(CakeFillingPrice)),
.create_table_from_entity(CakeFillingPrice) builder.build(&get_stmt().table(CakeFillingPrice.table_ref()).to_owned())
.to_string(MysqlQueryBuilder),
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] {
let schema = Schema::new(builder);
assert_eq!( assert_eq!(
schema builder.build(&schema.create_table_from_entity(CakeFillingPrice)),
.create_table_from_entity(CakeFillingPrice) builder.build(&get_stmt().table(CakeFillingPrice).to_owned())
.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 get_stmt() -> TableCreateStatement {
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::create()
.table(CakeFillingPrice)
.col( .col(
ColumnDef::new(cake_filling_price::Column::CakeId) ColumnDef::new(cake_filling_price::Column::CakeId)
.integer() .integer()
.not_null() .not_null(),
) )
.col( .col(
ColumnDef::new(cake_filling_price::Column::FillingId) ColumnDef::new(cake_filling_price::Column::FillingId)
.integer() .integer()
.not_null() .not_null(),
) )
.col( .col(
ColumnDef::new(cake_filling_price::Column::Price) ColumnDef::new(cake_filling_price::Column::Price)
.decimal() .decimal()
.not_null() .not_null(),
) )
.primary_key( .primary_key(
Index::create() Index::create()
.name("pk-cake_filling_price") .name("pk-cake_filling_price")
.col(cake_filling_price::Column::CakeId) .col(cake_filling_price::Column::CakeId)
.col(cake_filling_price::Column::FillingId) .col(cake_filling_price::Column::FillingId)
.primary() .primary(),
) )
.foreign_key( .foreign_key(
ForeignKeyCreateStatement::new() ForeignKeyCreateStatement::new()
@ -302,9 +227,8 @@ mod tests {
.from_col(cake_filling_price::Column::FillingId) .from_col(cake_filling_price::Column::FillingId)
.to_tbl(CakeFilling) .to_tbl(CakeFilling)
.to_col(cake_filling::Column::CakeId) .to_col(cake_filling::Column::CakeId)
.to_col(cake_filling::Column::FillingId) .to_col(cake_filling::Column::FillingId),
) )
.to_string(SqliteQueryBuilder) .to_owned()
);
} }
} }