|
| 1 | +use rs_cms_entity::user; |
| 2 | +use sea_orm::{DatabaseConnection, DbErr}; |
| 3 | + |
| 4 | +struct UserDao { |
| 5 | + conn: DatabaseConnection, |
| 6 | +} |
| 7 | + |
| 8 | +impl UserDao { |
| 9 | + fn init(&mut self, mut conn: DatabaseConnection) { |
| 10 | + self.conn = conn |
| 11 | + } |
| 12 | + |
| 13 | + fn get_user(&self) -> Result<user::Model, DbErr> { |
| 14 | + todo!() |
| 15 | + } |
| 16 | + |
| 17 | + fn add_user(&mut self) {} |
| 18 | +} |
| 19 | + |
| 20 | +#[cfg(test)] |
| 21 | +mod tests { |
| 22 | + use sea_orm::{ |
| 23 | + entity::{prelude::*, *}, |
| 24 | + tests_cfg::*, |
| 25 | + DatabaseBackend, MockDatabase, Transaction, |
| 26 | + }; |
| 27 | + |
| 28 | + #[async_std::test] |
| 29 | + async fn test_find_cake() -> Result<(), DbErr> { |
| 30 | + // Create MockDatabase with mock query results |
| 31 | + let db = MockDatabase::new(DatabaseBackend::Postgres) |
| 32 | + .append_query_results(vec![ |
| 33 | + // First query result |
| 34 | + vec![cake::Model { id: 1, name: "New York Cheese".to_owned() }], |
| 35 | + // Second query result |
| 36 | + vec![ |
| 37 | + cake::Model { id: 1, name: "New York Cheese".to_owned() }, |
| 38 | + cake::Model { id: 2, name: "Chocolate Forest".to_owned() }, |
| 39 | + ], |
| 40 | + ]) |
| 41 | + .append_query_results(vec![ |
| 42 | + // Third query result |
| 43 | + vec![( |
| 44 | + cake::Model { id: 1, name: "Apple Cake".to_owned() }, |
| 45 | + fruit::Model { id: 2, name: "Apple".to_owned(), cake_id: Some(1) }, |
| 46 | + )], |
| 47 | + ]) |
| 48 | + .into_connection(); |
| 49 | + |
| 50 | + // Find a cake from MockDatabase |
| 51 | + // Return the first query result |
| 52 | + assert_eq!( |
| 53 | + cake::Entity::find().one(&db).await?, |
| 54 | + Some(cake::Model { id: 1, name: "New York Cheese".to_owned() }) |
| 55 | + ); |
| 56 | + |
| 57 | + // Find all cakes from MockDatabase |
| 58 | + // Return the second query result |
| 59 | + assert_eq!( |
| 60 | + cake::Entity::find().all(&db).await?, |
| 61 | + vec![ |
| 62 | + cake::Model { id: 1, name: "New York Cheese".to_owned() }, |
| 63 | + cake::Model { id: 2, name: "Chocolate Forest".to_owned() }, |
| 64 | + ] |
| 65 | + ); |
| 66 | + |
| 67 | + // Find all cakes with its related fruits |
| 68 | + assert_eq!( |
| 69 | + cake::Entity::find().find_also_related(fruit::Entity).all(&db).await?, |
| 70 | + vec![( |
| 71 | + cake::Model { id: 1, name: "Apple Cake".to_owned() }, |
| 72 | + Some(fruit::Model { id: 2, name: "Apple".to_owned(), cake_id: Some(1) }) |
| 73 | + )] |
| 74 | + ); |
| 75 | + |
| 76 | + // Checking transaction log |
| 77 | + assert_eq!( |
| 78 | + db.into_transaction_log(), |
| 79 | + vec![ |
| 80 | + Transaction::from_sql_and_values( |
| 81 | + DatabaseBackend::Postgres, |
| 82 | + r#"SELECT "cake"."id", "cake"."name" FROM "cake" LIMIT $1"#, |
| 83 | + vec![1u64.into()] |
| 84 | + ), |
| 85 | + Transaction::from_sql_and_values( |
| 86 | + DatabaseBackend::Postgres, |
| 87 | + r#"SELECT "cake"."id", "cake"."name" FROM "cake""#, |
| 88 | + vec![] |
| 89 | + ), |
| 90 | + Transaction::from_sql_and_values( |
| 91 | + DatabaseBackend::Postgres, |
| 92 | + 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""#, |
| 93 | + vec![] |
| 94 | + ), |
| 95 | + ] |
| 96 | + ); |
| 97 | + |
| 98 | + Ok(()) |
| 99 | + } |
| 100 | +} |
0 commit comments