More tests
This commit is contained in:
parent
f66834f6e6
commit
d11998bf9e
@ -1,7 +1,7 @@
|
|||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
||||||
pub use common::{bakery_chain::*, setup::*, TestContext};
|
pub use common::{bakery_chain::*, setup::*, TestContext};
|
||||||
pub use sea_orm::{entity::*, query::*, DbErr, FromQueryResult};
|
pub use sea_orm::{entity::*, query::*, DbConn, DbErr, FromQueryResult};
|
||||||
|
|
||||||
#[sea_orm_macros::test]
|
#[sea_orm_macros::test]
|
||||||
#[cfg(any(
|
#[cfg(any(
|
||||||
@ -13,28 +13,9 @@ async fn loader_load_one() -> Result<(), DbErr> {
|
|||||||
let ctx = TestContext::new("loader_test_load_one").await;
|
let ctx = TestContext::new("loader_test_load_one").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
let bakery = bakery::ActiveModel {
|
let bakery = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
name: Set("SeaSide Bakery".to_owned()),
|
|
||||||
profit_margin: Set(10.4),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert bakery");
|
|
||||||
|
|
||||||
let baker_1 = baker::ActiveModel {
|
let baker_1 = insert_baker(&ctx.db, "Baker 1", bakery.id).await?;
|
||||||
name: Set("Baker 1".to_owned()),
|
|
||||||
contact_details: Set(serde_json::json!({
|
|
||||||
"mobile": "+61424000000",
|
|
||||||
"home": "0395555555",
|
|
||||||
"address": "12 Test St, Testville, Vic, Australia"
|
|
||||||
})),
|
|
||||||
bakery_id: Set(Some(bakery.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let baker_2 = baker::ActiveModel {
|
let baker_2 = baker::ActiveModel {
|
||||||
name: Set("Baker 2".to_owned()),
|
name: Set("Baker 2".to_owned()),
|
||||||
@ -73,38 +54,10 @@ async fn loader_load_one_complex() -> Result<(), DbErr> {
|
|||||||
let ctx = TestContext::new("loader_test_load_one").await;
|
let ctx = TestContext::new("loader_test_load_one").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
let bakery = bakery::ActiveModel {
|
let bakery = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
name: Set("SeaSide Bakery".to_owned()),
|
|
||||||
profit_margin: Set(10.4),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert bakery");
|
|
||||||
|
|
||||||
let baker_1 = baker::ActiveModel {
|
let baker_1 = insert_baker(&ctx.db, "Baker 1", bakery.id).await?;
|
||||||
name: Set("Baker 1".to_owned()),
|
let baker_2 = insert_baker(&ctx.db, "Baker 2", bakery.id).await?;
|
||||||
contact_details: Set(serde_json::json!({
|
|
||||||
"mobile": "+61424000000",
|
|
||||||
"home": "0395555555",
|
|
||||||
"address": "12 Test St, Testville, Vic, Australia"
|
|
||||||
})),
|
|
||||||
bakery_id: Set(Some(bakery.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let baker_2 = baker::ActiveModel {
|
|
||||||
name: Set("Baker 2".to_owned()),
|
|
||||||
contact_details: Set(serde_json::json!({})),
|
|
||||||
bakery_id: Set(Some(bakery.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let bakers = baker::Entity::find()
|
let bakers = baker::Entity::find()
|
||||||
.all(&ctx.db)
|
.all(&ctx.db)
|
||||||
@ -133,67 +86,14 @@ async fn loader_load_many() -> Result<(), DbErr> {
|
|||||||
let ctx = TestContext::new("loader_test_load_many").await;
|
let ctx = TestContext::new("loader_test_load_many").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
let bakery_1 = bakery::ActiveModel {
|
let bakery_1 = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
name: Set("SeaSide Bakery".to_owned()),
|
let bakery_2 = insert_bakery(&ctx.db, "Offshore Bakery").await?;
|
||||||
profit_margin: Set(10.4),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert bakery");
|
|
||||||
|
|
||||||
let bakery_2 = bakery::ActiveModel {
|
let baker_1 = insert_baker(&ctx.db, "Baker 1", bakery_1.id).await?;
|
||||||
name: Set("Offshore Bakery".to_owned()),
|
let baker_2 = insert_baker(&ctx.db, "Baker 2", bakery_1.id).await?;
|
||||||
profit_margin: Set(10.4),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert bakery");
|
|
||||||
|
|
||||||
let baker_1 = baker::ActiveModel {
|
let baker_3 = insert_baker(&ctx.db, "John", bakery_2.id).await?;
|
||||||
name: Set("Baker 1".to_owned()),
|
let baker_4 = insert_baker(&ctx.db, "Baker 4", bakery_2.id).await?;
|
||||||
contact_details: Set(serde_json::json!({
|
|
||||||
"mobile": "+61424000000",
|
|
||||||
"home": "0395555555",
|
|
||||||
"address": "12 Test St, Testville, Vic, Australia"
|
|
||||||
})),
|
|
||||||
bakery_id: Set(Some(bakery_1.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let baker_2 = baker::ActiveModel {
|
|
||||||
name: Set("Baker 2".to_owned()),
|
|
||||||
contact_details: Set(serde_json::json!({})),
|
|
||||||
bakery_id: Set(Some(bakery_1.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let baker_3 = baker::ActiveModel {
|
|
||||||
name: Set("John".to_owned()),
|
|
||||||
contact_details: Set(serde_json::json!({})),
|
|
||||||
bakery_id: Set(Some(bakery_2.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let baker_4 = baker::ActiveModel {
|
|
||||||
name: Set("Baker 4".to_owned()),
|
|
||||||
contact_details: Set(serde_json::json!({})),
|
|
||||||
bakery_id: Set(Some(bakery_2.id)),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.insert(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let bakeries = bakery::Entity::find()
|
let bakeries = bakery::Entity::find()
|
||||||
.all(&ctx.db)
|
.all(&ctx.db)
|
||||||
@ -230,3 +130,72 @@ async fn loader_load_many() -> Result<(), DbErr> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[sea_orm_macros::test]
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "sqlx-mysql",
|
||||||
|
feature = "sqlx-sqlite",
|
||||||
|
feature = "sqlx-postgres"
|
||||||
|
))]
|
||||||
|
async fn loader_load_many_many() -> Result<(), DbErr> {
|
||||||
|
let ctx = TestContext::new("loader_test_load_many_many").await;
|
||||||
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
|
let bakery_1 = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
|
let bakery_2 = insert_bakery(&ctx.db, "Offshore Bakery").await?;
|
||||||
|
|
||||||
|
let baker_1 = insert_baker(&ctx.db, "John", bakery_1.id).await?;
|
||||||
|
let baker_2 = insert_baker(&ctx.db, "Jane", bakery_1.id).await?;
|
||||||
|
let baker_3 = insert_baker(&ctx.db, "Peter", bakery_2.id).await?;
|
||||||
|
|
||||||
|
let cake_1 = insert_cake(&ctx.db, "Cheesecake", bakery_1.id).await?;
|
||||||
|
let cake_2 = insert_cake(&ctx.db, "Chocolate", bakery_2.id).await?;
|
||||||
|
let cake_3 = insert_cake(&ctx.db, "Chiffon", bakery_2.id).await?;
|
||||||
|
|
||||||
|
let bakeries = bakery::Entity::find().all(&ctx.db).await?;
|
||||||
|
let bakers = bakeries.load_many(baker::Entity::find(), &ctx.db).await?;
|
||||||
|
let cakes = bakeries.load_many(cake::Entity::find(), &ctx.db).await?;
|
||||||
|
|
||||||
|
println!("{bakers:?}");
|
||||||
|
println!("{bakeries:?}");
|
||||||
|
println!("{cakes:?}");
|
||||||
|
|
||||||
|
assert_eq!(bakeries, [bakery_1, bakery_2]);
|
||||||
|
assert_eq!(bakers, [vec![baker_1, baker_2], vec![baker_3]]);
|
||||||
|
assert_eq!(cakes, [vec![cake_1], vec![cake_2, cake_3]]);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn insert_bakery(db: &DbConn, name: &str) -> Result<bakery::Model, DbErr> {
|
||||||
|
bakery::ActiveModel {
|
||||||
|
name: Set(name.to_owned()),
|
||||||
|
profit_margin: Set(1.0),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.insert(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn insert_baker(db: &DbConn, name: &str, bakery_id: i32) -> Result<baker::Model, DbErr> {
|
||||||
|
baker::ActiveModel {
|
||||||
|
name: Set(name.to_owned()),
|
||||||
|
contact_details: Set(serde_json::json!({})),
|
||||||
|
bakery_id: Set(Some(bakery_id)),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.insert(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn insert_cake(db: &DbConn, name: &str, bakery_id: i32) -> Result<cake::Model, DbErr> {
|
||||||
|
cake::ActiveModel {
|
||||||
|
name: Set(name.to_owned()),
|
||||||
|
price: Set(rust_decimal::Decimal::ONE),
|
||||||
|
gluten_free: Set(false),
|
||||||
|
bakery_id: Set(Some(bakery_id)),
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
.insert(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user