From 7298fdeda9ef71a661804eab44ac21793f549a37 Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Tue, 16 Nov 2021 17:35:49 +0800 Subject: [PATCH] Rewrite doctests --- src/entity/active_model.rs | 84 ++++++++++++++++-------------- src/entity/base_entity.rs | 103 +++++++++++++++++++++++-------------- src/executor/paginator.rs | 24 +++++++-- src/executor/select.rs | 18 ++++++- 4 files changed, 146 insertions(+), 83 deletions(-) diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index c7d26574..06e01fcb 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -151,11 +151,11 @@ pub trait ActiveModelTrait: Clone + Debug { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) - /// # .append_exec_results(vec![ - /// # MockExecResult { - /// # last_insert_id: 15, - /// # rows_affected: 1, - /// # }, + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 15, + /// # name: "Apple Pie".to_owned(), + /// # }], /// # ]) /// # .into_connection(); /// # @@ -167,15 +167,13 @@ pub trait ActiveModelTrait: Clone + Debug { /// }; /// /// assert_eq!( - /// apple - /// .insert(&db) - /// .await?, - /// cake::ActiveModel { - /// id: Set(150), - /// name: Set("Apple Pie".to_owned()), + /// apple.insert(&db).await?, + /// cake::Model { + /// id: 15, + /// name: "Apple Pie".to_owned(), /// } + /// .into_active_model() /// ); - /// assert!(false); /// /// assert_eq!( /// db.into_transaction_log(), @@ -183,7 +181,8 @@ pub trait ActiveModelTrait: Clone + Debug { /// DbBackend::Postgres, /// r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#, /// vec!["Apple Pie".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -199,6 +198,12 @@ pub trait ActiveModelTrait: Clone + Debug { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::MySql) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 15, + /// # name: "Apple Pie".to_owned(), + /// # }], + /// # ]) /// # .append_exec_results(vec![ /// # MockExecResult { /// # last_insert_id: 15, @@ -215,13 +220,12 @@ pub trait ActiveModelTrait: Clone + Debug { /// }; /// /// assert_eq!( - /// apple - /// .insert(&db) - /// .await?, - /// cake::ActiveModel { - /// id: Set(150), - /// name: Set("Apple Pie".to_owned()), + /// apple.insert(&db).await?, + /// cake::Model { + /// id: 15, + /// name: "Apple Pie".to_owned(), /// } + /// .into_active_model() /// ); /// /// assert_eq!( @@ -235,7 +239,10 @@ pub trait ActiveModelTrait: Clone + Debug { /// Transaction::from_sql_and_values( /// DbBackend::MySql, /// r#"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = ? LIMIT ?"#, - /// vec![15.into(), 1u64.into()])]); + /// vec![15.into(), 1u64.into()] + /// ) + /// ] + /// ); /// # /// # Ok(()) /// # } @@ -265,11 +272,12 @@ pub trait ActiveModelTrait: Clone + Debug { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) - /// # .append_exec_results(vec![ - /// # MockExecResult { - /// # last_insert_id: 0, - /// # rows_affected: 1, - /// # }, + /// # .append_query_results(vec![ + /// # vec![fruit::Model { + /// # id: 1, + /// # name: "Orange".to_owned(), + /// # cake_id: None, + /// # }], /// # ]) /// # .into_connection(); /// # @@ -282,14 +290,13 @@ pub trait ActiveModelTrait: Clone + Debug { /// }; /// /// assert_eq!( - /// orange - /// .update(&db) - /// .await?, - /// fruit::ActiveModel { - /// id: Set(1), - /// name: Set("Orange".to_owned()), - /// cake_id: Set(None), + /// orange.update(&db).await?, + /// fruit::Model { + /// id: 1, + /// name: "Orange".to_owned(), + /// cake_id: None, /// } + /// .into_active_model() /// ); /// /// assert_eq!( @@ -338,14 +345,13 @@ pub trait ActiveModelTrait: Clone + Debug { /// }; /// /// assert_eq!( - /// orange - /// .update(&db) - /// .await?, - /// fruit::ActiveModel { - /// id: Set(1), - /// name: Set("Orange".to_owned()), - /// cake_id: Set(None), + /// orange.update(&db).await?, + /// fruit::Model { + /// id: 1, + /// name: "Orange".to_owned(), + /// cake_id: None, /// } + /// .into_active_model() /// ); /// /// assert_eq!( diff --git a/src/entity/base_entity.rs b/src/entity/base_entity.rs index 8bbb6195..e6247d35 100644 --- a/src/entity/base_entity.rs +++ b/src/entity/base_entity.rs @@ -285,12 +285,9 @@ pub trait EntityTrait: EntityName { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) - /// # .append_exec_results(vec![ - /// # MockExecResult { - /// # last_insert_id: 15, - /// # rows_affected: 1, - /// # }, - /// # ]) + /// # .append_query_results(vec![vec![maplit::btreemap! { + /// # "id" => Into::::into(15), + /// # }]]) /// # .into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; @@ -302,8 +299,7 @@ pub trait EntityTrait: EntityName { /// /// let insert_result = cake::Entity::insert(apple).exec(&db).await?; /// - /// assert_eq!(dbg!(insert_result.last_insert_id), 150); - /// assert!(false); + /// assert_eq!(dbg!(insert_result.last_insert_id), 15); /// /// assert_eq!( /// db.into_transaction_log(), @@ -311,7 +307,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::Postgres, /// r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id""#, /// vec!["Apple Pie".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -352,7 +349,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::MySql, /// r#"INSERT INTO `cake` (`name`) VALUES (?)"#, /// vec!["Apple Pie".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -376,12 +374,9 @@ pub trait EntityTrait: EntityName { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) - /// # .append_exec_results(vec![ - /// # MockExecResult { - /// # last_insert_id: 28, - /// # rows_affected: 2, - /// # }, - /// # ]) + /// # .append_query_results(vec![vec![maplit::btreemap! { + /// # "id" => Into::::into(28), + /// # }]]) /// # .into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; @@ -395,7 +390,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?; + /// let insert_result = cake::Entity::insert_many(vec![apple, orange]) + /// .exec(&db) + /// .await?; /// /// assert_eq!(insert_result.last_insert_id, 28); /// @@ -405,7 +402,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::Postgres, /// r#"INSERT INTO "cake" ("name") VALUES ($1), ($2) RETURNING "id""#, /// vec!["Apple Pie".into(), "Orange Scone".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -440,7 +438,9 @@ pub trait EntityTrait: EntityName { /// ..Default::default() /// }; /// - /// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?; + /// let insert_result = cake::Entity::insert_many(vec![apple, orange]) + /// .exec(&db) + /// .await?; /// /// assert_eq!(insert_result.last_insert_id, 28); /// @@ -450,7 +450,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::MySql, /// r#"INSERT INTO `cake` (`name`) VALUES (?), (?)"#, /// vec!["Apple Pie".into(), "Orange Scone".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -477,11 +478,12 @@ pub trait EntityTrait: EntityName { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::Postgres) - /// # .append_exec_results(vec![ - /// # MockExecResult { - /// # last_insert_id: 0, - /// # rows_affected: 1, - /// # }, + /// # .append_query_results(vec![ + /// # vec![fruit::Model { + /// # id: 1, + /// # name: "Orange".to_owned(), + /// # cake_id: None, + /// # }], /// # ]) /// # .into_connection(); /// # @@ -498,7 +500,12 @@ pub trait EntityTrait: EntityName { /// .filter(fruit::Column::Name.contains("orange")) /// .exec(&db) /// .await?, - /// orange + /// fruit::Model { + /// id: 1, + /// name: "Orange".to_owned(), + /// cake_id: None, + /// } + /// .into_active_model(), /// ); /// /// assert_eq!( @@ -523,6 +530,12 @@ pub trait EntityTrait: EntityName { /// # pub async fn main() -> Result<(), DbErr> { /// # /// # let db = MockDatabase::new(DbBackend::MySql) + /// # .append_exec_results(vec![ + /// # MockExecResult { + /// # last_insert_id: 0, + /// # rows_affected: 1, + /// # }, + /// # ]) /// # .append_query_results(vec![ /// # vec![fruit::Model { /// # id: 1, @@ -530,12 +543,6 @@ pub trait EntityTrait: EntityName { /// # 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}; @@ -551,7 +558,12 @@ pub trait EntityTrait: EntityName { /// .filter(fruit::Column::Name.contains("orange")) /// .exec(&db) /// .await?, - /// orange + /// fruit::Model { + /// id: 1, + /// name: "Orange".to_owned(), + /// cake_id: None, + /// } + /// .into_active_model(), /// ); /// /// assert_eq!( @@ -600,7 +612,12 @@ pub trait EntityTrait: EntityName { /// # ]) /// # .into_connection(); /// # - /// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, Value}}; + /// use sea_orm::{ + /// entity::*, + /// query::*, + /// sea_query::{Expr, Value}, + /// tests_cfg::fruit, + /// }; /// /// let update_result = fruit::Entity::update_many() /// .col_expr(fruit::Column::CakeId, Expr::value(Value::Int(None))) @@ -616,7 +633,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::Postgres, /// r#"UPDATE "fruit" SET "cake_id" = $1 WHERE "fruit"."name" LIKE $2"#, /// vec![Value::Int(None), "%Apple%".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -661,9 +679,11 @@ pub trait EntityTrait: EntityName { /// assert_eq!( /// db.into_transaction_log(), /// vec![Transaction::from_sql_and_values( - /// DbBackend::Postgres, r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#, + /// DbBackend::Postgres, + /// r#"DELETE FROM "fruit" WHERE "fruit"."id" = $1"#, /// vec![3i32.into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } @@ -695,6 +715,12 @@ pub trait EntityTrait: EntityName { /// # rows_affected: 5, /// # }, /// # ]) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 15, + /// # name: "Apple Pie".to_owned(), + /// # }], + /// # ]) /// # .into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::fruit}; @@ -712,7 +738,8 @@ pub trait EntityTrait: EntityName { /// DbBackend::Postgres, /// r#"DELETE FROM "fruit" WHERE "fruit"."name" LIKE $1"#, /// vec!["%Apple%".into()] - /// )]); + /// )] + /// ); /// # /// # Ok(()) /// # } diff --git a/src/executor/paginator.rs b/src/executor/paginator.rs index 47548a0a..e8f37bea 100644 --- a/src/executor/paginator.rs +++ b/src/executor/paginator.rs @@ -96,14 +96,22 @@ where /// Fetch one page and increment the page counter /// - /// ```rust + /// ``` /// # use sea_orm::{error::*, tests_cfg::*, *}; /// # /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] /// # pub async fn main() -> Result<(), DbErr> { /// # - /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); + /// # let owned_db = MockDatabase::new(DbBackend::Postgres) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 1, + /// # name: "Cake".to_owned(), + /// # }], + /// # vec![], + /// # ]) + /// # .into_connection(); /// # let db = &owned_db; /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; @@ -127,14 +135,22 @@ where /// Convert self into an async stream /// - /// ```rust + /// ``` /// # use sea_orm::{error::*, tests_cfg::*, *}; /// # /// # #[smol_potat::main] /// # #[cfg(feature = "mock")] /// # pub async fn main() -> Result<(), DbErr> { /// # - /// # let owned_db = MockDatabase::new(DbBackend::Postgres).into_connection(); + /// # let owned_db = MockDatabase::new(DbBackend::Postgres) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 1, + /// # name: "Cake".to_owned(), + /// # }], + /// # vec![], + /// # ]) + /// # .into_connection(); /// # let db = &owned_db; /// # /// use futures::TryStreamExt; diff --git a/src/executor/select.rs b/src/executor/select.rs index 3a08392e..4c0d8ecf 100644 --- a/src/executor/select.rs +++ b/src/executor/select.rs @@ -637,7 +637,14 @@ where /// # #[cfg(feature = "mock")] /// # pub async fn main() -> Result<(), DbErr> { /// # - /// # let db = MockDatabase::new(DbBackend::Postgres).into_connection(); + /// # let db = MockDatabase::new(DbBackend::Postgres) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 1, + /// # name: "Cake".to_owned(), + /// # }], + /// # ]) + /// # .into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; /// @@ -681,7 +688,14 @@ where /// # #[cfg(feature = "mock")] /// # pub async fn main() -> Result<(), DbErr> { /// # - /// # let db = MockDatabase::new(DbBackend::Postgres).into_connection(); + /// # let db = MockDatabase::new(DbBackend::Postgres) + /// # .append_query_results(vec![ + /// # vec![cake::Model { + /// # id: 1, + /// # name: "Cake".to_owned(), + /// # }], + /// # ]) + /// # .into_connection(); /// # /// use sea_orm::{entity::*, query::*, tests_cfg::cake}; ///