Skip to main content

sea_orm/database/
mock.rs

1use crate::{
2    DatabaseConnection, DatabaseConnectionType, DbBackend, EntityTrait, ExecResult,
3    ExecResultHolder, Iden, IdenStatic, Iterable, MockDatabaseConnection, MockDatabaseTrait,
4    ModelTrait, QueryResult, QueryResultRow, SelectA, SelectB, Statement, error::*,
5};
6use sea_query::{Value, ValueType, Values};
7use std::{collections::BTreeMap, sync::Arc};
8use tracing::instrument;
9
10/// Scripted in-memory database for unit tests.
11///
12/// Queue up exec and query results with [`append_exec_results`](Self::append_exec_results)
13/// / [`append_query_results`](Self::append_query_results), call
14/// [`into_connection`](Self::into_connection) to get a [`DatabaseConnection`]
15/// you can pass to your code, then inspect what was executed via
16/// [`into_transaction_log`](crate::MockDatabaseConnection::into_transaction_log)
17/// on the connection.
18#[derive(Debug)]
19pub struct MockDatabase {
20    db_backend: DbBackend,
21    transaction: Option<OpenTransaction>,
22    transaction_log: Vec<Transaction>,
23    exec_results: Vec<Result<MockExecResult, DbErr>>,
24    query_results: Vec<Result<Vec<MockRow>, DbErr>>,
25}
26
27/// Canned [`ExecResult`](crate::ExecResult)-equivalent returned by a
28/// [`MockDatabase`] for non-`SELECT` statements.
29#[derive(Clone, Debug, Default)]
30pub struct MockExecResult {
31    /// Value reported by [`ExecResult::last_insert_id`](crate::ExecResult::last_insert_id).
32    pub last_insert_id: u64,
33    /// Value reported by [`ExecResult::rows_affected`](crate::ExecResult::rows_affected).
34    pub rows_affected: u64,
35}
36
37/// A single canned row returned by a [`MockDatabase`] — a name → value map
38/// matching the columns the query selected.
39#[derive(Clone, Debug)]
40pub struct MockRow {
41    /// Cell values keyed by column name.
42    pub(crate) values: BTreeMap<String, Value>,
43}
44
45/// Conversion into a [`MockRow`]. Implemented for `Model` (via
46/// `#[derive(DeriveModel)]`) and for `BTreeMap<String, Value>` so you can
47/// hand either to [`MockDatabase::append_query_results`].
48pub trait IntoMockRow {
49    /// Build the row.
50    fn into_mock_row(self) -> MockRow;
51}
52
53/// An in-progress transaction recorded by a [`MockDatabase`]. Once committed
54/// or rolled back, it becomes a [`Transaction`] in the transaction log.
55#[derive(Debug)]
56pub struct OpenTransaction {
57    stmts: Vec<Statement>,
58    transaction_depth: usize,
59}
60
61/// A completed transaction recorded by a [`MockDatabase`] — the ordered list
62/// of statements executed against it. Compare against expected SQL in tests
63/// via [`Transaction::from_sql_and_values`](crate::Transaction::from_sql_and_values).
64#[derive(Debug, Clone, PartialEq)]
65pub struct Transaction {
66    stmts: Vec<Statement>,
67}
68
69impl MockDatabase {
70    /// Instantiate a mock database with a [DbBackend] to simulate real
71    /// world SQL databases
72    pub fn new(db_backend: DbBackend) -> Self {
73        Self {
74            db_backend,
75            transaction: None,
76            transaction_log: Vec::new(),
77            exec_results: Vec::new(),
78            query_results: Vec::new(),
79        }
80    }
81
82    /// Create a database connection
83    pub fn into_connection(self) -> DatabaseConnection {
84        DatabaseConnectionType::MockDatabaseConnection(Arc::new(MockDatabaseConnection::new(self)))
85            .into()
86    }
87
88    /// Add some [MockExecResult]s to `exec_results`
89    pub fn append_exec_results<I>(mut self, vec: I) -> Self
90    where
91        I: IntoIterator<Item = MockExecResult>,
92    {
93        self.exec_results.extend(vec.into_iter().map(Result::Ok));
94        self
95    }
96
97    /// Add some Values to `query_results`
98    pub fn append_query_results<T, I, II>(mut self, vec: II) -> Self
99    where
100        T: IntoMockRow,
101        I: IntoIterator<Item = T>,
102        II: IntoIterator<Item = I>,
103    {
104        for row in vec.into_iter() {
105            let row = row.into_iter().map(|vec| Ok(vec.into_mock_row())).collect();
106            self.query_results.push(row);
107        }
108        self
109    }
110
111    /// Add some [DbErr]s to `exec_results`
112    pub fn append_exec_errors<I>(mut self, vec: I) -> Self
113    where
114        I: IntoIterator<Item = DbErr>,
115    {
116        self.exec_results.extend(vec.into_iter().map(Result::Err));
117        self
118    }
119
120    /// Add some [DbErr]s to `query_results`
121    pub fn append_query_errors<I>(mut self, vec: I) -> Self
122    where
123        I: IntoIterator<Item = DbErr>,
124    {
125        self.query_results.extend(vec.into_iter().map(Result::Err));
126        self
127    }
128}
129
130impl MockDatabaseTrait for MockDatabase {
131    #[instrument(level = "trace", skip(statement))]
132    fn execute(&mut self, counter: usize, statement: Statement) -> Result<ExecResult, DbErr> {
133        if let Some(transaction) = &mut self.transaction {
134            transaction.push(statement);
135        } else {
136            self.transaction_log.push(Transaction::one(statement));
137        }
138        if counter < self.exec_results.len() {
139            match std::mem::replace(
140                &mut self.exec_results[counter],
141                Err(exec_err("this value has been consumed already")),
142            ) {
143                Ok(result) => Ok(ExecResult {
144                    result: ExecResultHolder::Mock(result),
145                }),
146                Err(err) => Err(err),
147            }
148        } else {
149            Err(exec_err("`exec_results` buffer is empty"))
150        }
151    }
152
153    #[instrument(level = "trace", skip(statement))]
154    fn query(&mut self, counter: usize, statement: Statement) -> Result<Vec<QueryResult>, DbErr> {
155        if let Some(transaction) = &mut self.transaction {
156            transaction.push(statement);
157        } else {
158            self.transaction_log.push(Transaction::one(statement));
159        }
160        if counter < self.query_results.len() {
161            match std::mem::replace(
162                &mut self.query_results[counter],
163                Err(query_err("this value has been consumed already")),
164            ) {
165                Ok(result) => Ok(result
166                    .into_iter()
167                    .map(|row| QueryResult {
168                        row: QueryResultRow::Mock(row),
169                    })
170                    .collect()),
171                Err(err) => Err(err),
172            }
173        } else {
174            Err(query_err("`query_results` buffer is empty."))
175        }
176    }
177
178    #[instrument(level = "trace")]
179    fn begin(&mut self) {
180        match self.transaction.as_mut() {
181            Some(transaction) => transaction.begin_nested(self.db_backend),
182            None => self.transaction = Some(OpenTransaction::init()),
183        }
184    }
185
186    #[instrument(level = "trace")]
187    fn commit(&mut self) {
188        match self.transaction.as_mut() {
189            Some(transaction) => {
190                if transaction.commit(self.db_backend) {
191                    if let Some(transaction) = self.transaction.take() {
192                        self.transaction_log.push(transaction.into_transaction());
193                    }
194                }
195            }
196            None => panic!("There is no open transaction to commit"),
197        }
198    }
199
200    #[instrument(level = "trace")]
201    fn rollback(&mut self) {
202        match self.transaction.as_mut() {
203            Some(transaction) => {
204                if transaction.rollback(self.db_backend) {
205                    if let Some(transaction) = self.transaction.take() {
206                        self.transaction_log.push(transaction.into_transaction());
207                    }
208                }
209            }
210            None => panic!("There is no open transaction to rollback"),
211        }
212    }
213
214    fn drain_transaction_log(&mut self) -> Vec<Transaction> {
215        std::mem::take(&mut self.transaction_log)
216    }
217
218    fn get_database_backend(&self) -> DbBackend {
219        self.db_backend
220    }
221
222    fn ping(&self) -> Result<(), DbErr> {
223        Ok(())
224    }
225}
226
227impl MockRow {
228    /// Get a value from the [MockRow]
229    pub fn try_get<T, I: crate::ColIdx>(&self, index: I) -> Result<T, DbErr>
230    where
231        T: ValueType,
232    {
233        if let Some(index) = index.as_str() {
234            T::try_from(
235                self.values
236                    .get(index)
237                    .ok_or_else(|| query_err(format!("No column for ColIdx {index:?}")))?
238                    .clone(),
239            )
240            .map_err(type_err)
241        } else if let Some(index) = index.as_usize() {
242            let (_, value) = self
243                .values
244                .iter()
245                .nth(*index)
246                .ok_or_else(|| query_err(format!("Column at index {index} not found")))?;
247            T::try_from(value.clone()).map_err(type_err)
248        } else {
249            unreachable!("Missing ColIdx implementation for MockRow");
250        }
251    }
252
253    /// An iterator over the keys and values of a mock row
254    pub fn into_column_value_tuples(self) -> impl Iterator<Item = (String, Value)> {
255        self.values.into_iter()
256    }
257}
258
259impl IntoMockRow for MockRow {
260    fn into_mock_row(self) -> MockRow {
261        self
262    }
263}
264
265impl<M> IntoMockRow for M
266where
267    M: ModelTrait,
268{
269    fn into_mock_row(self) -> MockRow {
270        let mut values = BTreeMap::new();
271        for col in <<M::Entity as EntityTrait>::Column>::iter() {
272            values.insert(col.to_string(), self.get(col));
273        }
274        MockRow { values }
275    }
276}
277
278impl<M, N> IntoMockRow for (M, N)
279where
280    M: ModelTrait,
281    N: ModelTrait,
282{
283    fn into_mock_row(self) -> MockRow {
284        let mut mapped_join = BTreeMap::new();
285
286        for column in <<M as ModelTrait>::Entity as EntityTrait>::Column::iter() {
287            mapped_join.insert(
288                format!("{}{}", SelectA.as_str(), column.as_str()),
289                self.0.get(column),
290            );
291        }
292        for column in <<N as ModelTrait>::Entity as EntityTrait>::Column::iter() {
293            mapped_join.insert(
294                format!("{}{}", SelectB.as_str(), column.as_str()),
295                self.1.get(column),
296            );
297        }
298
299        mapped_join.into_mock_row()
300    }
301}
302
303impl<M, N> IntoMockRow for (M, Option<N>)
304where
305    M: ModelTrait,
306    N: ModelTrait,
307{
308    fn into_mock_row(self) -> MockRow {
309        let mut mapped_join = BTreeMap::new();
310
311        for column in <<M as ModelTrait>::Entity as EntityTrait>::Column::iter() {
312            mapped_join.insert(
313                format!("{}{}", SelectA.as_str(), column.as_str()),
314                self.0.get(column),
315            );
316        }
317        if let Some(b_entity) = self.1 {
318            for column in <<N as ModelTrait>::Entity as EntityTrait>::Column::iter() {
319                mapped_join.insert(
320                    format!("{}{}", SelectB.as_str(), column.as_str()),
321                    b_entity.get(column),
322                );
323            }
324        }
325
326        mapped_join.into_mock_row()
327    }
328}
329
330impl<T> IntoMockRow for BTreeMap<T, Value>
331where
332    T: Into<String>,
333{
334    fn into_mock_row(self) -> MockRow {
335        MockRow {
336            values: self.into_iter().map(|(k, v)| (k.into(), v)).collect(),
337        }
338    }
339}
340
341impl Transaction {
342    /// Get the [Value]s from s raw SQL statement depending on the [DatabaseBackend](crate::DatabaseBackend)
343    pub fn from_sql_and_values<I, T>(db_backend: DbBackend, sql: T, values: I) -> Self
344    where
345        I: IntoIterator<Item = Value>,
346        T: Into<String>,
347    {
348        Self::one(Statement::from_string_values_tuple(
349            db_backend,
350            (sql, Values(values.into_iter().collect())),
351        ))
352    }
353
354    /// Create a Transaction with one statement
355    pub fn one(stmt: Statement) -> Self {
356        Self { stmts: vec![stmt] }
357    }
358
359    /// Create a Transaction with many statements
360    pub fn many<I>(stmts: I) -> Self
361    where
362        I: IntoIterator<Item = Statement>,
363    {
364        Self {
365            stmts: stmts.into_iter().collect(),
366        }
367    }
368
369    /// Wrap each Statement as a single-statement Transaction
370    pub fn wrap<I>(stmts: I) -> Vec<Self>
371    where
372        I: IntoIterator<Item = Statement>,
373    {
374        stmts.into_iter().map(Self::one).collect()
375    }
376
377    /// Get the list of statements
378    pub fn statements(&self) -> &[Statement] {
379        &self.stmts
380    }
381}
382
383impl OpenTransaction {
384    fn init() -> Self {
385        Self {
386            stmts: vec![Statement::from_string(DbBackend::Postgres, "BEGIN")],
387            transaction_depth: 0,
388        }
389    }
390
391    fn begin_nested(&mut self, db_backend: DbBackend) {
392        self.transaction_depth += 1;
393        self.push(Statement::from_string(
394            db_backend,
395            format!("SAVEPOINT savepoint_{}", self.transaction_depth),
396        ));
397    }
398
399    fn commit(&mut self, db_backend: DbBackend) -> bool {
400        if self.transaction_depth == 0 {
401            self.push(Statement::from_string(db_backend, "COMMIT"));
402            true
403        } else {
404            self.push(Statement::from_string(
405                db_backend,
406                format!("RELEASE SAVEPOINT savepoint_{}", self.transaction_depth),
407            ));
408            self.transaction_depth -= 1;
409            false
410        }
411    }
412
413    fn rollback(&mut self, db_backend: DbBackend) -> bool {
414        if self.transaction_depth == 0 {
415            self.push(Statement::from_string(db_backend, "ROLLBACK"));
416            true
417        } else {
418            self.push(Statement::from_string(
419                db_backend,
420                format!("ROLLBACK TO SAVEPOINT savepoint_{}", self.transaction_depth),
421            ));
422            self.transaction_depth -= 1;
423            false
424        }
425    }
426
427    fn push(&mut self, stmt: Statement) {
428        self.stmts.push(stmt);
429    }
430
431    fn into_transaction(self) -> Transaction {
432        match self.transaction_depth {
433            0 => Transaction { stmts: self.stmts },
434            _ => panic!("There is uncommitted nested transaction"),
435        }
436    }
437}
438
439#[cfg(test)]
440#[cfg(feature = "mock")]
441mod tests {
442    #[cfg(feature = "sync")]
443    use crate::util::StreamShim;
444    use crate::{
445        DbBackend, DbErr, IntoMockRow, MockDatabase, Statement, Transaction, TransactionError,
446        TransactionTrait, entity::*, error::*, tests_cfg::*,
447    };
448    use futures_util::TryStreamExt;
449    use pretty_assertions::assert_eq;
450
451    #[derive(Debug, PartialEq, Eq)]
452    pub struct MyErr(String);
453
454    impl std::error::Error for MyErr {}
455
456    impl std::fmt::Display for MyErr {
457        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
458            write!(f, "{}", self.0.as_str())
459        }
460    }
461
462    #[test]
463    fn test_transaction_1() {
464        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
465
466        db.transaction::<_, (), DbErr>(|txn| {
467            let _1 = cake::Entity::find().one(txn);
468            let _2 = fruit::Entity::find().all(txn);
469
470            Ok(())
471        })
472        .unwrap();
473
474        let _ = cake::Entity::find().all(&db);
475
476        assert_eq!(
477            db.into_transaction_log(),
478            [
479                Transaction::many([
480                    Statement::from_string(DbBackend::Postgres, "BEGIN"),
481                    Statement::from_sql_and_values(
482                        DbBackend::Postgres,
483                        r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
484                        [1u64.into()]
485                    ),
486                    Statement::from_sql_and_values(
487                        DbBackend::Postgres,
488                        r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
489                        []
490                    ),
491                    Statement::from_string(DbBackend::Postgres, "COMMIT"),
492                ]),
493                Transaction::from_sql_and_values(
494                    DbBackend::Postgres,
495                    r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
496                    []
497                ),
498            ]
499        );
500    }
501
502    #[test]
503    fn test_transaction_2() {
504        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
505
506        let result = db.transaction::<_, (), MyErr>(|txn| {
507            let _ = cake::Entity::find().one(txn);
508            Err(MyErr("test".to_owned()))
509        });
510
511        match result {
512            Err(TransactionError::Transaction(err)) => {
513                assert_eq!(err, MyErr("test".to_owned()))
514            }
515            _ => unreachable!(),
516        }
517
518        assert_eq!(
519            db.into_transaction_log(),
520            [Transaction::many([
521                Statement::from_string(DbBackend::Postgres, "BEGIN"),
522                Statement::from_sql_and_values(
523                    DbBackend::Postgres,
524                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
525                    [1u64.into()]
526                ),
527                Statement::from_string(DbBackend::Postgres, "ROLLBACK"),
528            ])]
529        );
530    }
531
532    #[test]
533    fn test_nested_transaction_1() {
534        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
535
536        db.transaction::<_, (), DbErr>(|txn| {
537            let _ = cake::Entity::find().one(txn);
538
539            txn.transaction::<_, (), DbErr>(|txn| {
540                let _ = fruit::Entity::find().all(txn);
541
542                Ok(())
543            })
544            .unwrap();
545
546            Ok(())
547        })
548        .unwrap();
549
550        assert_eq!(
551            db.into_transaction_log(),
552            [Transaction::many([
553                Statement::from_string(DbBackend::Postgres, "BEGIN"),
554                Statement::from_sql_and_values(
555                    DbBackend::Postgres,
556                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
557                    [1u64.into()]
558                ),
559                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_1"),
560                Statement::from_sql_and_values(
561                    DbBackend::Postgres,
562                    r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
563                    []
564                ),
565                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_1"),
566                Statement::from_string(DbBackend::Postgres, "COMMIT"),
567            ]),]
568        );
569    }
570
571    #[test]
572    fn test_nested_transaction_2() {
573        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
574
575        db.transaction::<_, (), DbErr>(|txn| {
576            let _ = cake::Entity::find().one(txn);
577
578            txn.transaction::<_, (), DbErr>(|txn| {
579                let _ = fruit::Entity::find().all(txn);
580
581                txn.transaction::<_, (), DbErr>(|txn| {
582                    let _ = cake::Entity::find().all(txn);
583
584                    Ok(())
585                })
586                .unwrap();
587
588                Ok(())
589            })
590            .unwrap();
591
592            Ok(())
593        })
594        .unwrap();
595
596        assert_eq!(
597            db.into_transaction_log(),
598            [Transaction::many([
599                Statement::from_string(DbBackend::Postgres, "BEGIN"),
600                Statement::from_sql_and_values(
601                    DbBackend::Postgres,
602                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
603                    [1u64.into()]
604                ),
605                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_1"),
606                Statement::from_sql_and_values(
607                    DbBackend::Postgres,
608                    r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
609                    []
610                ),
611                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_2"),
612                Statement::from_sql_and_values(
613                    DbBackend::Postgres,
614                    r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
615                    []
616                ),
617                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_2"),
618                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_1"),
619                Statement::from_string(DbBackend::Postgres, "COMMIT"),
620            ]),]
621        );
622    }
623
624    #[test]
625    #[cfg(feature = "stream")]
626    fn test_stream_1() -> Result<(), DbErr> {
627        let apple = fruit::Model {
628            id: 1,
629            name: "Apple".to_owned(),
630            cake_id: Some(1),
631        };
632
633        let orange = fruit::Model {
634            id: 2,
635            name: "orange".to_owned(),
636            cake_id: None,
637        };
638
639        let db = MockDatabase::new(DbBackend::Postgres)
640            .append_query_results([[apple.clone(), orange.clone()]])
641            .into_connection();
642
643        let mut stream = fruit::Entity::find().stream(&db)?;
644
645        assert_eq!(stream.try_next()?, Some(apple));
646
647        assert_eq!(stream.try_next()?, Some(orange));
648
649        assert_eq!(stream.try_next()?, None);
650
651        Ok(())
652    }
653
654    #[test]
655    #[cfg(feature = "stream")]
656    fn test_stream_2() -> Result<(), DbErr> {
657        use fruit::Entity as Fruit;
658        let db = MockDatabase::new(DbBackend::Postgres)
659            .append_query_results([Vec::<fruit::Model>::new()])
660            .into_connection();
661
662        let mut stream = Fruit::find().stream(&db)?;
663
664        while let Some(item) = stream.try_next()? {
665            let _item: fruit::ActiveModel = item.into();
666        }
667
668        Ok(())
669    }
670
671    #[test]
672    #[cfg(feature = "stream")]
673    fn test_stream_in_transaction() -> Result<(), DbErr> {
674        let apple = fruit::Model {
675            id: 1,
676            name: "Apple".to_owned(),
677            cake_id: Some(1),
678        };
679
680        let orange = fruit::Model {
681            id: 2,
682            name: "orange".to_owned(),
683            cake_id: None,
684        };
685
686        let db = MockDatabase::new(DbBackend::Postgres)
687            .append_query_results([[apple.clone(), orange.clone()]])
688            .into_connection();
689
690        let txn = db.begin()?;
691
692        if let Ok(mut stream) = fruit::Entity::find().stream(&txn) {
693            assert_eq!(stream.try_next()?, Some(apple));
694
695            assert_eq!(stream.try_next()?, Some(orange));
696
697            assert_eq!(stream.try_next()?, None);
698
699            // stream will be dropped end of scope
700        }
701
702        txn.commit()?;
703
704        Ok(())
705    }
706
707    #[test]
708    fn test_mocked_join() {
709        let row = (
710            cake::Model {
711                id: 1,
712                name: "Apple Cake".to_owned(),
713            },
714            fruit::Model {
715                id: 2,
716                name: "Apple".to_owned(),
717                cake_id: Some(1),
718            },
719        );
720        let mocked_row = row.into_mock_row();
721
722        let a_id = mocked_row.try_get::<i32, _>("A_id");
723        assert!(a_id.is_ok());
724        assert_eq!(1, a_id.unwrap());
725        let b_id = mocked_row.try_get::<i32, _>("B_id");
726        assert!(b_id.is_ok());
727        assert_eq!(2, b_id.unwrap());
728    }
729
730    #[test]
731    fn test_find_also_related_1() -> Result<(), DbErr> {
732        let db = MockDatabase::new(DbBackend::Postgres)
733            .append_query_results([[(
734                cake::Model {
735                    id: 1,
736                    name: "Apple Cake".to_owned(),
737                },
738                fruit::Model {
739                    id: 2,
740                    name: "Apple".to_owned(),
741                    cake_id: Some(1),
742                },
743            )]])
744            .into_connection();
745
746        assert_eq!(
747            cake::Entity::find()
748                .find_also_related(fruit::Entity)
749                .all(&db)?,
750            [(
751                cake::Model {
752                    id: 1,
753                    name: "Apple Cake".to_owned(),
754                },
755                Some(fruit::Model {
756                    id: 2,
757                    name: "Apple".to_owned(),
758                    cake_id: Some(1),
759                })
760            )]
761        );
762
763        assert_eq!(
764            db.into_transaction_log(),
765            [Transaction::from_sql_and_values(
766                DbBackend::Postgres,
767                r#"SELECT "cake"."id" AS "A_id", "cake"."name" AS "A_name", "fruit"."id" AS "B_id", "fruit"."name" AS "B_name", "fruit"."cake_id" AS "B_cake_id" FROM "cake" LEFT JOIN "fruit" ON "cake"."id" = "fruit"."cake_id""#,
768                []
769            ),]
770        );
771
772        Ok(())
773    }
774
775    #[cfg(feature = "postgres-array")]
776    #[test]
777    fn test_postgres_array_1() -> Result<(), DbErr> {
778        mod collection {
779            use crate as sea_orm;
780            use crate::entity::prelude::*;
781
782            #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
783            #[sea_orm(table_name = "collection")]
784            pub struct Model {
785                #[sea_orm(primary_key)]
786                pub id: i32,
787                pub integers: Vec<i32>,
788                pub integers_opt: Option<Vec<i32>>,
789            }
790
791            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
792            pub enum Relation {}
793
794            impl ActiveModelBehavior for ActiveModel {}
795        }
796
797        let db = MockDatabase::new(DbBackend::Postgres)
798            .append_query_results([[
799                collection::Model {
800                    id: 1,
801                    integers: vec![1, 2, 3],
802                    integers_opt: Some(vec![1, 2, 3]),
803                },
804                collection::Model {
805                    id: 2,
806                    integers: vec![],
807                    integers_opt: Some(vec![]),
808                },
809                collection::Model {
810                    id: 3,
811                    integers: vec![3, 1, 4],
812                    integers_opt: None,
813                },
814            ]])
815            .into_connection();
816
817        assert_eq!(
818            collection::Entity::find().all(&db)?,
819            [
820                collection::Model {
821                    id: 1,
822                    integers: vec![1, 2, 3],
823                    integers_opt: Some(vec![1, 2, 3]),
824                },
825                collection::Model {
826                    id: 2,
827                    integers: vec![],
828                    integers_opt: Some(vec![]),
829                },
830                collection::Model {
831                    id: 3,
832                    integers: vec![3, 1, 4],
833                    integers_opt: None,
834                },
835            ]
836        );
837
838        assert_eq!(
839            db.into_transaction_log(),
840            [Transaction::from_sql_and_values(
841                DbBackend::Postgres,
842                r#"SELECT "collection"."id", "collection"."integers", "collection"."integers_opt" FROM "collection""#,
843                []
844            ),]
845        );
846
847        Ok(())
848    }
849
850    #[test]
851    fn test_query_err() {
852        let db = MockDatabase::new(DbBackend::MySql)
853            .append_query_errors([query_err("this is a mock query error")])
854            .into_connection();
855
856        assert_eq!(
857            cake::Entity::find().all(&db),
858            Err(query_err("this is a mock query error"))
859        );
860    }
861
862    #[test]
863    fn test_exec_err() {
864        let db = MockDatabase::new(DbBackend::MySql)
865            .append_exec_errors([exec_err("this is a mock exec error")])
866            .into_connection();
867
868        let model = cake::ActiveModel::new();
869
870        assert_eq!(model.save(&db), Err(exec_err("this is a mock exec error")));
871    }
872}