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    #[smol_potat::test]
463    async fn test_transaction_1() {
464        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
465
466        db.transaction_async::<_, (), DbErr>(async |txn| {
467            let _1 = cake::Entity::find().one(txn).await;
468            let _2 = fruit::Entity::find().all(txn).await;
469
470            Ok(())
471        })
472        .await
473        .unwrap();
474
475        let _ = cake::Entity::find().all(&db).await;
476
477        assert_eq!(
478            db.into_transaction_log(),
479            [
480                Transaction::many([
481                    Statement::from_string(DbBackend::Postgres, "BEGIN"),
482                    Statement::from_sql_and_values(
483                        DbBackend::Postgres,
484                        r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
485                        [1u64.into()]
486                    ),
487                    Statement::from_sql_and_values(
488                        DbBackend::Postgres,
489                        r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
490                        []
491                    ),
492                    Statement::from_string(DbBackend::Postgres, "COMMIT"),
493                ]),
494                Transaction::from_sql_and_values(
495                    DbBackend::Postgres,
496                    r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
497                    []
498                ),
499            ]
500        );
501    }
502
503    #[smol_potat::test]
504    async fn test_transaction_2() {
505        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
506
507        let result = db
508            .transaction_async::<_, (), MyErr>(async |txn| {
509                let _ = cake::Entity::find().one(txn).await;
510                Err(MyErr("test".to_owned()))
511            })
512            .await;
513
514        match result {
515            Err(TransactionError::Transaction(err)) => {
516                assert_eq!(err, MyErr("test".to_owned()))
517            }
518            _ => unreachable!(),
519        }
520
521        assert_eq!(
522            db.into_transaction_log(),
523            [Transaction::many([
524                Statement::from_string(DbBackend::Postgres, "BEGIN"),
525                Statement::from_sql_and_values(
526                    DbBackend::Postgres,
527                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
528                    [1u64.into()]
529                ),
530                Statement::from_string(DbBackend::Postgres, "ROLLBACK"),
531            ])]
532        );
533    }
534
535    #[smol_potat::test]
536    async fn test_nested_transaction_1() {
537        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
538
539        db.transaction_async::<_, (), DbErr>(async |txn| {
540            let _ = cake::Entity::find().one(txn).await;
541
542            txn.transaction_async::<_, (), DbErr>(async |txn| {
543                let _ = fruit::Entity::find().all(txn).await;
544
545                Ok(())
546            })
547            .await
548            .unwrap();
549
550            Ok(())
551        })
552        .await
553        .unwrap();
554
555        assert_eq!(
556            db.into_transaction_log(),
557            [Transaction::many([
558                Statement::from_string(DbBackend::Postgres, "BEGIN"),
559                Statement::from_sql_and_values(
560                    DbBackend::Postgres,
561                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
562                    [1u64.into()]
563                ),
564                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_1"),
565                Statement::from_sql_and_values(
566                    DbBackend::Postgres,
567                    r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
568                    []
569                ),
570                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_1"),
571                Statement::from_string(DbBackend::Postgres, "COMMIT"),
572            ]),]
573        );
574    }
575
576    #[smol_potat::test]
577    async fn test_nested_transaction_2() {
578        let db = MockDatabase::new(DbBackend::Postgres).into_connection();
579
580        db.transaction_async::<_, (), DbErr>(async |txn| {
581            let _ = cake::Entity::find().one(txn).await;
582
583            txn.transaction_async::<_, (), DbErr>(async |txn| {
584                let _ = fruit::Entity::find().all(txn).await;
585
586                txn.transaction_async::<_, (), DbErr>(async |txn| {
587                    let _ = cake::Entity::find().all(txn).await;
588
589                    Ok(())
590                })
591                .await
592                .unwrap();
593
594                Ok(())
595            })
596            .await
597            .unwrap();
598
599            Ok(())
600        })
601        .await
602        .unwrap();
603
604        assert_eq!(
605            db.into_transaction_log(),
606            [Transaction::many([
607                Statement::from_string(DbBackend::Postgres, "BEGIN"),
608                Statement::from_sql_and_values(
609                    DbBackend::Postgres,
610                    r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#,
611                    [1u64.into()]
612                ),
613                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_1"),
614                Statement::from_sql_and_values(
615                    DbBackend::Postgres,
616                    r#"SELECT "fruit"."id", "fruit"."name", "fruit"."cake_id" FROM "fruit""#,
617                    []
618                ),
619                Statement::from_string(DbBackend::Postgres, "SAVEPOINT savepoint_2"),
620                Statement::from_sql_and_values(
621                    DbBackend::Postgres,
622                    r#"SELECT "cake"."id", "cake"."name" FROM "cake""#,
623                    []
624                ),
625                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_2"),
626                Statement::from_string(DbBackend::Postgres, "RELEASE SAVEPOINT savepoint_1"),
627                Statement::from_string(DbBackend::Postgres, "COMMIT"),
628            ]),]
629        );
630    }
631
632    #[smol_potat::test]
633    #[cfg(feature = "stream")]
634    async fn test_stream_1() -> Result<(), DbErr> {
635        let apple = fruit::Model {
636            id: 1,
637            name: "Apple".to_owned(),
638            cake_id: Some(1),
639        };
640
641        let orange = fruit::Model {
642            id: 2,
643            name: "orange".to_owned(),
644            cake_id: None,
645        };
646
647        let db = MockDatabase::new(DbBackend::Postgres)
648            .append_query_results([[apple.clone(), orange.clone()]])
649            .into_connection();
650
651        let mut stream = fruit::Entity::find().stream(&db).await?;
652
653        assert_eq!(stream.try_next().await?, Some(apple));
654
655        assert_eq!(stream.try_next().await?, Some(orange));
656
657        assert_eq!(stream.try_next().await?, None);
658
659        Ok(())
660    }
661
662    #[smol_potat::test]
663    #[cfg(feature = "stream")]
664    async fn test_stream_2() -> Result<(), DbErr> {
665        use fruit::Entity as Fruit;
666        let db = MockDatabase::new(DbBackend::Postgres)
667            .append_query_results([Vec::<fruit::Model>::new()])
668            .into_connection();
669
670        let mut stream = Fruit::find().stream(&db).await?;
671
672        while let Some(item) = stream.try_next().await? {
673            let _item: fruit::ActiveModel = item.into();
674        }
675
676        Ok(())
677    }
678
679    #[smol_potat::test]
680    #[cfg(feature = "stream")]
681    async fn test_stream_in_transaction() -> Result<(), DbErr> {
682        let apple = fruit::Model {
683            id: 1,
684            name: "Apple".to_owned(),
685            cake_id: Some(1),
686        };
687
688        let orange = fruit::Model {
689            id: 2,
690            name: "orange".to_owned(),
691            cake_id: None,
692        };
693
694        let db = MockDatabase::new(DbBackend::Postgres)
695            .append_query_results([[apple.clone(), orange.clone()]])
696            .into_connection();
697
698        let txn = db.begin().await?;
699
700        if let Ok(mut stream) = fruit::Entity::find().stream(&txn).await {
701            assert_eq!(stream.try_next().await?, Some(apple));
702
703            assert_eq!(stream.try_next().await?, Some(orange));
704
705            assert_eq!(stream.try_next().await?, None);
706
707            // stream will be dropped end of scope
708        }
709
710        txn.commit().await?;
711
712        Ok(())
713    }
714
715    #[smol_potat::test]
716    async fn test_mocked_join() {
717        let row = (
718            cake::Model {
719                id: 1,
720                name: "Apple Cake".to_owned(),
721            },
722            fruit::Model {
723                id: 2,
724                name: "Apple".to_owned(),
725                cake_id: Some(1),
726            },
727        );
728        let mocked_row = row.into_mock_row();
729
730        let a_id = mocked_row.try_get::<i32, _>("A_id");
731        assert!(a_id.is_ok());
732        assert_eq!(1, a_id.unwrap());
733        let b_id = mocked_row.try_get::<i32, _>("B_id");
734        assert!(b_id.is_ok());
735        assert_eq!(2, b_id.unwrap());
736    }
737
738    #[smol_potat::test]
739    async fn test_find_also_related_1() -> Result<(), DbErr> {
740        let db = MockDatabase::new(DbBackend::Postgres)
741            .append_query_results([[(
742                cake::Model {
743                    id: 1,
744                    name: "Apple Cake".to_owned(),
745                },
746                fruit::Model {
747                    id: 2,
748                    name: "Apple".to_owned(),
749                    cake_id: Some(1),
750                },
751            )]])
752            .into_connection();
753
754        assert_eq!(
755            cake::Entity::find()
756                .find_also_related(fruit::Entity)
757                .all(&db)
758                .await?,
759            [(
760                cake::Model {
761                    id: 1,
762                    name: "Apple Cake".to_owned(),
763                },
764                Some(fruit::Model {
765                    id: 2,
766                    name: "Apple".to_owned(),
767                    cake_id: Some(1),
768                })
769            )]
770        );
771
772        assert_eq!(
773            db.into_transaction_log(),
774            [Transaction::from_sql_and_values(
775                DbBackend::Postgres,
776                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""#,
777                []
778            ),]
779        );
780
781        Ok(())
782    }
783
784    #[cfg(feature = "postgres-array")]
785    #[smol_potat::test]
786    async fn test_postgres_array_1() -> Result<(), DbErr> {
787        mod collection {
788            use crate as sea_orm;
789            use crate::entity::prelude::*;
790
791            #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
792            #[sea_orm(table_name = "collection")]
793            pub struct Model {
794                #[sea_orm(primary_key)]
795                pub id: i32,
796                pub integers: Vec<i32>,
797                pub integers_opt: Option<Vec<i32>>,
798            }
799
800            #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
801            pub enum Relation {}
802
803            impl ActiveModelBehavior for ActiveModel {}
804        }
805
806        let db = MockDatabase::new(DbBackend::Postgres)
807            .append_query_results([[
808                collection::Model {
809                    id: 1,
810                    integers: vec![1, 2, 3],
811                    integers_opt: Some(vec![1, 2, 3]),
812                },
813                collection::Model {
814                    id: 2,
815                    integers: vec![],
816                    integers_opt: Some(vec![]),
817                },
818                collection::Model {
819                    id: 3,
820                    integers: vec![3, 1, 4],
821                    integers_opt: None,
822                },
823            ]])
824            .into_connection();
825
826        assert_eq!(
827            collection::Entity::find().all(&db).await?,
828            [
829                collection::Model {
830                    id: 1,
831                    integers: vec![1, 2, 3],
832                    integers_opt: Some(vec![1, 2, 3]),
833                },
834                collection::Model {
835                    id: 2,
836                    integers: vec![],
837                    integers_opt: Some(vec![]),
838                },
839                collection::Model {
840                    id: 3,
841                    integers: vec![3, 1, 4],
842                    integers_opt: None,
843                },
844            ]
845        );
846
847        assert_eq!(
848            db.into_transaction_log(),
849            [Transaction::from_sql_and_values(
850                DbBackend::Postgres,
851                r#"SELECT "collection"."id", "collection"."integers", "collection"."integers_opt" FROM "collection""#,
852                []
853            ),]
854        );
855
856        Ok(())
857    }
858
859    #[smol_potat::test]
860    async fn test_query_err() {
861        let db = MockDatabase::new(DbBackend::MySql)
862            .append_query_errors([query_err("this is a mock query error")])
863            .into_connection();
864
865        assert_eq!(
866            cake::Entity::find().all(&db).await,
867            Err(query_err("this is a mock query error"))
868        );
869    }
870
871    #[smol_potat::test]
872    async fn test_exec_err() {
873        let db = MockDatabase::new(DbBackend::MySql)
874            .append_exec_errors([exec_err("this is a mock exec error")])
875            .into_connection();
876
877        let model = cake::ActiveModel::new();
878
879        assert_eq!(
880            model.save(&db).await,
881            Err(exec_err("this is a mock exec error"))
882        );
883    }
884}