From 4c147a2d24dcbede4a2dac598dedfda43ea45266 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 16:27:54 +0800 Subject: [PATCH] Rewrite doctests --- src/docs.rs | 11 +- src/entity/active_model.rs | 253 +++++++++++++++++++++++++++++++++++++ src/entity/base_entity.rs | 143 +++++++++++---------- src/entity/model.rs | 12 +- src/executor/paginator.rs | 18 ++- src/executor/query.rs | 13 +- src/executor/select.rs | 78 ++++++------ 7 files changed, 406 insertions(+), 122 deletions(-) diff --git a/src/docs.rs b/src/docs.rs index 4d1226c3..3870e716 100644 --- a/src/docs.rs +++ b/src/docs.rs @@ -3,7 +3,12 @@ //! 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::*, DatabaseConnection, DbBackend, MockDatabase, Transaction, IntoMockRow}; +//! # use sea_orm::{error::*, tests_cfg::*, *}; +//! # +//! # #[smol_potat::main] +//! # #[cfg(feature = "mock")] +//! # pub async fn main() -> Result<(), DbErr> { +//! # //! # let db = MockDatabase::new(DbBackend::Postgres) //! # .append_query_results(vec![ //! # vec![cake::Model { @@ -19,7 +24,7 @@ //! # .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))?; @@ -53,7 +58,7 @@ //! # ] //! # ); //! # Ok(()) -//! # }); +//! # } //! ``` //! //! 2. Dynamic diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index efb234ea..c7d26574 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -140,6 +140,106 @@ pub trait ActiveModelTrait: Clone + Debug { } /// Perform an `INSERT` operation on the ActiveModel + /// + /// # Example (Postgres) + /// + /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] + /// # #[cfg(feature = "mock")] + /// # pub async fn main() -> Result<(), DbErr> { + /// # + /// # let db = MockDatabase::new(DbBackend::Postgres) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 15, + /// # rows_affected: 1, + /// # }, + /// # ]) + /// # .into_connection(); + /// # + /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; + /// + /// let apple = cake::ActiveModel { + /// name: Set("Apple Pie".to_owned()), + /// ..Default::default() + /// }; + /// + /// assert_eq!( + /// apple + /// .insert(&db) + /// .await?, + /// cake::ActiveModel { + /// id: Set(150), + /// name: Set("Apple Pie".to_owned()), + /// } + /// ); + /// assert!(false); + /// + /// assert_eq!( + /// db.into_transaction_log(), + /// vec![Transaction::from_sql_and_values( + /// DbBackend::Postgres, + /// r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#, + /// vec!["Apple Pie".into()] + /// )]); + /// # + /// # Ok(()) + /// # } + /// ``` + /// + /// # Example (MySQL) + /// + /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] + /// # #[cfg(feature = "mock")] + /// # pub async fn main() -> Result<(), DbErr> { + /// # + /// # let db = MockDatabase::new(DbBackend::MySql) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 15, + /// # rows_affected: 1, + /// # }, + /// # ]) + /// # .into_connection(); + /// # + /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; + /// + /// let apple = cake::ActiveModel { + /// name: Set("Apple Pie".to_owned()), + /// ..Default::default() + /// }; + /// + /// assert_eq!( + /// apple + /// .insert(&db) + /// .await?, + /// cake::ActiveModel { + /// id: Set(150), + /// name: Set("Apple Pie".to_owned()), + /// } + /// ); + /// + /// assert_eq!( + /// db.into_transaction_log(), + /// vec![ + /// Transaction::from_sql_and_values( + /// DbBackend::MySql, + /// r#"INSERT INTO `cake` (`name`) VALUES (?)"#, + /// vec!["Apple Pie".into()] + /// ), + /// Transaction::from_sql_and_values( + /// DbBackend::MySql, + /// r#"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = ? LIMIT ?"#, + /// vec![15.into(), 1u64.into()])]); + /// # + /// # Ok(()) + /// # } + /// ``` async fn insert<'a, C>(self, db: &'a C) -> Result where ::Model: IntoActiveModel, @@ -154,6 +254,117 @@ pub trait ActiveModelTrait: Clone + Debug { } /// Perform the `UPDATE` operation on an ActiveModel + /// + /// # Example (Postgres) + /// + /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] + /// # #[cfg(feature = "mock")] + /// # pub async fn main() -> Result<(), DbErr> { + /// # + /// # let db = MockDatabase::new(DbBackend::Postgres) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 0, + /// # rows_affected: 1, + /// # }, + /// # ]) + /// # .into_connection(); + /// # + /// use sea_orm::{entity::*, query::*, tests_cfg::fruit}; + /// + /// let orange = fruit::ActiveModel { + /// id: Set(1), + /// name: Set("Orange".to_owned()), + /// ..Default::default() + /// }; + /// + /// assert_eq!( + /// orange + /// .update(&db) + /// .await?, + /// fruit::ActiveModel { + /// id: Set(1), + /// name: Set("Orange".to_owned()), + /// cake_id: Set(None), + /// } + /// ); + /// + /// assert_eq!( + /// db.into_transaction_log(), + /// vec![Transaction::from_sql_and_values( + /// DbBackend::Postgres, + /// r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 RETURNING "id", "name", "cake_id""#, + /// vec!["Orange".into(), 1i32.into()] + /// )]); + /// # + /// # Ok(()) + /// # } + /// ``` + /// + /// # Example (MySQL) + /// + /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] + /// # #[cfg(feature = "mock")] + /// # pub async fn main() -> Result<(), DbErr> { + /// # + /// # let db = MockDatabase::new(DbBackend::MySql) + /// # .append_query_results(vec![ + /// # vec![fruit::Model { + /// # id: 1, + /// # name: "Orange".to_owned(), + /// # cake_id: None, + /// # }], + /// # ]) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 0, + /// # rows_affected: 1, + /// # }, + /// # ]) + /// # .into_connection(); + /// # + /// use sea_orm::{entity::*, query::*, tests_cfg::fruit}; + /// + /// let orange = fruit::ActiveModel { + /// id: Set(1), + /// name: Set("Orange".to_owned()), + /// ..Default::default() + /// }; + /// + /// assert_eq!( + /// orange + /// .update(&db) + /// .await?, + /// fruit::ActiveModel { + /// id: Set(1), + /// name: Set("Orange".to_owned()), + /// cake_id: Set(None), + /// } + /// ); + /// + /// assert_eq!( + /// db.into_transaction_log(), + /// vec![ + /// Transaction::from_sql_and_values( + /// DbBackend::MySql, + /// r#"UPDATE `fruit` SET `name` = ? WHERE `fruit`.`id` = ?"#, + /// vec!["Orange".into(), 1i32.into()] + /// ), + /// Transaction::from_sql_and_values( + /// DbBackend::MySql, + /// r#"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = ? LIMIT ?"#, + /// vec![1i32.into(), 1u64.into()] + /// )]); + /// # + /// # Ok(()) + /// # } + /// ``` async fn update<'a, C>(self, db: &'a C) -> Result where ::Model: IntoActiveModel, @@ -191,6 +402,48 @@ pub trait ActiveModelTrait: Clone + Debug { } /// Delete an active model by its primary key + /// + /// # Example + /// + /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] + /// # #[cfg(feature = "mock")] + /// # pub async fn main() -> Result<(), DbErr> { + /// # + /// # let db = MockDatabase::new(DbBackend::Postgres) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 0, + /// # rows_affected: 1, + /// # }, + /// # ]) + /// # .into_connection(); + /// # + /// use sea_orm::{entity::*, query::*, tests_cfg::fruit}; + /// + /// let orange = fruit::ActiveModel { + /// id: Set(3), + /// ..Default::default() + /// }; + /// + /// let delete_result = orange.delete(&db).await?; + /// + /// assert_eq!(delete_result.rows_affected, 1); + /// + /// assert_eq!( + /// db.into_transaction_log(), + /// vec![Transaction::from_sql_and_values( + /// DbBackend::Postgres, + /// r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#, + /// vec![3i32.into()] + /// )] + /// ); + /// # + /// # Ok(()) + /// # } + /// ``` async fn delete<'a, C>(self, db: &'a C) -> Result where Self: ActiveModelBehavior + 'a, diff --git a/src/entity/base_entity.rs b/src/entity/base_entity.rs index 60369623..8bbb6195 100644 --- a/src/entity/base_entity.rs +++ b/src/entity/base_entity.rs @@ -95,8 +95,11 @@ pub trait EntityTrait: EntityName { /// # Example /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![ @@ -121,8 +124,6 @@ pub trait EntityTrait: EntityName { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// assert_eq!( /// cake::Entity::find().one(&db).await?, /// Some(cake::Model { @@ -144,9 +145,6 @@ pub trait EntityTrait: EntityName { /// }, /// ] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -163,6 +161,9 @@ pub trait EntityTrait: EntityName { /// ), /// ] /// ); + /// # + /// # Ok(()) + /// # } /// ``` fn find() -> Select { Select::new() @@ -173,8 +174,11 @@ pub trait EntityTrait: EntityName { /// # Example /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![ @@ -189,8 +193,6 @@ pub trait EntityTrait: EntityName { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// assert_eq!( /// cake::Entity::find_by_id(11).all(&db).await?, /// vec![cake::Model { @@ -198,9 +200,6 @@ pub trait EntityTrait: EntityName { /// name: "Sponge Cake".to_owned(), /// }] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -210,11 +209,17 @@ pub trait EntityTrait: EntityName { /// vec![11i32.into()] /// )] /// ); + /// # + /// # Ok(()) + /// # } /// ``` /// Find by composite key /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![ @@ -229,8 +234,6 @@ pub trait EntityTrait: EntityName { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake_filling}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// assert_eq!( /// cake_filling::Entity::find_by_id((2, 3)).all(&db).await?, /// vec![cake_filling::Model { @@ -238,9 +241,6 @@ pub trait EntityTrait: EntityName { /// filling_id: 3, /// }] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -252,6 +252,9 @@ pub trait EntityTrait: EntityName { /// ].join(" ").as_str(), /// vec![2i32.into(), 3i32.into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn find_by_id(values: ::ValueType) -> Select { let mut select = Self::find(); @@ -317,8 +320,11 @@ pub trait EntityTrait: EntityName { /// # Example (MySQL) /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::MySql) /// # .append_exec_results(vec![ @@ -336,14 +342,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let insert_result = cake::Entity::insert(apple).exec(&db).await?; /// /// assert_eq!(insert_result.last_insert_id, 15); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -352,6 +353,9 @@ pub trait EntityTrait: EntityName { /// r#"INSERT INTO `cake` (`name`) VALUES (?)"#, /// vec!["Apple Pie".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn insert(model: A) -> Insert where @@ -365,8 +369,11 @@ pub trait EntityTrait: EntityName { /// # Example (Postgres) /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_exec_results(vec![ @@ -388,14 +395,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?; /// /// assert_eq!(insert_result.last_insert_id, 28); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -404,13 +406,19 @@ pub trait EntityTrait: EntityName { /// r#"INSERT INTO "cake" ("name") VALUES ($1), ($2) RETURNING "id""#, /// vec!["Apple Pie".into(), "Orange Scone".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` /// /// # Example (MySQL) /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::MySql) /// # .append_exec_results(vec![ @@ -432,14 +440,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?; /// /// assert_eq!(insert_result.last_insert_id, 28); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -448,6 +451,9 @@ pub trait EntityTrait: EntityName { /// r#"INSERT INTO `cake` (`name`) VALUES (?), (?)"#, /// vec!["Apple Pie".into(), "Orange Scone".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn insert_many(models: I) -> Insert where @@ -464,8 +470,11 @@ pub trait EntityTrait: EntityName { /// # Example (Postgres) /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_exec_results(vec![ @@ -484,8 +493,6 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// assert_eq!( /// fruit::Entity::update(orange.clone()) /// .filter(fruit::Column::Name.contains("orange")) @@ -493,9 +500,6 @@ pub trait EntityTrait: EntityName { /// .await?, /// orange /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -504,13 +508,19 @@ pub trait EntityTrait: EntityName { /// r#"UPDATE "fruit" SET "name" = $1 WHERE "fruit"."id" = $2 AND "fruit"."name" LIKE $3 RETURNING "id", "name", "cake_id""#, /// vec!["Orange".into(), 1i32.into(), "%orange%".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` /// /// # Example (MySQL) /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::MySql) /// # .append_query_results(vec![ @@ -536,8 +546,6 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// assert_eq!( /// fruit::Entity::update(orange.clone()) /// .filter(fruit::Column::Name.contains("orange")) @@ -545,9 +553,6 @@ pub trait EntityTrait: EntityName { /// .await?, /// orange /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -562,6 +567,9 @@ pub trait EntityTrait: EntityName { /// r#"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `fruit`.`id` = ? LIMIT ?"#, /// vec![1i32.into(), 1u64.into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn update(model: A) -> UpdateOne where @@ -577,8 +585,11 @@ pub trait EntityTrait: EntityName { /// # Example /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_exec_results(vec![ @@ -591,8 +602,6 @@ pub trait EntityTrait: EntityName { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, Value}}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let update_result = fruit::Entity::update_many() /// .col_expr(fruit::Column::CakeId, Expr::value(Value::Int(None))) /// .filter(fruit::Column::Name.contains("Apple")) @@ -600,9 +609,6 @@ pub trait EntityTrait: EntityName { /// .await?; /// /// assert_eq!(update_result.rows_affected, 5); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -611,6 +617,9 @@ pub trait EntityTrait: EntityName { /// r#"UPDATE "fruit" SET "cake_id" = $1 WHERE "fruit"."name" LIKE $2"#, /// vec![Value::Int(None), "%Apple%".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn update_many() -> UpdateMany { Update::many(Self::default()) @@ -623,8 +632,11 @@ pub trait EntityTrait: EntityName { /// # Example /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_exec_results(vec![ @@ -642,14 +654,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let delete_result = fruit::Entity::delete(orange).exec(&db).await?; /// /// assert_eq!(delete_result.rows_affected, 1); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -657,6 +664,9 @@ pub trait EntityTrait: EntityName { /// DbBackend::Postgres, r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#, /// vec![3i32.into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn delete(model: A) -> DeleteOne where @@ -672,8 +682,11 @@ pub trait EntityTrait: EntityName { /// # Example /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{entity::*, error::*, query::*, tests_cfg::*, MockDatabase, MockExecResult, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_exec_results(vec![ @@ -686,17 +699,12 @@ pub trait EntityTrait: EntityName { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::fruit}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let delete_result = fruit::Entity::delete_many() /// .filter(fruit::Column::Name.contains("Apple")) /// .exec(&db) /// .await?; /// /// assert_eq!(delete_result.rows_affected, 5); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -705,6 +713,9 @@ pub trait EntityTrait: EntityName { /// r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE $1"#, /// vec!["%Apple%".into()] /// )]); + /// # + /// # Ok(()) + /// # } /// ``` fn delete_many() -> DeleteMany { Delete::many(Self::default()) diff --git a/src/entity/model.rs b/src/entity/model.rs index b11a700b..acea83d6 100644 --- a/src/entity/model.rs +++ b/src/entity/model.rs @@ -47,8 +47,11 @@ pub trait FromQueryResult: Sized { } /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -67,8 +70,6 @@ pub trait FromQueryResult: Sized { /// num_of_cakes: i32, /// } /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec = SelectResult::find_by_statement(Statement::from_sql_and_values( /// DbBackend::Postgres, /// r#"SELECT "name", COUNT(*) AS "num_of_cakes" FROM "cake" GROUP BY("name")"#, @@ -85,8 +86,6 @@ pub trait FromQueryResult: Sized { /// },] /// ); /// # - /// # Ok(()) - /// # }); /// # assert_eq!( /// # db.into_transaction_log(), /// # vec![Transaction::from_sql_and_values( @@ -95,6 +94,9 @@ pub trait FromQueryResult: Sized { /// # vec![] /// # ),] /// # ); + /// # + /// # Ok(()) + /// # } /// ``` fn find_by_statement(stmt: Statement) -> SelectorRaw> { SelectorRaw::>::from_statement(stmt) diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index f52b3bd8..47548a0a 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -97,11 +97,14 @@ where /// Fetch one page and increment the page counter /// /// ```rust + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { + /// # /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; - /// # let _: Result<(), DbErr> = smol::block_on(async { /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// let mut cake_pages = cake::Entity::find() @@ -113,7 +116,7 @@ where /// } /// # /// # Ok(()) - /// # }); + /// # } /// ``` pub async fn fetch_and_next(&mut self) -> Result>, DbErr> { let vec = self.fetch().await?; @@ -125,11 +128,14 @@ where /// Convert self into an async stream /// /// ```rust + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, MockDatabase, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { + /// # /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # let db = &owned_db; - /// # let _: Result<(), DbErr> = smol::block_on(async { /// # /// use futures::TryStreamExt; /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; @@ -143,7 +149,7 @@ where /// } /// # /// # Ok(()) - /// # }); + /// # } /// ``` pub fn into_stream(mut self) -> PinBoxStream<'db, Result, DbErr>> { Box::pin(stream! { diff --git a/src/executor/query.rs b/src/executor/query.rs index 3c1a9184..12d0c7cf 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -320,8 +320,11 @@ pub trait TryGetableMany: Sized { fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result; /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(all(feature = "mock", feature = "macros"))] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -344,8 +347,6 @@ pub trait TryGetableMany: Sized { /// NumOfCakes, /// } /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec<(String, i32)> = /// <(String, i32)>::find_by_statement::(Statement::from_sql_and_values( /// DbBackend::Postgres, @@ -362,9 +363,6 @@ pub trait TryGetableMany: Sized { /// ("New York Cheese".to_owned(), 1), /// ] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -374,6 +372,9 @@ pub trait TryGetableMany: Sized { /// vec![] /// ),] /// ); + /// # + /// # Ok(()) + /// # } /// ``` fn find_by_statement(stmt: Statement) -> SelectorRaw> where diff --git a/src/executor/select.rs b/src/executor/select.rs index 9fb906cf..3a08392e 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -143,8 +143,11 @@ where } /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(all(feature = "mock", feature = "macros"))] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -164,8 +167,6 @@ where /// CakeName, /// } /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec = cake::Entity::find() /// .select_only() /// .column_as(cake::Column::Name, QueryAs::CakeName) @@ -177,9 +178,6 @@ where /// res, /// vec!["Chocolate Forest".to_owned(), "New York Cheese".to_owned()] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -189,11 +187,17 @@ where /// vec![] /// )] /// ); + /// # + /// # Ok(()) + /// # } /// ``` /// /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(all(feature = "mock", feature = "macros"))] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -212,8 +216,6 @@ where /// NumOfCakes, /// } /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec<(String, i64)> = cake::Entity::find() /// .select_only() /// .column_as(cake::Column::Name, QueryAs::CakeName) @@ -224,9 +226,6 @@ where /// .await?; /// /// assert_eq!(res, vec![("Chocolate Forest".to_owned(), 2i64)]); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -241,6 +240,9 @@ where /// vec![] /// )] /// ); + /// # + /// # Ok(()) + /// # } /// ``` pub fn into_values(self) -> Selector> where @@ -490,8 +492,11 @@ where } /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -514,8 +519,6 @@ where /// num_of_cakes: i32, /// } /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec = cake::Entity::find() /// .from_raw_sql(Statement::from_sql_and_values( /// DbBackend::Postgres, @@ -539,9 +542,6 @@ where /// }, /// ] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -551,6 +551,9 @@ where /// vec![] /// ),] /// ); + /// # + /// # Ok(()) + /// # } /// ``` pub fn into_model(self) -> SelectorRaw> where @@ -563,8 +566,11 @@ where } /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) /// # .append_query_results(vec![vec![ @@ -581,8 +587,6 @@ where /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let res: Vec = cake::Entity::find().from_raw_sql( /// Statement::from_sql_and_values( /// DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![] @@ -605,9 +609,6 @@ where /// }), /// ] /// ); - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -616,6 +617,9 @@ where /// DbBackend::Postgres, r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, vec![] /// ), /// ]); + /// # + /// # Ok(()) + /// # } /// ``` #[cfg(feature = "with-json")] pub fn into_json(self) -> SelectorRaw> { @@ -627,15 +631,16 @@ where /// Get an item from the Select query /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let _: Option = cake::Entity::find() /// .from_raw_sql(Statement::from_sql_and_values( /// DbBackend::Postgres, @@ -644,9 +649,6 @@ where /// )) /// .one(&db) /// .await?; - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -656,6 +658,9 @@ where /// vec![1.into()] /// ),] /// ); + /// # + /// # Ok(()) + /// # } /// ``` pub async fn one<'a, C>(self, db: &C) -> Result, DbErr> where @@ -670,15 +675,16 @@ where /// Get all items from the Select query /// ``` + /// # use sea_orm::{error::*, tests_cfg::*, *}; + /// # + /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] - /// # use sea_orm::{error::*, tests_cfg::*, MockDatabase, Transaction, DbBackend}; + /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres).into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// - /// # let _: Result<(), DbErr> = smol::block_on(async { - /// # /// let _: Vec = cake::Entity::find() /// .from_raw_sql(Statement::from_sql_and_values( /// DbBackend::Postgres, @@ -687,9 +693,6 @@ where /// )) /// .all(&db) /// .await?; - /// # - /// # Ok(()) - /// # }); /// /// assert_eq!( /// db.into_transaction_log(), @@ -699,6 +702,9 @@ where /// vec![] /// ),] /// ); + /// # + /// # Ok(()) + /// # } /// ``` pub async fn all<'a, C>(self, db: &C) -> Result, DbErr> where