Test try_join!
This commit is contained in:
parent
c9047d49e7
commit
b22753bebf
@ -35,7 +35,6 @@ Relying on [SQLx](https://github.com/launchbadge/sqlx), SeaORM is a new library
|
|||||||
// execute multiple queries in parallel
|
// execute multiple queries in parallel
|
||||||
let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
|
let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
|
||||||
futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?;
|
futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?;
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Dynamic
|
2. Dynamic
|
||||||
|
@ -29,19 +29,6 @@ pub trait IntoMockRow {
|
|||||||
fn into_mock_row(self) -> MockRow;
|
fn into_mock_row(self) -> MockRow;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M> IntoMockRow for M
|
|
||||||
where
|
|
||||||
M: ModelTrait,
|
|
||||||
{
|
|
||||||
fn into_mock_row(self) -> MockRow {
|
|
||||||
let mut values = BTreeMap::new();
|
|
||||||
for col in <<M::Entity as EntityTrait>::Column>::iter() {
|
|
||||||
values.insert(col.to_string(), self.get(col));
|
|
||||||
}
|
|
||||||
MockRow { values }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MockDatabase {
|
impl MockDatabase {
|
||||||
pub fn new(db_backend: DbBackend) -> Self {
|
pub fn new(db_backend: DbBackend) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@ -121,6 +108,25 @@ impl MockRow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IntoMockRow for MockRow {
|
||||||
|
fn into_mock_row(self) -> MockRow {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<M> IntoMockRow for M
|
||||||
|
where
|
||||||
|
M: ModelTrait,
|
||||||
|
{
|
||||||
|
fn into_mock_row(self) -> MockRow {
|
||||||
|
let mut values = BTreeMap::new();
|
||||||
|
for col in <<M::Entity as EntityTrait>::Column>::iter() {
|
||||||
|
values.insert(col.to_string(), self.get(col));
|
||||||
|
}
|
||||||
|
MockRow { values }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl IntoMockRow for BTreeMap<&str, Value> {
|
impl IntoMockRow for BTreeMap<&str, Value> {
|
||||||
fn into_mock_row(self) -> MockRow {
|
fn into_mock_row(self) -> MockRow {
|
||||||
MockRow {
|
MockRow {
|
||||||
|
51
src/lib.rs
51
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.
|
//! 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::*};
|
//! # use sea_orm::{DbConn, error::*, entity::*, query::*, tests_cfg::*, DatabaseConnection, DbBackend, MockDatabase, Transaction, IntoMockRow};
|
||||||
//! # async fn function(db: &DbConn) -> Result<(), DbErr> {
|
//! # 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
|
//! // execute multiple queries in parallel
|
||||||
//! let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
|
//! let cakes_and_fruits: (Vec<cake::Model>, Vec<fruit::Model>) =
|
||||||
//! futures::try_join!(Cake::find().all(&db), Fruit::find().all(&db))?;
|
//! 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(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # });
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! 2. Dynamic
|
//! 2. Dynamic
|
||||||
|
Loading…
x
Reference in New Issue
Block a user