Skip to content

Commit

Permalink
Add more find_also_related test cases
Browse files Browse the repository at this point in the history
From darkmmon/sea-orm/commit/ce5f6991391fdbb0b8deb72ddd404944bb05cf98
  • Loading branch information
tyt2y3 committed Aug 18, 2023
1 parent d50312c commit 08c3441
Showing 1 changed file with 134 additions and 5 deletions.
139 changes: 134 additions & 5 deletions src/executor/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,33 @@ mod tests {
sea_orm::tests_cfg::cake::Model { id, name }
}

fn filling_model(id: i32) -> sea_orm::tests_cfg::filling::Model {
let name = match id {
1 => "apple juice",
2 => "orange jam",
3 => "fruit",
4 => "chocolate crust",
_ => "",
}
.to_string();
sea_orm::tests_cfg::filling::Model {
id,
name,
vendor_id: Some(1),
ignored_attr: 0,
}
}

fn cake_filling_models(
cake_id: i32,
filling_id: i32,
) -> (
sea_orm::tests_cfg::cake::Model,
sea_orm::tests_cfg::filling::Model,
) {
(cake_model(cake_id), filling_model(filling_id))
}

fn fruit_model(id: i32, cake_id: Option<i32>) -> sea_orm::tests_cfg::fruit::Model {
let name = match id {
1 => "apple",
Expand Down Expand Up @@ -1230,7 +1257,7 @@ mod tests {
.append_query_results([[
cake_fruit_model(1, 1),
cake_fruit_model(1, 2),
cake_fruit_model(2, 2),
cake_fruit_model(2, 3),
]])
.into_connection();

Expand All @@ -1239,7 +1266,7 @@ mod tests {
[
(cake_model(1), Some(fruit_model(1, Some(1)))),
(cake_model(1), Some(fruit_model(2, Some(1)))),
(cake_model(2), Some(fruit_model(2, Some(2))))
(cake_model(2), Some(fruit_model(3, Some(2))))
]
);

Expand All @@ -1255,7 +1282,7 @@ mod tests {
.append_query_results([[
cake_fruit_model(1, 1).into_mock_row(),
cake_fruit_model(1, 2).into_mock_row(),
cake_fruit_model(2, 2).into_mock_row(),
cake_fruit_model(2, 3).into_mock_row(),
(cake_model(3), None::<fruit::Model>).into_mock_row(),
]])
.into_connection();
Expand All @@ -1265,7 +1292,59 @@ mod tests {
[
(cake_model(1), Some(fruit_model(1, Some(1)))),
(cake_model(1), Some(fruit_model(2, Some(1)))),
(cake_model(2), Some(fruit_model(2, Some(2)))),
(cake_model(2), Some(fruit_model(3, Some(2)))),
(cake_model(3), None)
]
);

Ok(())
}

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

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_filling_models(1, 1).into_mock_row(),
cake_filling_models(1, 2).into_mock_row(),
cake_filling_models(2, 2).into_mock_row(),
]])
.into_connection();

assert_eq!(
Cake::find().find_also_related(Filling).all(&db).await?,
[
(cake_model(1), Some(filling_model(1))),
(cake_model(1), Some(filling_model(2))),
(cake_model(2), Some(filling_model(2))),
]
);

Ok(())
}

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

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_filling_models(1, 1).into_mock_row(),
cake_filling_models(1, 2).into_mock_row(),
cake_filling_models(2, 2).into_mock_row(),
(cake_model(3), None::<filling::Model>).into_mock_row(),
]])
.into_connection();

assert_eq!(
Cake::find().find_also_related(Filling).all(&db).await?,
[
(cake_model(1), Some(filling_model(1))),
(cake_model(1), Some(filling_model(2))),
(cake_model(2), Some(filling_model(2))),
(cake_model(3), None)
]
);
Expand Down Expand Up @@ -1383,6 +1462,56 @@ mod tests {
Ok(())
}

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

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_filling_models(1, 1).into_mock_row(),
cake_filling_models(1, 2).into_mock_row(),
cake_filling_models(2, 2).into_mock_row(),
]])
.into_connection();

assert_eq!(
Cake::find().find_with_related(Filling).all(&db).await?,
[
(cake_model(1), vec![filling_model(1), filling_model(2)]),
(cake_model(2), vec![filling_model(2)]),
]
);

Ok(())
}

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

let db = MockDatabase::new(DbBackend::Postgres)
.append_query_results([[
cake_filling_models(1, 1).into_mock_row(),
cake_filling_models(1, 2).into_mock_row(),
cake_filling_models(2, 2).into_mock_row(),
(cake_model(3), None::<filling::Model>).into_mock_row(),
]])
.into_connection();

assert_eq!(
Cake::find().find_with_related(Filling).all(&db).await?,
[
(cake_model(1), vec![filling_model(1), filling_model(2)]),
(cake_model(2), vec![filling_model(2)]),
(cake_model(3), vec![])
]
);

Ok(())
}

#[smol_potat::test]
pub async fn also_linked_base() -> Result<(), sea_orm::DbErr> {
use sea_orm::tests_cfg::*;
Expand Down Expand Up @@ -1668,4 +1797,4 @@ mod tests {

Ok(())
}
}
}

0 comments on commit 08c3441

Please sign in to comment.