| 47 | |
| 48 | #[cfg(feature = "sqlx-sqlite")] |
| 49 | async fn crud_cake(db: &DbConn) -> Result<(), DbErr> { |
| 50 | let apple = cake::ActiveModel { |
| 51 | name: Set("Apple Pie".to_owned()), |
| 52 | ..Default::default() |
| 53 | }; |
| 54 | |
| 55 | let mut apple = apple.save(db).await?; |
| 56 | |
| 57 | println!(); |
| 58 | println!("Inserted: {apple:?}"); |
| 59 | |
| 60 | assert_eq!( |
| 61 | apple, |
| 62 | cake::ActiveModel { |
| 63 | id: Unchanged(1), |
| 64 | name: Unchanged("Apple Pie".to_owned()), |
| 65 | } |
| 66 | ); |
| 67 | |
| 68 | apple.name = Set("Lemon Tart".to_owned()); |
| 69 | |
| 70 | let apple = apple.save(db).await?; |
| 71 | |
| 72 | println!(); |
| 73 | println!("Updated: {apple:?}"); |
| 74 | |
| 75 | let count = cake::Entity::find().count(db).await?; |
| 76 | |
| 77 | println!(); |
| 78 | println!("Count: {count:?}"); |
| 79 | assert_eq!(count, 1); |
| 80 | |
| 81 | let apple = cake::Entity::find_by_id(1).one(db).await?; |
| 82 | |
| 83 | assert_eq!( |
| 84 | Some(cake::Model { |
| 85 | id: 1, |
| 86 | name: "Lemon Tart".to_owned(), |
| 87 | }), |
| 88 | apple |
| 89 | ); |
| 90 | |
| 91 | let apple: cake::Model = apple.unwrap(); |
| 92 | |
| 93 | let result = apple.delete(db).await?; |
| 94 | |
| 95 | println!(); |
| 96 | println!("Deleted: {result:?}"); |
| 97 | |
| 98 | let apple = cake::Entity::find_by_id(1).one(db).await?; |
| 99 | |
| 100 | assert_eq!(None, apple); |
| 101 | |
| 102 | let count = cake::Entity::find().count(db).await?; |
| 103 | |
| 104 | println!(); |
| 105 | println!("Count: {count:?}"); |
| 106 | assert_eq!(count, 0); |