| 22 | } |
| 23 | |
| 24 | pub async fn insert_teas(db: &DatabaseConnection) -> Result<(), DbErr> { |
| 25 | use teas::*; |
| 26 | |
| 27 | let model = Model { |
| 28 | id: Tea::EverydayTea, |
| 29 | category: None, |
| 30 | color: None, |
| 31 | }; |
| 32 | |
| 33 | assert_eq!( |
| 34 | model, |
| 35 | ActiveModel { |
| 36 | id: Set(Tea::EverydayTea), |
| 37 | category: Set(None), |
| 38 | color: Set(None), |
| 39 | } |
| 40 | .insert(db) |
| 41 | .await? |
| 42 | ); |
| 43 | assert_eq!(model, Entity::find().one(db).await?.unwrap()); |
| 44 | assert_eq!( |
| 45 | model, |
| 46 | Entity::find() |
| 47 | .filter(Column::Id.is_not_null()) |
| 48 | .filter(Column::Category.is_null()) |
| 49 | .filter(Column::Color.is_null()) |
| 50 | .one(db) |
| 51 | .await? |
| 52 | .unwrap() |
| 53 | ); |
| 54 | |
| 55 | // UNIQUE constraint failed |
| 56 | assert!(ActiveModel { |
| 57 | id: Set(Tea::EverydayTea), |
| 58 | category: Set(Some(Category::Big)), |
| 59 | color: Set(Some(Color::Black)), |
| 60 | } |
| 61 | .insert(db) |
| 62 | .await |
| 63 | .is_err()); |
| 64 | |
| 65 | // UNIQUE constraint failed |
| 66 | assert!(Entity::insert(ActiveModel { |
| 67 | id: Set(Tea::EverydayTea), |
| 68 | category: Set(Some(Category::Big)), |
| 69 | color: Set(Some(Color::Black)), |
| 70 | }) |
| 71 | .exec(db) |
| 72 | .await |
| 73 | .is_err()); |
| 74 | |
| 75 | let _ = ActiveModel { |
| 76 | category: Set(Some(Category::Big)), |
| 77 | color: Set(Some(Color::Black)), |
| 78 | ..model.into_active_model() |
| 79 | } |
| 80 | .save(db) |
| 81 | .await?; |