Improve doc test

This commit is contained in:
Billy Chan 2021-07-09 00:18:43 +08:00
parent 82992aab61
commit 3659abbf0a
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7

View File

@ -51,14 +51,56 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_query_results(vec![
/// # vec![
/// # cake::Model {
/// # id: 1,
/// # name: "New York Cheese".to_owned(),
/// # },
/// # ],
/// # vec![
/// # cake::Model {
/// # id: 1,
/// # name: "New York Cheese".to_owned(),
/// # },
/// # cake::Model {
/// # id: 2,
/// # name: "Chocolate Forest".to_owned(),
/// # },
/// # ],
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # async_std::task::block_on(async {
/// cake::Entity::find().one(&db).await;
/// cake::Entity::find().all(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// assert_eq!(
/// cake::Entity::find().one(&db).await?,
/// Some(cake::Model {
/// id: 1,
/// name: "New York Cheese".to_owned(),
/// })
/// );
///
/// assert_eq!(
/// cake::Entity::find().all(&db).await?,
/// vec![
/// cake::Model {
/// id: 1,
/// name: "New York Cheese".to_owned(),
/// },
/// cake::Model {
/// id: 2,
/// name: "Chocolate Forest".to_owned(),
/// },
/// ]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -79,13 +121,32 @@ pub trait EntityTrait: EntityName {
/// Find a model by primary key
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_query_results(vec![
/// # vec![
/// # cake::Model {
/// # id: 11,
/// # name: "Sponge Cake".to_owned(),
/// # },
/// # ],
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
/// # async_std::task::block_on(async {
/// cake::Entity::find_by_id(11).all(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// assert_eq!(
/// cake::Entity::find_by_id(11).all(&db).await?,
/// vec![cake::Model {
/// id: 11,
/// name: "Sponge Cake".to_owned(),
/// }]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -97,13 +158,32 @@ pub trait EntityTrait: EntityName {
/// Find by composite key
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_query_results(vec![
/// # vec![
/// # cake_filling::Model {
/// # cake_id: 2,
/// # filling_id: 3,
/// # },
/// # ],
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake_filling};
///
/// # async_std::task::block_on(async {
/// cake_filling::Entity::find_by_id((2, 3)).all(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// assert_eq!(
/// cake_filling::Entity::find_by_id((2, 3)).all(&db).await?,
/// vec![cake_filling::Model {
/// cake_id: 2,
/// filling_id: 3,
/// }]
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -137,8 +217,16 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 15,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
@ -147,8 +235,14 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # async_std::task::block_on(async {
/// cake::Entity::insert(apple).exec(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// let insert_result = cake::Entity::insert(apple).exec(&db).await?;
///
/// assert_eq!(insert_result.last_insert_id, 15);
/// // assert_eq!(insert_result.rows_affected, 1);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -166,8 +260,16 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 28,
/// # rows_affected: 2,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::cake};
///
@ -180,8 +282,14 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # async_std::task::block_on(async {
/// cake::Entity::insert_many(vec![apple, orange]).exec(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// let insert_result = cake::Entity::insert_many(vec![apple, orange]).exec(&db).await?;
///
/// assert_eq!(insert_result.last_insert_id, 28);
/// // assert_eq!(insert_result.rows_affected, 2);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -201,8 +309,16 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
@ -212,8 +328,14 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # async_std::task::block_on(async {
/// fruit::Entity::update(orange).exec(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// assert_eq!(
/// fruit::Entity::update(orange.clone()).exec(&db).await?, // Clone here because we need to assert_eq
/// orange
/// );
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -231,17 +353,30 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 5,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::{Expr, Value}};
///
/// # async_std::task::block_on(async {
/// fruit::Entity::update_many()
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// let update_result = fruit::Entity::update_many()
/// .col_expr(fruit::Column::CakeId, Expr::value(Value::Null))
/// .filter(fruit::Column::Name.contains("Apple"))
/// .exec(&db)
/// .await;
/// .await?;
///
/// assert_eq!(update_result.rows_affected, 5);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -256,8 +391,16 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 1,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
@ -266,8 +409,13 @@ pub trait EntityTrait: EntityName {
/// ..Default::default()
/// };
///
/// # async_std::task::block_on(async {
/// fruit::Entity::delete(orange).exec(&db).await;
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// let delete_result = fruit::Entity::delete(orange).exec(&db).await?;
///
/// assert_eq!(delete_result.rows_affected, 1);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(
@ -285,16 +433,29 @@ pub trait EntityTrait: EntityName {
/// ```
/// # #[cfg(feature = "mock")]
/// # use sea_orm::{MockDatabase, Transaction};
/// # let db = MockDatabase::new().into_connection();
/// # use sea_orm::{error::*, MockDatabase, MockExecResult, Transaction, tests_cfg::*};
/// #
/// # let db = MockDatabase::new()
/// # .append_exec_results(vec![
/// # MockExecResult {
/// # last_insert_id: 0,
/// # rows_affected: 5,
/// # },
/// # ])
/// # .into_connection();
/// #
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit};
///
/// # async_std::task::block_on(async {
/// fruit::Entity::delete_many()
/// # let _: Result<(), DbErr> = async_std::task::block_on(async {
/// #
/// let delete_result = fruit::Entity::delete_many()
/// .filter(fruit::Column::Name.contains("Apple"))
/// .exec(&db)
/// .await;
/// .await?;
///
/// assert_eq!(delete_result.rows_affected, 5);
/// #
/// # Ok(())
/// # });
///
/// assert_eq!(