Skip to content

Commit

Permalink
WIP strange fail with postgres driver
Browse files Browse the repository at this point in the history
  • Loading branch information
samsamai committed Aug 1, 2021
1 parent 1c3e1b4 commit abed934
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 98 deletions.
2 changes: 1 addition & 1 deletion src/database/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl std::fmt::Debug for DatabaseConnection {
#[cfg(feature = "sqlx-mysql")]
Self::SqlxMySqlPoolConnection(_) => "SqlxMySqlPoolConnection",
#[cfg(feature = "sqlx-postgres")]
Self::SqlxPostgresPoolConnection(_) => "SqlxMySqlPoolConnection",
Self::SqlxPostgresPoolConnection(_) => "SqlxPostgresPoolConnection",
#[cfg(feature = "sqlx-sqlite")]
Self::SqlxSqlitePoolConnection(_) => "SqlxSqlitePoolConnection",
#[cfg(feature = "mock")]
Expand Down
10 changes: 5 additions & 5 deletions tests/bakery_chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ async fn create_entities(db: &DatabaseConnection) {
crud::create_lineitem::test_create_lineitem(db).await;
crud::create_order::test_create_order(db).await;

crud::updates::test_update_cake(db).await;
crud::updates::test_update_bakery(db).await;
crud::updates::test_update_deleted_customer(db).await;
// crud::updates::test_update_cake(db).await;
// crud::updates::test_update_bakery(db).await;
// crud::updates::test_update_deleted_customer(db).await;

crud::deletes::test_delete_cake(db).await;
crud::deletes::test_delete_bakery(db).await;
// crud::deletes::test_delete_cake(db).await;
// crud::deletes::test_delete_bakery(db).await;
}
6 changes: 3 additions & 3 deletions tests/common/bakery_chain/lineitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pub struct Model {
pub id: i32,
pub price: Decimal,
pub quantity: i32,
pub order_id: Option<i32>,
pub cake_id: Option<i32>,
pub order_id: i32,
pub cake_id: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
Expand Down Expand Up @@ -51,7 +51,7 @@ impl ColumnTrait for Column {
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
Self::Price => ColumnType::Money(Some((19, 4))).def(),
Self::Price => ColumnType::Decimal(Some((19, 4))).def(),
Self::Quantity => ColumnType::Integer.def(),
Self::OrderId => ColumnType::Integer.def(),
Self::CakeId => ColumnType::Integer.def(),
Expand Down
4 changes: 2 additions & 2 deletions tests/common/bakery_chain/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ impl EntityName for Entity {
pub struct Model {
pub id: i32,
pub total: Decimal,
pub bakery_id: Option<i32>,
pub customer_id: Option<i32>,
pub bakery_id: i32,
pub customer_id: i32,
pub placed_at: NaiveDateTime,
}

Expand Down
103 changes: 77 additions & 26 deletions tests/common/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,57 @@ pub mod schema;
pub use schema::*;

pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
let url = format!("{}/mysql", base_url);
let db = Database::connect(&url).await.unwrap();
let _drop_db_result = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("DROP DATABASE IF EXISTS `{}`;", db_name),
))
.await;

let _create_db_result = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("CREATE DATABASE `{}`;", db_name),
))
.await;

let url = format!("{}/{}", base_url, db_name);
let db = Database::connect(&url).await.unwrap();
let db = if cfg!(feature = "sqlx-mysql") {
println!("sqlx-mysql");

let url = format!("{}/mysql", base_url);
let db = Database::connect(&url).await.unwrap();
let _drop_db_result = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("DROP DATABASE IF EXISTS `{}`;", db_name),
))
.await;

let _create_db_result = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("CREATE DATABASE `{}`;", db_name),
))
.await;

let url = format!("{}/{}", base_url, db_name);
Database::connect(&url).await.unwrap()
} else if cfg!(feature = "sqlx-postgres") {
println!("sqlx-postgres");

let url = format!("{}/postgres", base_url);
println!("url: {:#?}", url);
let db = Database::connect(&url).await.unwrap();
println!("db: {:#?}", db);
let _drop_db_result = db
.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("DROP DATABASE IF EXISTS \"{}\";", db_name),
))
.await;

let _create_db_result = db
.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("CREATE DATABASE \"{}\";", db_name),
))
.await;

let url = format!("{}/{}", base_url, db_name);
println!("url: {:#?}", url);

Database::connect(&url).await.unwrap()
} else {
println!("sqlx-sqlite");

Database::connect("sqlite::memory:").await.unwrap()
};

assert!(schema::create_bakery_table(&db).await.is_ok());
assert!(schema::create_baker_table(&db).await.is_ok());
Expand All @@ -33,12 +66,30 @@ pub async fn setup(base_url: &str, db_name: &str) -> DatabaseConnection {
}

pub async fn tear_down(base_url: &str, db_name: &str) {
let url = format!("{}/mysql", base_url);
let db = Database::connect(&url).await.unwrap();
let _drop_db_result = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("DROP DATABASE IF EXISTS `{}`;", db_name),
))
.await;
if cfg!(feature = "sqlx-mysql") {
println!("sqlx-mysql");

let url = format!("{}/mysql", base_url);
let db = Database::connect(&url).await.unwrap();
let _ = db
.execute(Statement::from_string(
DatabaseBackend::MySql,
format!("DROP DATABASE IF EXISTS \"{}\";", db_name),
))
.await;
} else if cfg!(feature = "sqlx-postgres") {
println!("sqlx-postgres");

let url = format!("{}/postgres", base_url);
println!("url: {:#?}", url);
let db = Database::connect(&url).await.unwrap();
let _ = db
.execute(Statement::from_string(
DatabaseBackend::Postgres,
format!("DROP DATABASE IF EXISTS \"{}\";", db_name),
))
.await;
} else {
println!("sqlx-sqlite");
};
}
13 changes: 7 additions & 6 deletions tests/crud/create_lineitem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub async fn test_create_lineitem(db: &DbConn) {

// Order
let order_1 = order::ActiveModel {
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
customer_id: Set(Some(customer_insert_res.last_insert_id as i32)),
bakery_id: Set(bakery_insert_res.last_insert_id as i32),
customer_id: Set(customer_insert_res.last_insert_id as i32),
total: Set(dec!(7.55)),
placed_at: Set(Utc::now().naive_utc()),
..Default::default()
Expand All @@ -83,8 +83,8 @@ pub async fn test_create_lineitem(db: &DbConn) {

// Lineitem
let lineitem_1 = lineitem::ActiveModel {
cake_id: Set(Some(cake_insert_res.last_insert_id as i32)),
order_id: Set(Some(order_insert_res.last_insert_id as i32)),
cake_id: Set(cake_insert_res.last_insert_id as i32),
order_id: Set(order_insert_res.last_insert_id as i32),
price: Set(dec!(7.55)),
quantity: Set(1),
..Default::default()
Expand All @@ -102,9 +102,10 @@ pub async fn test_create_lineitem(db: &DbConn) {

assert!(lineitem.is_some());
let lineitem_model = lineitem.unwrap();

assert_eq!(lineitem_model.price, dec!(7.55));

let cake: Option<cake::Model> = Cake::find_by_id(lineitem_model.cake_id)
let cake: Option<cake::Model> = Cake::find_by_id(lineitem_model.cake_id as u64)
.one(db)
.await
.expect("could not find cake");
Expand All @@ -119,7 +120,7 @@ pub async fn test_create_lineitem(db: &DbConn) {

let order_model = order.unwrap();
assert_eq!(
order_model.customer_id.unwrap(),
order_model.customer_id,
customer_insert_res.last_insert_id as i32
);
}
67 changes: 40 additions & 27 deletions tests/crud/create_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ pub async fn test_create_order(db: &DbConn) {

// Order
let order_1 = order::ActiveModel {
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
customer_id: Set(Some(customer_insert_res.last_insert_id as i32)),
bakery_id: Set(bakery_insert_res.last_insert_id as i32),
customer_id: Set(customer_insert_res.last_insert_id as i32),
total: Set(dec!(15.10)),
placed_at: Set(Utc::now().naive_utc()),
..Default::default()
Expand All @@ -81,10 +81,12 @@ pub async fn test_create_order(db: &DbConn) {
.await
.expect("could not insert order");

println!("order_insert_res: {:#?}", order_insert_res);

// Lineitem
let lineitem_1 = lineitem::ActiveModel {
cake_id: Set(Some(cake_insert_res.last_insert_id as i32)),
order_id: Set(Some(order_insert_res.last_insert_id as i32)),
cake_id: Set(cake_insert_res.last_insert_id as i32),
order_id: Set(order_insert_res.last_insert_id as i32),
price: Set(dec!(7.55)),
quantity: Set(2),
..Default::default()
Expand All @@ -94,37 +96,48 @@ pub async fn test_create_order(db: &DbConn) {
.await
.expect("could not insert lineitem");

// This fails with "error returned from database: incorrect binary data format in bind parameter 1"
let order: Option<order::Model> = Order::find_by_id(order_insert_res.last_insert_id)
.one(db)
.await
.expect("could not find order");

assert!(order.is_some());
let order_model = order.unwrap();
assert_eq!(order_model.total, dec!(15.10));

let customer: Option<customer::Model> = Customer::find_by_id(order_model.customer_id)
.one(db)
// this is ok
let orders: Vec<order::Model> = Order::find_by_id(order_insert_res.last_insert_id)
.all(db)
.await
.expect("could not find customer");
.unwrap();
println!("order: {:#?}", orders);
let order: order::Model = orders[0].clone();
println!("order: {:#?}", order);

let customer_model = customer.unwrap();
assert_eq!(customer_model.name, "Kate");
assert_eq!(1, 2);
// assert!(order.is_some());
// let order_model = order.unwrap();
// assert_eq!(order_model.total, dec!(15.10));

let bakery: Option<bakery::Model> = Bakery::find_by_id(order_model.bakery_id)
.one(db)
.await
.expect("could not find bakery");
// let customer: Option<customer::Model> = Customer::find_by_id(order_model.customer_id)
// .one(db)
// .await
// .expect("could not find customer");

let bakery_model = bakery.unwrap();
assert_eq!(bakery_model.name, "SeaSide Bakery");
// let customer_model = customer.unwrap();
// assert_eq!(customer_model.name, "Kate");

let related_lineitems: Vec<lineitem::Model> = order_model
.find_related(Lineitem)
.all(db)
.await
.expect("could not find related lineitems");
assert_eq!(related_lineitems.len(), 1);
assert_eq!(related_lineitems[0].price, dec!(7.55));
assert_eq!(related_lineitems[0].quantity, 2);
// let bakery: Option<bakery::Model> = Bakery::find_by_id(order_model.bakery_id)
// .one(db)
// .await
// .expect("could not find bakery");

// let bakery_model = bakery.unwrap();
// assert_eq!(bakery_model.name, "SeaSide Bakery");

// let related_lineitems: Vec<lineitem::Model> = order_model
// .find_related(Lineitem)
// .all(db)
// .await
// .expect("could not find related lineitems");
// assert_eq!(related_lineitems.len(), 1);
// assert_eq!(related_lineitems[0].price, dec!(7.55));
// assert_eq!(related_lineitems[0].quantity, 2);
}
1 change: 1 addition & 0 deletions tests/pg_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use sea_query::{ColumnDef, TableCreateStatement};
#[cfg_attr(feature = "runtime-async-std", async_std::test)]
#[cfg_attr(feature = "runtime-actix", actix_rt::test)]
#[cfg_attr(feature = "runtime-tokio", tokio::test)]
#[async_std::test]
#[cfg(feature = "sqlx-postgres")]
async fn main() {
let base_url = "postgres://root:root@localhost";
Expand Down
Loading

0 comments on commit abed934

Please sign in to comment.