-
-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added delete_by_id * Added an integration test * Added composite key example * Remove extra find_by_id in delete_by_id test * Added unit test with composite primary key
- Loading branch information
1 parent
7a86ac0
commit 12f6661
Showing
3 changed files
with
172 additions
and
1 deletion.
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,57 @@ | ||
pub mod common; | ||
pub use common::{features::*, setup::*, TestContext}; | ||
use sea_orm::{entity::prelude::*, DatabaseConnection, IntoActiveModel}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("delete_by_id_tests").await; | ||
create_tables(&ctx.db).await?; | ||
create_and_delete_applog(&ctx.db).await?; | ||
|
||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn create_and_delete_applog(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
let log1 = applog::Model { | ||
id: 1, | ||
action: "Testing".to_owned(), | ||
json: Json::String("HI".to_owned()), | ||
created_at: "2021-09-17T17:50:20+08:00".parse().unwrap(), | ||
}; | ||
|
||
Applog::insert(log1.clone().into_active_model()) | ||
.exec(db) | ||
.await?; | ||
|
||
let log2 = applog::Model { | ||
id: 2, | ||
action: "Tests".to_owned(), | ||
json: Json::String("HELLO".to_owned()), | ||
created_at: "2022-09-17T17:50:20+08:00".parse().unwrap(), | ||
}; | ||
|
||
Applog::insert(log2.clone().into_active_model()) | ||
.exec(db) | ||
.await?; | ||
|
||
let delete_res = Applog::delete_by_id(2).exec(db).await?; | ||
assert_eq!(delete_res.rows_affected, 1); | ||
|
||
let find_res = Applog::find_by_id(1).all(db).await?; | ||
assert_eq!(find_res, vec![log1]); | ||
|
||
let find_res = Applog::find_by_id(2).all(db).await?; | ||
assert_eq!(find_res, vec![]); | ||
|
||
let delete_res = Applog::delete_by_id(3).exec(db).await?; | ||
assert_eq!(delete_res.rows_affected, 0); | ||
|
||
Ok(()) | ||
} |