Improve test cases
This commit is contained in:
parent
d8a2428f46
commit
0670827148
@ -7,7 +7,7 @@ use sea_query::{
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
/// Defines the type of relationship
|
/// Defines the type of relationship
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum RelationType {
|
pub enum RelationType {
|
||||||
/// An Entity has one relationship
|
/// An Entity has one relationship
|
||||||
HasOne,
|
HasOne,
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::*, ColumnTrait, Condition, ConnectionTrait, DbErr, EntityTrait, Identity, ModelTrait,
|
error::*, Condition, ConnectionTrait, DbErr, EntityTrait, Identity, ModelTrait, QueryFilter,
|
||||||
QueryFilter, Related, RelationType, Select,
|
Related, RelationType, Select,
|
||||||
};
|
};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use sea_query::{Expr, IntoColumnRef, SimpleExpr, ValueTuple};
|
use sea_query::{ColumnRef, DynIden, Expr, IntoColumnRef, SimpleExpr, TableRef, ValueTuple};
|
||||||
use std::{collections::HashMap, str::FromStr};
|
use std::{collections::HashMap, str::FromStr};
|
||||||
|
|
||||||
/// A trait for basic Dataloader
|
/// A trait for basic Dataloader
|
||||||
@ -71,12 +71,13 @@ where
|
|||||||
R::Model: Send + Sync,
|
R::Model: Send + Sync,
|
||||||
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
|
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
|
||||||
{
|
{
|
||||||
|
// we verify that is HasOne relation
|
||||||
|
if <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::via().is_some() {
|
||||||
|
return Err(type_err("Relation is ManytoMany instead of HasOne"));
|
||||||
|
}
|
||||||
let rel_def = <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::to();
|
let rel_def = <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::to();
|
||||||
|
if rel_def.rel_type == RelationType::HasMany {
|
||||||
// we verify that is has_one relation
|
return Err(type_err("Relation is HasMany instead of HasOne"));
|
||||||
match (rel_def).rel_type {
|
|
||||||
RelationType::HasOne => (),
|
|
||||||
RelationType::HasMany => return Err(type_err("Relation is HasMany instead of HasOne")),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let keys: Vec<ValueTuple> = self
|
let keys: Vec<ValueTuple> = self
|
||||||
@ -84,7 +85,7 @@ where
|
|||||||
.map(|model: &M| extract_key(&rel_def.from_col, model))
|
.map(|model: &M| extract_key(&rel_def.from_col, model))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let condition = prepare_condition::<<R as EntityTrait>::Model>(&rel_def.to_col, &keys);
|
let condition = prepare_condition(&rel_def.to_tbl, &rel_def.to_col, &keys);
|
||||||
|
|
||||||
let stmt = <Select<R> as QueryFilter>::filter(stmt, condition);
|
let stmt = <Select<R> as QueryFilter>::filter(stmt, condition);
|
||||||
|
|
||||||
@ -119,12 +120,14 @@ where
|
|||||||
R::Model: Send + Sync,
|
R::Model: Send + Sync,
|
||||||
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
|
<<Self as LoaderTrait>::Model as ModelTrait>::Entity: Related<R>,
|
||||||
{
|
{
|
||||||
let rel_def = <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::to();
|
// we verify that is HasMany relation
|
||||||
|
|
||||||
// we verify that is has_many relation
|
if <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::via().is_some() {
|
||||||
match (rel_def).rel_type {
|
return Err(type_err("Relation is ManyToMany instead of HasMany"));
|
||||||
RelationType::HasMany => (),
|
}
|
||||||
RelationType::HasOne => return Err(type_err("Relation is HasOne instead of HasMany")),
|
let rel_def = <<<Self as LoaderTrait>::Model as ModelTrait>::Entity as Related<R>>::to();
|
||||||
|
if rel_def.rel_type == RelationType::HasOne {
|
||||||
|
return Err(type_err("Relation is HasOne instead of HasMany"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let keys: Vec<ValueTuple> = self
|
let keys: Vec<ValueTuple> = self
|
||||||
@ -132,7 +135,7 @@ where
|
|||||||
.map(|model: &M| extract_key(&rel_def.from_col, model))
|
.map(|model: &M| extract_key(&rel_def.from_col, model))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let condition = prepare_condition::<<R as EntityTrait>::Model>(&rel_def.to_col, &keys);
|
let condition = prepare_condition(&rel_def.to_tbl, &rel_def.to_col, &keys);
|
||||||
|
|
||||||
let stmt = <Select<R> as QueryFilter>::filter(stmt, condition);
|
let stmt = <Select<R> as QueryFilter>::filter(stmt, condition);
|
||||||
|
|
||||||
@ -222,54 +225,35 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepare_condition<M>(col: &Identity, keys: &[ValueTuple]) -> Condition
|
fn prepare_condition(table: &TableRef, col: &Identity, keys: &[ValueTuple]) -> Condition {
|
||||||
where
|
|
||||||
M: ModelTrait,
|
|
||||||
{
|
|
||||||
match col {
|
match col {
|
||||||
Identity::Unary(column_a) => {
|
Identity::Unary(column_a) => {
|
||||||
let column_a: <M::Entity as EntityTrait>::Column =
|
let column_a = table_column(table, column_a);
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_a.to_string())
|
Condition::all().add(Expr::col(column_a).is_in(keys.iter().cloned().flatten()))
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *A:1"));
|
|
||||||
Condition::all().add(ColumnTrait::is_in(
|
|
||||||
&column_a,
|
|
||||||
keys.iter().cloned().flatten(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
Identity::Binary(column_a, column_b) => {
|
|
||||||
let column_a: <M::Entity as EntityTrait>::Column =
|
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_a.to_string())
|
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *A:2"));
|
|
||||||
let column_b: <M::Entity as EntityTrait>::Column =
|
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_b.to_string())
|
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *B:2"));
|
|
||||||
Condition::all().add(
|
|
||||||
Expr::tuple([
|
|
||||||
SimpleExpr::Column(column_a.into_column_ref()),
|
|
||||||
SimpleExpr::Column(column_b.into_column_ref()),
|
|
||||||
])
|
|
||||||
.in_tuples(keys.iter().cloned()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
Identity::Ternary(column_a, column_b, column_c) => {
|
|
||||||
let column_a: <M::Entity as EntityTrait>::Column =
|
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_a.to_string())
|
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *A:3"));
|
|
||||||
let column_b: <M::Entity as EntityTrait>::Column =
|
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_b.to_string())
|
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *B:3"));
|
|
||||||
let column_c: <M::Entity as EntityTrait>::Column =
|
|
||||||
<<M::Entity as EntityTrait>::Column as FromStr>::from_str(&column_c.to_string())
|
|
||||||
.unwrap_or_else(|_| panic!("Failed at mapping string to column *C:3"));
|
|
||||||
Condition::all().add(
|
|
||||||
Expr::tuple([
|
|
||||||
SimpleExpr::Column(column_a.into_column_ref()),
|
|
||||||
SimpleExpr::Column(column_b.into_column_ref()),
|
|
||||||
SimpleExpr::Column(column_c.into_column_ref()),
|
|
||||||
])
|
|
||||||
.in_tuples(keys.iter().cloned()),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
Identity::Binary(column_a, column_b) => Condition::all().add(
|
||||||
|
Expr::tuple([
|
||||||
|
SimpleExpr::Column(table_column(table, column_a)),
|
||||||
|
SimpleExpr::Column(table_column(table, column_b)),
|
||||||
|
])
|
||||||
|
.in_tuples(keys.iter().cloned()),
|
||||||
|
),
|
||||||
|
Identity::Ternary(column_a, column_b, column_c) => Condition::all().add(
|
||||||
|
Expr::tuple([
|
||||||
|
SimpleExpr::Column(table_column(table, column_a)),
|
||||||
|
SimpleExpr::Column(table_column(table, column_b)),
|
||||||
|
SimpleExpr::Column(table_column(table, column_c)),
|
||||||
|
])
|
||||||
|
.in_tuples(keys.iter().cloned()),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_column(tbl: &TableRef, col: &DynIden) -> ColumnRef {
|
||||||
|
match tbl.to_owned() {
|
||||||
|
TableRef::Table(tbl) => (tbl, col.clone()).into_column_ref(),
|
||||||
|
TableRef::SchemaTable(sch, tbl) => (sch, tbl, col.clone()).into_column_ref(),
|
||||||
|
val => unimplemented!("Unsupported TableRef {val:?}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,18 +24,10 @@ async fn loader_load_one() -> Result<(), DbErr> {
|
|||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.insert(&ctx.db)
|
.insert(&ctx.db)
|
||||||
.await
|
.await?;
|
||||||
.expect("could not insert baker");
|
|
||||||
|
|
||||||
let bakers = baker::Entity::find()
|
let bakers = baker::Entity::find().all(&ctx.db).await?;
|
||||||
.all(&ctx.db)
|
let bakeries = bakers.load_one(bakery::Entity::find(), &ctx.db).await?;
|
||||||
.await
|
|
||||||
.expect("Should load bakers");
|
|
||||||
|
|
||||||
let bakeries = bakers
|
|
||||||
.load_one(bakery::Entity::find(), &ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("Should load bakeries");
|
|
||||||
|
|
||||||
assert_eq!(bakers, [baker_1, baker_2]);
|
assert_eq!(bakers, [baker_1, baker_2]);
|
||||||
|
|
||||||
@ -59,15 +51,8 @@ async fn loader_load_one_complex() -> Result<(), DbErr> {
|
|||||||
let baker_1 = insert_baker(&ctx.db, "Baker 1", bakery.id).await?;
|
let baker_1 = insert_baker(&ctx.db, "Baker 1", bakery.id).await?;
|
||||||
let baker_2 = insert_baker(&ctx.db, "Baker 2", bakery.id).await?;
|
let baker_2 = insert_baker(&ctx.db, "Baker 2", bakery.id).await?;
|
||||||
|
|
||||||
let bakers = baker::Entity::find()
|
let bakers = baker::Entity::find().all(&ctx.db).await?;
|
||||||
.all(&ctx.db)
|
let bakeries = bakers.load_one(bakery::Entity::find(), &ctx.db).await?;
|
||||||
.await
|
|
||||||
.expect("Should load bakers");
|
|
||||||
|
|
||||||
let bakeries = bakers
|
|
||||||
.load_one(bakery::Entity::find(), &ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("Should load bakeries");
|
|
||||||
|
|
||||||
assert_eq!(bakers, [baker_1, baker_2]);
|
assert_eq!(bakers, [baker_1, baker_2]);
|
||||||
|
|
||||||
@ -95,23 +80,19 @@ async fn loader_load_many() -> Result<(), DbErr> {
|
|||||||
let baker_3 = insert_baker(&ctx.db, "John", bakery_2.id).await?;
|
let baker_3 = insert_baker(&ctx.db, "John", bakery_2.id).await?;
|
||||||
let baker_4 = insert_baker(&ctx.db, "Baker 4", bakery_2.id).await?;
|
let baker_4 = insert_baker(&ctx.db, "Baker 4", bakery_2.id).await?;
|
||||||
|
|
||||||
let bakeries = bakery::Entity::find()
|
let bakeries = bakery::Entity::find().all(&ctx.db).await?;
|
||||||
.all(&ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("Should load bakeries");
|
|
||||||
|
|
||||||
let bakers = bakeries
|
let bakers = bakeries
|
||||||
.load_many(
|
.load_many(
|
||||||
baker::Entity::find().filter(baker::Column::Name.like("Baker%")),
|
baker::Entity::find().filter(baker::Column::Name.like("Baker%")),
|
||||||
&ctx.db,
|
&ctx.db,
|
||||||
)
|
)
|
||||||
.await
|
.await?;
|
||||||
.expect("Should load bakers");
|
|
||||||
|
|
||||||
println!("A: {bakers:?}");
|
println!("A: {bakers:?}");
|
||||||
println!("B: {bakeries:?}");
|
println!("B: {bakeries:?}");
|
||||||
|
|
||||||
assert_eq!(bakeries, [bakery_1, bakery_2]);
|
assert_eq!(bakeries, [bakery_1.clone(), bakery_2.clone()]);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
bakers,
|
bakers,
|
||||||
@ -121,12 +102,32 @@ async fn loader_load_many() -> Result<(), DbErr> {
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
let bakers = bakeries
|
let bakers = bakeries.load_many(baker::Entity::find(), &ctx.db).await?;
|
||||||
.load_many(baker::Entity::find(), &ctx.db)
|
|
||||||
.await
|
|
||||||
.expect("Should load bakers");
|
|
||||||
|
|
||||||
assert_eq!(bakers, [[baker_1, baker_2], [baker_3, baker_4]]);
|
assert_eq!(
|
||||||
|
bakers,
|
||||||
|
[
|
||||||
|
[baker_1.clone(), baker_2.clone()],
|
||||||
|
[baker_3.clone(), baker_4.clone()]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// now, start from baker
|
||||||
|
|
||||||
|
let bakers = baker::Entity::find().all(&ctx.db).await?;
|
||||||
|
let bakeries = bakers.load_one(bakery::Entity::find(), &ctx.db).await?;
|
||||||
|
|
||||||
|
// note that two bakers share the same bakery
|
||||||
|
assert_eq!(bakers, [baker_1, baker_2, baker_3, baker_4]);
|
||||||
|
assert_eq!(
|
||||||
|
bakeries,
|
||||||
|
[
|
||||||
|
Some(bakery_1.clone()),
|
||||||
|
Some(bakery_1),
|
||||||
|
Some(bakery_2.clone()),
|
||||||
|
Some(bakery_2)
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -137,8 +138,8 @@ async fn loader_load_many() -> Result<(), DbErr> {
|
|||||||
feature = "sqlx-sqlite",
|
feature = "sqlx-sqlite",
|
||||||
feature = "sqlx-postgres"
|
feature = "sqlx-postgres"
|
||||||
))]
|
))]
|
||||||
async fn loader_load_many_many() -> Result<(), DbErr> {
|
async fn loader_load_many_multi() -> Result<(), DbErr> {
|
||||||
let ctx = TestContext::new("loader_test_load_many_many").await;
|
let ctx = TestContext::new("loader_test_load_many_multi").await;
|
||||||
create_tables(&ctx.db).await?;
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
let bakery_1 = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
let bakery_1 = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
@ -167,6 +168,43 @@ async fn loader_load_many_many() -> Result<(), DbErr> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[ignore]
|
||||||
|
#[sea_orm_macros::test]
|
||||||
|
#[cfg(any(
|
||||||
|
feature = "sqlx-mysql",
|
||||||
|
feature = "sqlx-sqlite",
|
||||||
|
feature = "sqlx-postgres"
|
||||||
|
))]
|
||||||
|
async fn loader_load_many_to_many() -> Result<(), DbErr> {
|
||||||
|
let ctx = TestContext::new("loader_test_load_many_to_many").await;
|
||||||
|
create_tables(&ctx.db).await?;
|
||||||
|
|
||||||
|
let bakery_1 = insert_bakery(&ctx.db, "SeaSide Bakery").await?;
|
||||||
|
|
||||||
|
let baker_1 = insert_baker(&ctx.db, "Jane", bakery_1.id).await?;
|
||||||
|
let baker_2 = insert_baker(&ctx.db, "Peter", bakery_1.id).await?;
|
||||||
|
|
||||||
|
let cake_1 = insert_cake(&ctx.db, "Cheesecake", bakery_1.id).await?;
|
||||||
|
let cake_2 = insert_cake(&ctx.db, "Chocolate", bakery_1.id).await?;
|
||||||
|
let cake_3 = insert_cake(&ctx.db, "Chiffon", bakery_1.id).await?;
|
||||||
|
|
||||||
|
insert_cake_baker(&ctx.db, baker_1.id, cake_1.id).await?;
|
||||||
|
insert_cake_baker(&ctx.db, baker_1.id, cake_2.id).await?;
|
||||||
|
insert_cake_baker(&ctx.db, baker_2.id, cake_2.id).await?;
|
||||||
|
insert_cake_baker(&ctx.db, baker_2.id, cake_3.id).await?;
|
||||||
|
|
||||||
|
let bakers = baker::Entity::find().all(&ctx.db).await?;
|
||||||
|
let cakes = bakers.load_many(cake::Entity::find(), &ctx.db).await?;
|
||||||
|
|
||||||
|
println!("{bakers:?}");
|
||||||
|
println!("{cakes:?}");
|
||||||
|
|
||||||
|
assert_eq!(bakers, [baker_1, baker_2]);
|
||||||
|
assert_eq!(cakes, [vec![cake_1, cake_2.clone()], vec![cake_2, cake_3]]);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn insert_bakery(db: &DbConn, name: &str) -> Result<bakery::Model, DbErr> {
|
pub async fn insert_bakery(db: &DbConn, name: &str) -> Result<bakery::Model, DbErr> {
|
||||||
bakery::ActiveModel {
|
bakery::ActiveModel {
|
||||||
name: Set(name.to_owned()),
|
name: Set(name.to_owned()),
|
||||||
@ -199,3 +237,16 @@ pub async fn insert_cake(db: &DbConn, name: &str, bakery_id: i32) -> Result<cake
|
|||||||
.insert(db)
|
.insert(db)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn insert_cake_baker(
|
||||||
|
db: &DbConn,
|
||||||
|
baker_id: i32,
|
||||||
|
cake_id: i32,
|
||||||
|
) -> Result<cakes_bakers::Model, DbErr> {
|
||||||
|
cakes_bakers::ActiveModel {
|
||||||
|
cake_id: Set(cake_id),
|
||||||
|
baker_id: Set(baker_id),
|
||||||
|
}
|
||||||
|
.insert(db)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user