-
-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
pub mod common; | ||
|
||
pub use common::{features::*, setup::*, TestContext}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(any( | ||
feature = "sqlx-mysql", | ||
feature = "sqlx-sqlite", | ||
feature = "sqlx-postgres" | ||
))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("byte_primary_key_tests").await; | ||
create_tables(&ctx.db).await?; | ||
create_and_update(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn create_and_update(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
use common::features::byte_primary_key::*; | ||
|
||
let model = Model { | ||
id: vec![1, 2, 3], | ||
value: "First Row".to_owned(), | ||
}; | ||
|
||
let res = Entity::insert(model.clone().into_active_model()) | ||
.exec(db) | ||
.await?; | ||
|
||
assert_eq!(Entity::find().one(db).await?, Some(model.clone())); | ||
|
||
assert_eq!(res.last_insert_id, model.id); | ||
|
||
let updated_active_model = ActiveModel { | ||
value: Set("First Row (Updated)".to_owned()), | ||
..model.clone().into_active_model() | ||
}; | ||
|
||
let update_res = Entity::update(updated_active_model.clone()) | ||
.filter(Column::Id.eq(vec![1, 2, 3, 4])) | ||
.exec(db) | ||
.await; | ||
|
||
assert_eq!( | ||
update_res, | ||
Err(DbErr::RecordNotFound( | ||
"None of the database rows are affected".to_owned() | ||
)) | ||
); | ||
|
||
let update_res = Entity::update(updated_active_model.clone()) | ||
.filter(Column::Id.eq(vec![1, 2, 3])) | ||
.exec(db) | ||
.await?; | ||
|
||
assert_eq!(update_res, updated_active_model); | ||
|
||
assert_eq!( | ||
Entity::find() | ||
.filter(Column::Id.eq(vec![1, 2, 3])) | ||
.one(db) | ||
.await?, | ||
Some(Model { | ||
id: vec![1, 2, 3], | ||
value: "First Row (Updated)".to_owned(), | ||
}) | ||
); | ||
|
||
Ok(()) | ||
} |
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,14 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "byte_primary_key")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: Vec<u8>, | ||
pub value: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
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