From b22753bebf3280b24d5726e7aca5c1fff8b7b473 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 20 Sep 2021 15:05:39 +0800 Subject: [PATCH] Test `try_join!` --- README.md | 1 - src/database/mock.rs | 32 ++++++++++++++++----------- src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index ae9e7bfc..c362a592 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,6 @@ Relying on [SQLx](https://github.com/launchbadge/sqlx), SeaORM is a new library // execute multiple queries in parallel let cakes_and_fruits: (Vec, Vec) = futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?; - ``` 2. Dynamic diff --git a/src/database/mock.rs b/src/database/mock.rs index e35280cc..ccb34a49 100644 --- a/src/database/mock.rs +++ b/src/database/mock.rs @@ -29,19 +29,6 @@ pub trait IntoMockRow { fn into_mock_row(self) -> MockRow; } -impl IntoMockRow for M -where - M: ModelTrait, -{ - fn into_mock_row(self) -> MockRow { - let mut values = BTreeMap::new(); - for col in <::Column>::iter() { - values.insert(col.to_string(), self.get(col)); - } - MockRow { values } - } -} - impl MockDatabase { pub fn new(db_backend: DbBackend) -> Self { Self { @@ -121,6 +108,25 @@ impl MockRow { } } +impl IntoMockRow for MockRow { + fn into_mock_row(self) -> MockRow { + self + } +} + +impl IntoMockRow for M +where + M: ModelTrait, +{ + fn into_mock_row(self) -> MockRow { + let mut values = BTreeMap::new(); + for col in <::Column>::iter() { + values.insert(col.to_string(), self.get(col)); + } + MockRow { values } + } +} + impl IntoMockRow for BTreeMap<&str, Value> { fn into_mock_row(self) -> MockRow { MockRow { diff --git a/src/lib.rs b/src/lib.rs index 9fea7569..7b54e2fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,14 +39,57 @@ //! Relying on [SQLx](https://github.com/launchbadge/sqlx), SeaORM is a new library with async support from day 1. //! //! ``` -//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*}; -//! # async fn function(db: &DbConn) -> Result<(), DbErr> { +//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*, DatabaseConnection, DbBackend, MockDatabase, Transaction, IntoMockRow}; +//! # 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 _: Result<(), DbErr> = smol::block_on(async { //! // execute multiple queries in parallel //! let cakes_and_fruits: (Vec, Vec) = //! futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?; -//! +//! # assert_eq!( +//! # cakes_and_fruits, +//! # ( +//! # vec![cake::Model { +//! # id: 1, +//! # name: "New York Cheese".to_owned(), +//! # }], +//! # vec![fruit::Model { +//! # id: 1, +//! # name: "Apple".to_owned(), +//! # cake_id: Some(1), +//! # }] +//! # ) +//! # ); +//! # assert_eq!( +//! # db.into_transaction_log(), +//! # vec![ +//! # Transaction::from_sql_and_values( +//! # DbBackend::Postgres, +//! # r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, +//! # vec![] +//! # ), +//! # Transaction::from_sql_and_values( +//! # DbBackend::Postgres, +//! # r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#, +//! # vec![] +//! # ), +//! # ] +//! # ); //! # Ok(()) -//! # } +//! # }); //! ``` //! //! 2. Dynamic