Skip to content

Commit

Permalink
Avoid repeating this 100 times
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Oct 12, 2023
1 parent 900b410 commit d29ed5b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
47 changes: 0 additions & 47 deletions tests/common/features/self_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,3 @@ impl Linked for SelfReferencingLink {
}

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(" ")
);
}
}
46 changes: 45 additions & 1 deletion tests/self_join_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod common;

pub use common::{features::*, setup::*, TestContext};
use pretty_assertions::assert_eq;
use sea_orm::{entity::prelude::*, IntoActiveModel, QueryOrder};
use sea_orm::{entity::prelude::*, query::*, DbBackend, IntoActiveModel, QueryOrder};

#[sea_orm_macros::test]
#[cfg(any(
Expand All @@ -15,6 +15,8 @@ async fn main() -> Result<(), DbErr> {
create_tables(&ctx.db).await?;
create_metadata(&ctx.db).await?;
ctx.delete().await;
find_linked_001();
find_also_linked_001();

Ok(())
}
Expand Down Expand Up @@ -87,3 +89,45 @@ pub async fn create_metadata(db: &DatabaseConnection) -> Result<(), DbErr> {

Ok(())
}

fn find_linked_001() {
use self_join::*;

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(" ")
);
}

fn find_also_linked_001() {
use self_join::*;

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(" ")
);
}

0 comments on commit d29ed5b

Please sign in to comment.