Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases improvement #1811

Merged
merged 23 commits into from
Aug 18, 2023
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 288 additions & 0 deletions src/query/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,4 +637,292 @@ mod tests {
.join(" ")
);
}

fn cake_fruit_model(cake_id: i32, cake_name: String, fruit_id: i32, fruit_name: String)
-> (sea_orm::tests_cfg::cake::Model, sea_orm::tests_cfg::fruit::Model) {
(cake_model(cake_id, cake_name), fruit_model(fruit_id, fruit_name, Some(cake_id)))
}
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved

fn cake_model(id: i32, name: String) -> sea_orm::tests_cfg::cake::Model {
sea_orm::tests_cfg::cake::Model {
id,
name,
}
}

fn fruit_model(id: i32, name: String, cake_id: Option<i32>) -> sea_orm::tests_cfg::fruit::Model {
sea_orm::tests_cfg::fruit::Model {
id,
name,
cake_id
}
}

#[smol_potat::test]
pub async fn also_related() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect, Transaction, Statement};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned())
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
]])
.into_connection();

assert_eq!(
Cake::find()
.find_also_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(1, "apple".to_owned(), Some(1)))
)
]
);

assert_eq!(
db.into_transaction_log(),
[Transaction::many([Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "cake"."name", "fruit"."name""#,
r#"FROM "cake""#,
r#"LEFT JOIN "fruit" ON "cake"."id" = "fruit"."cake_id""#
]
.join(" ")
.as_str(),
[]
),])]
);

Ok(())
}

#[smol_potat::test]
pub async fn also_related_2() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(1, "apple cake".to_owned(), 2, "orange".to_owned())
]])
.into_connection();

assert_eq!(
Cake::find()
.find_also_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(1, "apple".to_owned(), Some(1)))
), (
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(2, "orange".to_owned(), Some(1)))
)
]
);

Ok(())
}

#[smol_potat::test]
pub async fn also_related_3() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(1, "apple cake".to_owned(), 2, "orange".to_owned()),
cake_fruit_model(2, "orange cake".to_owned(), 2, "orange".to_owned())
]])
.into_connection();

assert_eq!(
Cake::find()
.find_also_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(1, "apple".to_owned(), Some(1)))
),
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(2, "orange".to_owned(), Some(1)))
),
(
cake_model(2, "orange cake".to_owned()),
Some(fruit_model(2, "orange".to_owned(), Some(2)))
)
]
);

Ok(())
}

// fixme: unsure on how to insert a cake with no fruit into query_result
// #[smol_potat::test]
pub async fn also_related_4() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(1, "apple cake".to_owned(), 2, "orange".to_owned()),
cake_fruit_model(2, "orange cake".to_owned(), 2, "orange".to_owned()),
]])
.append_query_results([[
cake_model(3, "chocolate cake".to_owned()) // no fruit in cake
]])
.into_connection();

assert_eq!(
Cake::find()
.find_also_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(1, "apple".to_owned(), Some(1)))
),
(
cake_model(1, "apple cake".to_owned()),
Some(fruit_model(2, "orange".to_owned(), Some(1)))
),
(
cake_model(2, "orange cake".to_owned()),
Some(fruit_model(2, "orange".to_owned(), Some(2)))
),
(
cake_model(3, "chocolate cake".to_owned()),
None
)
]
);

Ok(())
}

#[smol_potat::test]
#[cfg(any(
feature = "mock",
))]
pub async fn with_related() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect, Transaction, Statement};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(1, "apple cake".to_owned(), 2, "orange".to_owned())
]])
.into_connection();

assert_eq!(
Cake::find()
.find_with_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
vec![
fruit_model(1, "apple".to_owned(), Some(1)),
fruit_model(2, "orange".to_owned(), Some(1))
]
)
]
);

assert_eq!(
db.into_transaction_log(),
[Transaction::many([Statement::from_sql_and_values(
DbBackend::Postgres,
[
r#"SELECT "cake"."name", "fruit"."name""#,
r#"FROM "cake""#,
r#"LEFT JOIN "fruit" ON "cake"."id" = "fruit"."cake_id""#,
r#"ORDER BY "cake"."id" ASC"#
]
.join(" ")
.as_str(),
[]
),])]
);

Ok(())
}

#[smol_potat::test]
#[cfg(any(
feature = "mock",
))]
pub async fn with_related_2() -> Result<(), sea_orm::DbErr> {
use sea_orm::{MockDatabase, DbBackend, EntityTrait, QuerySelect};
use sea_orm::tests_cfg::*;

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_fruit_model(1, "apple cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(2, "fruit cake".to_owned(), 1, "apple".to_owned()),
cake_fruit_model(2, "fruit cake".to_owned(), 2, "orange".to_owned()),
cake_fruit_model(2, "fruit cake".to_owned(), 3, "grape".to_owned()),
cake_fruit_model(2, "fruit cake".to_owned(), 4, "melon".to_owned()),
tyt2y3 marked this conversation as resolved.
Show resolved Hide resolved
]])
.into_connection();

assert_eq!(
Cake::find()
.find_with_related(Fruit)
.select_only()
.column(cake::Column::Name)
.column(fruit::Column::Name)
.all(&db)
.await?,
[
(
cake_model(1, "apple cake".to_owned()),
vec![
fruit_model(1, "apple".to_owned(), Some(1)),
]
),
(
cake_model(2, "fruit cake".to_owned()),
vec![
fruit_model(1, "apple".to_owned(), Some(2)),
fruit_model(2, "orange".to_owned(), Some(2)),
fruit_model(3, "grape".to_owned(), Some(2)),
fruit_model(4, "melon".to_owned(), Some(2)),
]
)
]
);

Ok(())
}
}