-
-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make insert statement not panic when inserting nothing (#1708)
* end-of-day commit (WIP) * progress commit (WIP) * refactored and added InsertAttempt * async asjusting * completed implementation for insertAttempt in execution Added in tests for insertAttempt * updated wording for new INSERT type * removed InsertTrait
- Loading branch information
Showing
3 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
pub mod common; | ||
mod crud; | ||
|
||
pub use common::{bakery_chain::*, setup::*, TestContext}; | ||
pub use sea_orm::{ | ||
entity::*, error::DbErr, tests_cfg, DatabaseConnection, DbBackend, EntityName, ExecResult, | ||
}; | ||
|
||
pub use crud::*; | ||
// use common::bakery_chain::*; | ||
use sea_orm::{DbConn, TryInsertResult}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() { | ||
let ctx = TestContext::new("bakery_chain_empty_insert_tests").await; | ||
create_tables(&ctx.db).await.unwrap(); | ||
test(&ctx.db).await; | ||
ctx.delete().await; | ||
} | ||
|
||
pub async fn test(db: &DbConn) { | ||
let seaside_bakery = bakery::ActiveModel { | ||
name: Set("SeaSide Bakery".to_owned()), | ||
profit_margin: Set(10.4), | ||
..Default::default() | ||
}; | ||
|
||
let res = Bakery::insert(seaside_bakery) | ||
.on_empty_do_nothing() | ||
.exec(db) | ||
.await; | ||
|
||
assert!(matches!(res, TryInsertResult::Inserted(_))); | ||
|
||
let empty_iterator = [bakery::ActiveModel { | ||
name: Set("SeaSide Bakery".to_owned()), | ||
profit_margin: Set(10.4), | ||
..Default::default() | ||
}] | ||
.into_iter() | ||
.filter(|_| false); | ||
|
||
let empty_insert = Bakery::insert_many(empty_iterator) | ||
.on_empty_do_nothing() | ||
.exec(db) | ||
.await; | ||
|
||
assert!(matches!(empty_insert, TryInsertResult::Empty)); | ||
} |