From f7f90cd7c5f37b8491d550f108541813df5915fd Mon Sep 17 00:00:00 2001 From: Panagiotis Karatakis Date: Tue, 22 Nov 2022 10:38:54 +0200 Subject: [PATCH] Add load_many unit test --- src/entity/loader.rs | 121 ++++++++++++++++++++++++++---------------- src/entity/prelude.rs | 5 +- 2 files changed, 78 insertions(+), 48 deletions(-) diff --git a/src/entity/loader.rs b/src/entity/loader.rs index ede55008..3946e3bb 100644 --- a/src/entity/loader.rs +++ b/src/entity/loader.rs @@ -12,11 +12,9 @@ pub trait LoaderTrait { /// Source model type Model: ModelTrait; + /// /// Used to eager load has_one relations /// - /// - /// - /// async fn load_one(&self, stmt: Select, db: &C) -> Result>, DbErr> where C: ConnectionTrait, @@ -26,35 +24,9 @@ pub trait LoaderTrait { <::Model as ModelTrait>::Entity: Related, <<<::Model as ModelTrait>::Entity as EntityTrait>::Column as FromStr>::Err: Debug; - /// Used to eager load has_many relations /// - /// # Example + /// Used to eager load has_many relations /// - /// ``` - /// use sea_orm::{tests_cfg::*, entity::loader::*}; - /// - /// let db = MockDatabase::new(DbBackend::Postgres) - /// .append_query_results(vec![ - /// vec![cake::Model { - /// id: 1, - /// name: "New York Cheese".to_owned(), - /// } - /// .into_mock_row()], - /// vec![fruit::Model { - /// id: 1, - /// name: "Apple".to_owned(), - /// cake_id: Some(1), - /// } - /// .into_mock_row()], - /// ]) - /// .into_connection(); - /// - /// let cakes = vec![cake::Model { id: 1, name: "New York Cheese".to_owned(), }]; - /// - /// let fruits = cakes.load_many(fruit::Entity::find(), &db); - /// - /// assert_eq!(fruits, vec![[fruit::Model { id: 1, name: "Apple".to_owned(), cake_id: Some(1), }]]); - /// ``` async fn load_many(&self, stmt: Select, db: &C) -> Result>, DbErr> where C: ConnectionTrait, @@ -97,7 +69,7 @@ where .map(|model: &M| extract_key(&rel_def.from_col, model)) .collect(); - let condition = prepare_condition::(&rel_def.to_col, &keys); + let condition = prepare_condition::<::Model>(&rel_def.to_col, &keys); let stmt = as QueryFilter>::filter(stmt, condition); @@ -153,7 +125,7 @@ where .map(|model: &M| extract_key(&rel_def.from_col, model)) .collect(); - let condition = prepare_condition::(&rel_def.to_col, &keys); + let condition = prepare_condition::<::Model>(&rel_def.to_col, &keys); let stmt = as QueryFilter>::filter(stmt, condition); @@ -171,14 +143,21 @@ where .for_each(|value: ::Model| { let key = extract_key(&rel_def.to_col, &value); - let vec = hashmap.get_mut(&format!("{:?}", key)).unwrap(); + let vec = hashmap + .get_mut(&format!("{:?}", key)) + .expect("Failed at finding key on hashmap"); vec.push(value); }); let result: Vec> = keys .iter() - .map(|key: &Vec| hashmap.remove(&format!("{:?}", key)).to_owned().unwrap()) + .map(|key: &Vec| { + hashmap + .remove(&format!("{:?}", key)) + .to_owned() + .expect("Failed to convert key to owned") + }) .collect(); Ok(result) @@ -196,7 +175,7 @@ where <<::Entity as EntityTrait>::Column as FromStr>::from_str( &a.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column A:1"); vec![model.get(column_a)] } Identity::Binary(a, b) => { @@ -204,12 +183,12 @@ where <<::Entity as EntityTrait>::Column as FromStr>::from_str( &a.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column A:2"); let column_b = <<::Entity as EntityTrait>::Column as FromStr>::from_str( &b.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column B:2"); vec![model.get(column_a), model.get(column_b)] } Identity::Ternary(a, b, c) => { @@ -217,17 +196,17 @@ where <<::Entity as EntityTrait>::Column as FromStr>::from_str( &a.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column A:3"); let column_b = <<::Entity as EntityTrait>::Column as FromStr>::from_str( &b.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column B:3"); let column_c = <<::Entity as EntityTrait>::Column as FromStr>::from_str( &c.to_string(), ) - .unwrap(); + .expect("Failed at mapping string to column C:3"); vec![ model.get(column_a), model.get(column_b), @@ -246,7 +225,7 @@ where Identity::Unary(column_a) => { let column_a: ::Column = <::Column as FromStr>::from_str(&column_a.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *A:1"); Condition::all().add(ColumnTrait::is_in( &column_a, keys.iter() @@ -257,10 +236,10 @@ where Identity::Binary(column_a, column_b) => { let column_a: ::Column = <::Column as FromStr>::from_str(&column_a.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *A:2"); let column_b: ::Column = <::Column as FromStr>::from_str(&column_b.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *B:2"); Condition::all().add( Expr::tuple([ SimpleExpr::Column(column_a.into_column_ref()), @@ -276,13 +255,13 @@ where Identity::Ternary(column_a, column_b, column_c) => { let column_a: ::Column = <::Column as FromStr>::from_str(&column_a.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *A:3"); let column_b: ::Column = <::Column as FromStr>::from_str(&column_b.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *B:3"); let column_c: ::Column = <::Column as FromStr>::from_str(&column_c.to_string()) - .unwrap(); + .expect("Failed at mapping string to column *C:3"); Condition::all().add( Expr::tuple([ SimpleExpr::Column(column_a.into_column_ref()), @@ -300,3 +279,53 @@ where } } } + +#[cfg(test)] +mod tests { + #[tokio::test] + + async fn test_load_many() { + use crate::{ + entity::prelude::*, tests_cfg::*, DbBackend, IntoMockRow, LoaderTrait, MockDatabase, + }; + + let db = MockDatabase::new(DbBackend::Postgres) + .append_query_results(vec![ + vec![fruit::Model { + id: 1, + name: "Apple".to_owned(), + cake_id: Some(1), + } + .into_mock_row()], + ]) + .into_connection(); + + let cakes = vec![ + cake::Model { + id: 1, + name: "New York Cheese".to_owned(), + }, + cake::Model { + id: 2, + name: "London Cheese".to_owned(), + }, + ]; + + let fruits = cakes + .load_many(fruit::Entity::find(), &db) + .await + .expect("Should return something"); + + assert_eq!( + fruits, + vec![ + vec![fruit::Model { + id: 1, + name: "Apple".to_owned(), + cake_id: Some(1), + }], + vec![] + ] + ); + } +} diff --git a/src/entity/prelude.rs b/src/entity/prelude.rs index 1d68cba1..d724551e 100644 --- a/src/entity/prelude.rs +++ b/src/entity/prelude.rs @@ -1,8 +1,9 @@ pub use crate::{ error::*, ActiveEnum, ActiveModelBehavior, ActiveModelTrait, ColumnDef, ColumnTrait, ColumnType, CursorTrait, DatabaseConnection, DbConn, EntityName, EntityTrait, EnumIter, - ForeignKeyAction, Iden, IdenStatic, Linked, ModelTrait, PaginatorTrait, PrimaryKeyToColumn, - PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef, RelationTrait, Select, Value, + ForeignKeyAction, Iden, IdenStatic, Linked, LoaderTrait, ModelTrait, PaginatorTrait, + PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, QueryResult, Related, RelationDef, + RelationTrait, Select, Value, }; #[cfg(feature = "macros")]