-
-
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.
- Loading branch information
Showing
4 changed files
with
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use sea_orm::entity::prelude::*; | ||
use sea_orm::{ | ||
sea_query::{ArrayType, ColumnType, SeaRc, ValueType}, | ||
TryGetError, TryGetable, | ||
}; | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] | ||
#[sea_orm(table_name = "event_trigger")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i32, | ||
pub events: Events, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct Event(pub String); | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq)] | ||
pub struct Events(pub Vec<Event>); | ||
|
||
impl From<Events> for Value { | ||
fn from(events: Events) -> Self { | ||
let Events(events) = events; | ||
Value::Array( | ||
ArrayType::String, | ||
Some(Box::new( | ||
events | ||
.into_iter() | ||
.map(|Event(s)| Value::String(Some(Box::new(s)))) | ||
.collect(), | ||
)), | ||
) | ||
} | ||
} | ||
|
||
impl TryGetable for Events { | ||
fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result<Self, TryGetError> { | ||
let vec: Vec<String> = res.try_get(pre, col).map_err(TryGetError::DbErr)?; | ||
Ok(Events(vec.into_iter().map(Event).collect())) | ||
} | ||
} | ||
|
||
impl ValueType for Events { | ||
fn try_from(v: Value) -> Result<Self, sea_query::ValueTypeErr> { | ||
let value: Option<Vec<String>> = | ||
v.expect("This Value::Array should consist of Value::String"); | ||
let vec = match value { | ||
Some(v) => v.into_iter().map(Event).collect(), | ||
None => vec![], | ||
}; | ||
Ok(Events(vec)) | ||
} | ||
|
||
fn type_name() -> String { | ||
stringify!(Events).to_owned() | ||
} | ||
|
||
fn array_type() -> ArrayType { | ||
ArrayType::String | ||
} | ||
|
||
fn column_type() -> ColumnType { | ||
ColumnType::Array(SeaRc::new(Box::new(ColumnType::String(None)))) | ||
} | ||
} |
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,48 @@ | ||
pub mod common; | ||
|
||
pub use common::{ | ||
features::{ | ||
event_trigger::{Event, Events}, | ||
*, | ||
}, | ||
setup::*, | ||
TestContext, | ||
}; | ||
use pretty_assertions::assert_eq; | ||
use sea_orm::{entity::prelude::*, entity::*, DatabaseConnection}; | ||
|
||
#[sea_orm_macros::test] | ||
#[cfg(all(feature = "sqlx-postgres", feature = "postgres-array"))] | ||
async fn main() -> Result<(), DbErr> { | ||
let ctx = TestContext::new("event_trigger_tests").await; | ||
create_tables(&ctx.db).await?; | ||
insert_event_trigger(&ctx.db).await?; | ||
ctx.delete().await; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub async fn insert_event_trigger(db: &DatabaseConnection) -> Result<(), DbErr> { | ||
let event_trigger = event_trigger::Model { | ||
id: 1, | ||
events: Events( | ||
["A", "B", "C"] | ||
.into_iter() | ||
.map(|s| Event(s.to_owned())) | ||
.collect(), | ||
), | ||
}; | ||
|
||
let result = event_trigger.clone().into_active_model().insert(db).await?; | ||
|
||
assert_eq!(result, event_trigger); | ||
|
||
let model = event_trigger::Entity::find() | ||
.filter(event_trigger::Column::Id.eq(event_trigger.id)) | ||
.one(db) | ||
.await?; | ||
|
||
assert_eq!(model, Some(event_trigger)); | ||
|
||
Ok(()) | ||
} |