Test self referencing relation
This commit is contained in:
parent
bd56485180
commit
32f82a0c9b
@ -2,8 +2,10 @@ pub mod applog;
|
||||
pub mod metadata;
|
||||
pub mod repository;
|
||||
pub mod schema;
|
||||
pub mod self_join;
|
||||
|
||||
pub use applog::Entity as Applog;
|
||||
pub use metadata::Entity as Metadata;
|
||||
pub use repository::Entity as Repository;
|
||||
pub use schema::*;
|
||||
pub use self_join::Entity as SelfJoin;
|
||||
|
@ -3,12 +3,13 @@ pub use super::super::bakery_chain::*;
|
||||
use super::*;
|
||||
use crate::common::setup::create_table;
|
||||
use sea_orm::{error::*, sea_query, DatabaseConnection, DbConn, ExecResult};
|
||||
use sea_query::ColumnDef;
|
||||
use sea_query::{ColumnDef, ForeignKeyCreateStatement};
|
||||
|
||||
pub async fn create_tables(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
create_log_table(db).await?;
|
||||
create_metadata_table(db).await?;
|
||||
create_repository_table(db).await?;
|
||||
create_self_join_table(db).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -75,3 +76,27 @@ pub async fn create_repository_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||
|
||||
create_table(db, &stmt, Repository).await
|
||||
}
|
||||
|
||||
pub async fn create_self_join_table(db: &DbConn) -> Result<ExecResult, DbErr> {
|
||||
let stmt = sea_query::Table::create()
|
||||
.table(self_join::Entity)
|
||||
.col(
|
||||
ColumnDef::new(self_join::Column::Uuid)
|
||||
.uuid()
|
||||
.not_null()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(self_join::Column::UuidRef).uuid())
|
||||
.col(ColumnDef::new(self_join::Column::Time).time())
|
||||
.foreign_key(
|
||||
ForeignKeyCreateStatement::new()
|
||||
.name("fk-self_join-self_join")
|
||||
.from_tbl(SelfJoin)
|
||||
.from_col(self_join::Column::UuidRef)
|
||||
.to_tbl(SelfJoin)
|
||||
.to_col(self_join::Column::Uuid),
|
||||
)
|
||||
.to_owned();
|
||||
|
||||
create_table(db, &stmt, SelfJoin).await
|
||||
}
|
||||
|
77
tests/common/features/self_join.rs
Normal file
77
tests/common/features/self_join.rs
Normal file
@ -0,0 +1,77 @@
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "self_join")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key, auto_increment = false)]
|
||||
pub uuid: Uuid,
|
||||
pub uuid_ref: Option<Uuid>,
|
||||
pub time: Option<Time>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(belongs_to = "Entity", from = "Column::UuidRef", to = "Column::Uuid")]
|
||||
SelfReferencing,
|
||||
}
|
||||
|
||||
pub struct SelfReferencingLink;
|
||||
|
||||
impl Linked for SelfReferencingLink {
|
||||
type FromEntity = Entity;
|
||||
|
||||
type ToEntity = Entity;
|
||||
|
||||
fn link(&self) -> Vec<RelationDef> {
|
||||
vec![Relation::SelfReferencing.def()]
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pretty_assertions::assert_eq;
|
||||
use sea_orm::*;
|
||||
|
||||
#[test]
|
||||
fn find_linked_001() {
|
||||
let self_join_model = Model {
|
||||
uuid: Uuid::default(),
|
||||
uuid_ref: None,
|
||||
time: None,
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
self_join_model
|
||||
.find_linked(SelfReferencingLink)
|
||||
.build(DbBackend::MySql)
|
||||
.to_string(),
|
||||
[
|
||||
r#"SELECT `self_join`.`uuid`, `self_join`.`uuid_ref`, `self_join`.`time`"#,
|
||||
r#"FROM `self_join`"#,
|
||||
r#"INNER JOIN `self_join` AS `r0` ON `r0`.`uuid_ref` = `self_join`.`uuid`"#,
|
||||
r#"WHERE `r0`.`uuid` = '00000000-0000-0000-0000-000000000000'"#,
|
||||
]
|
||||
.join(" ")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn find_also_linked_001() {
|
||||
assert_eq!(
|
||||
Entity::find()
|
||||
.find_also_linked(SelfReferencingLink)
|
||||
.build(DbBackend::MySql)
|
||||
.to_string(),
|
||||
[
|
||||
r#"SELECT `self_join`.`uuid` AS `A_uuid`, `self_join`.`uuid_ref` AS `A_uuid_ref`, `self_join`.`time` AS `A_time`,"#,
|
||||
r#"`r0`.`uuid` AS `B_uuid`, `r0`.`uuid_ref` AS `B_uuid_ref`, `r0`.`time` AS `B_time`"#,
|
||||
r#"FROM `self_join`"#,
|
||||
r#"LEFT JOIN `self_join` AS `r0` ON `self_join`.`uuid_ref` = `r0`.`uuid`"#,
|
||||
]
|
||||
.join(" ")
|
||||
);
|
||||
}
|
||||
}
|
89
tests/self_join_tests.rs
Normal file
89
tests/self_join_tests.rs
Normal file
@ -0,0 +1,89 @@
|
||||
pub mod common;
|
||||
|
||||
pub use common::{features::*, setup::*, TestContext};
|
||||
use pretty_assertions::assert_eq;
|
||||
use sea_orm::{entity::prelude::*, *};
|
||||
|
||||
#[sea_orm_macros::test]
|
||||
#[cfg(any(
|
||||
feature = "sqlx-mysql",
|
||||
feature = "sqlx-sqlite",
|
||||
feature = "sqlx-postgres"
|
||||
))]
|
||||
async fn main() -> Result<(), DbErr> {
|
||||
let ctx = TestContext::new("self_join_tests").await;
|
||||
create_tables(&ctx.db).await?;
|
||||
create_metadata(&ctx.db).await?;
|
||||
ctx.delete().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
let model = self_join::Model {
|
||||
uuid: Uuid::new_v4(),
|
||||
uuid_ref: None,
|
||||
time: Some(Time::from_hms(1, 00, 00)),
|
||||
};
|
||||
|
||||
model.clone().into_active_model().insert(db).await?;
|
||||
|
||||
let linked_model = self_join::Model {
|
||||
uuid: Uuid::new_v4(),
|
||||
uuid_ref: Some(model.clone().uuid),
|
||||
time: Some(Time::from_hms(2, 00, 00)),
|
||||
};
|
||||
|
||||
linked_model.clone().into_active_model().insert(db).await?;
|
||||
|
||||
let not_linked_model = self_join::Model {
|
||||
uuid: Uuid::new_v4(),
|
||||
uuid_ref: None,
|
||||
time: Some(Time::from_hms(3, 00, 00)),
|
||||
};
|
||||
|
||||
not_linked_model
|
||||
.clone()
|
||||
.into_active_model()
|
||||
.insert(db)
|
||||
.await?;
|
||||
|
||||
assert_eq!(
|
||||
model
|
||||
.find_linked(self_join::SelfReferencingLink)
|
||||
.all(db)
|
||||
.await?,
|
||||
vec![]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
linked_model
|
||||
.find_linked(self_join::SelfReferencingLink)
|
||||
.all(db)
|
||||
.await?,
|
||||
vec![model.clone()]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
not_linked_model
|
||||
.find_linked(self_join::SelfReferencingLink)
|
||||
.all(db)
|
||||
.await?,
|
||||
vec![]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
self_join::Entity::find()
|
||||
.find_also_linked(self_join::SelfReferencingLink)
|
||||
.order_by_asc(self_join::Column::Time)
|
||||
.all(db)
|
||||
.await?,
|
||||
vec![
|
||||
(model.clone(), None),
|
||||
(linked_model, Some(model)),
|
||||
(not_linked_model, None),
|
||||
]
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user