From c6e1a84a120dff5a73abe450fb56ed4483d9413d Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Fri, 19 Jan 2024 18:47:49 +0800 Subject: [PATCH] codegen: testing entity generation of composite foreign key (#2071) --- sea-orm-codegen/src/entity/writer.rs | 96 ++++++++++++++++++- sea-orm-codegen/tests/compact/child.rs | 30 ++++++ sea-orm-codegen/tests/compact/parent.rs | 26 +++++ .../tests/compact_with_schema_name/child.rs | 30 ++++++ .../tests/compact_with_schema_name/parent.rs | 26 +++++ sea-orm-codegen/tests/expanded/child.rs | 73 ++++++++++++++ sea-orm-codegen/tests/expanded/parent.rs | 68 +++++++++++++ .../tests/expanded_with_schema_name/child.rs | 77 +++++++++++++++ .../tests/expanded_with_schema_name/parent.rs | 72 ++++++++++++++ 9 files changed, 494 insertions(+), 4 deletions(-) create mode 100644 sea-orm-codegen/tests/compact/child.rs create mode 100644 sea-orm-codegen/tests/compact/parent.rs create mode 100644 sea-orm-codegen/tests/compact_with_schema_name/child.rs create mode 100644 sea-orm-codegen/tests/compact_with_schema_name/parent.rs create mode 100644 sea-orm-codegen/tests/expanded/child.rs create mode 100644 sea-orm-codegen/tests/expanded/parent.rs create mode 100644 sea-orm-codegen/tests/expanded_with_schema_name/child.rs create mode 100644 sea-orm-codegen/tests/expanded_with_schema_name/parent.rs diff --git a/sea-orm-codegen/src/entity/writer.rs b/sea-orm-codegen/src/entity/writer.rs index 099a96797d..3c4c17eebe 100644 --- a/sea-orm-codegen/src/entity/writer.rs +++ b/sea-orm-codegen/src/entity/writer.rs @@ -1410,6 +1410,86 @@ mod tests { name: "id".to_owned(), }], }, + Entity { + table_name: "parent".to_owned(), + columns: vec![ + Column { + name: "id1".to_owned(), + col_type: ColumnType::Integer, + auto_increment: false, + not_null: true, + unique: false, + }, + Column { + name: "id2".to_owned(), + col_type: ColumnType::Integer, + auto_increment: false, + not_null: true, + unique: false, + }, + ], + relations: vec![Relation { + ref_table: "child".to_owned(), + columns: vec![], + ref_columns: vec![], + rel_type: RelationType::HasMany, + on_delete: None, + on_update: None, + self_referencing: false, + num_suffix: 0, + impl_related: true, + }], + conjunct_relations: vec![], + primary_keys: vec![ + PrimaryKey { + name: "id1".to_owned(), + }, + PrimaryKey { + name: "id2".to_owned(), + }, + ], + }, + Entity { + table_name: "child".to_owned(), + columns: vec![ + Column { + name: "id".to_owned(), + col_type: ColumnType::Integer, + auto_increment: true, + not_null: true, + unique: false, + }, + Column { + name: "parent_id1".to_owned(), + col_type: ColumnType::Integer, + auto_increment: false, + not_null: true, + unique: false, + }, + Column { + name: "parent_id2".to_owned(), + col_type: ColumnType::Integer, + auto_increment: false, + not_null: true, + unique: false, + }, + ], + relations: vec![Relation { + ref_table: "parent".to_owned(), + columns: vec!["parent_id1".to_owned(), "parent_id2".to_owned()], + ref_columns: vec!["id1".to_owned(), "id2".to_owned()], + rel_type: RelationType::BelongsTo, + on_delete: None, + on_update: None, + self_referencing: false, + num_suffix: 0, + impl_related: true, + }], + conjunct_relations: vec![], + primary_keys: vec![PrimaryKey { + name: "id".to_owned(), + }], + }, ] } @@ -1434,7 +1514,7 @@ mod tests { #[test] fn test_gen_expanded_code_blocks() -> io::Result<()> { let entities = setup(); - const ENTITY_FILES: [&str; 11] = [ + const ENTITY_FILES: [&str; 13] = [ include_str!("../../tests/expanded/cake.rs"), include_str!("../../tests/expanded/cake_filling.rs"), include_str!("../../tests/expanded/cake_filling_price.rs"), @@ -1446,8 +1526,10 @@ mod tests { include_str!("../../tests/expanded/cake_with_double.rs"), include_str!("../../tests/expanded/collection.rs"), include_str!("../../tests/expanded/collection_float.rs"), + include_str!("../../tests/expanded/parent.rs"), + include_str!("../../tests/expanded/child.rs"), ]; - const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 11] = [ + const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [ include_str!("../../tests/expanded_with_schema_name/cake.rs"), include_str!("../../tests/expanded_with_schema_name/cake_filling.rs"), include_str!("../../tests/expanded_with_schema_name/cake_filling_price.rs"), @@ -1459,6 +1541,8 @@ mod tests { include_str!("../../tests/expanded_with_schema_name/cake_with_double.rs"), include_str!("../../tests/expanded_with_schema_name/collection.rs"), include_str!("../../tests/expanded_with_schema_name/collection_float.rs"), + include_str!("../../tests/expanded_with_schema_name/parent.rs"), + include_str!("../../tests/expanded_with_schema_name/child.rs"), ]; assert_eq!(entities.len(), ENTITY_FILES.len()); @@ -1535,7 +1619,7 @@ mod tests { #[test] fn test_gen_compact_code_blocks() -> io::Result<()> { let entities = setup(); - const ENTITY_FILES: [&str; 11] = [ + const ENTITY_FILES: [&str; 13] = [ include_str!("../../tests/compact/cake.rs"), include_str!("../../tests/compact/cake_filling.rs"), include_str!("../../tests/compact/cake_filling_price.rs"), @@ -1547,8 +1631,10 @@ mod tests { include_str!("../../tests/compact/cake_with_double.rs"), include_str!("../../tests/compact/collection.rs"), include_str!("../../tests/compact/collection_float.rs"), + include_str!("../../tests/compact/parent.rs"), + include_str!("../../tests/compact/child.rs"), ]; - const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 11] = [ + const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [ include_str!("../../tests/compact_with_schema_name/cake.rs"), include_str!("../../tests/compact_with_schema_name/cake_filling.rs"), include_str!("../../tests/compact_with_schema_name/cake_filling_price.rs"), @@ -1560,6 +1646,8 @@ mod tests { include_str!("../../tests/compact_with_schema_name/cake_with_double.rs"), include_str!("../../tests/compact_with_schema_name/collection.rs"), include_str!("../../tests/compact_with_schema_name/collection_float.rs"), + include_str!("../../tests/compact_with_schema_name/parent.rs"), + include_str!("../../tests/compact_with_schema_name/child.rs"), ]; assert_eq!(entities.len(), ENTITY_FILES.len()); diff --git a/sea-orm-codegen/tests/compact/child.rs b/sea-orm-codegen/tests/compact/child.rs new file mode 100644 index 0000000000..428d09631d --- /dev/null +++ b/sea-orm-codegen/tests/compact/child.rs @@ -0,0 +1,30 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "child")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub parent_id1: i32, + pub parent_id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::parent::Entity", + from = "(Column::ParentId1, Column::ParentId2)", + to = "(super::parent::Column::Id1, super::parent::Column::Id2)", + )] + Parent, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/compact/parent.rs b/sea-orm-codegen/tests/compact/parent.rs new file mode 100644 index 0000000000..25dc9aeea0 --- /dev/null +++ b/sea-orm-codegen/tests/compact/parent.rs @@ -0,0 +1,26 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "parent")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id1: i32, + #[sea_orm(primary_key, auto_increment = false)] + pub id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::child::Entity")] + Child, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Child.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/compact_with_schema_name/child.rs b/sea-orm-codegen/tests/compact_with_schema_name/child.rs new file mode 100644 index 0000000000..28e687d614 --- /dev/null +++ b/sea-orm-codegen/tests/compact_with_schema_name/child.rs @@ -0,0 +1,30 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(schema_name = "schema_name", table_name = "child")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + pub parent_id1: i32, + pub parent_id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm( + belongs_to = "super::parent::Entity", + from = "(Column::ParentId1, Column::ParentId2)", + to = "(super::parent::Column::Id1, super::parent::Column::Id2)", + )] + Parent, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/compact_with_schema_name/parent.rs b/sea-orm-codegen/tests/compact_with_schema_name/parent.rs new file mode 100644 index 0000000000..3bd4ac071c --- /dev/null +++ b/sea-orm-codegen/tests/compact_with_schema_name/parent.rs @@ -0,0 +1,26 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(schema_name = "schema_name", table_name = "parent")] +pub struct Model { + #[sea_orm(primary_key, auto_increment = false)] + pub id1: i32, + #[sea_orm(primary_key, auto_increment = false)] + pub id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::child::Entity")] + Child, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Child.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded/child.rs b/sea-orm-codegen/tests/expanded/child.rs new file mode 100644 index 0000000000..7073fffb30 --- /dev/null +++ b/sea-orm-codegen/tests/expanded/child.rs @@ -0,0 +1,73 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "child" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)] +pub struct Model { + pub id: i32, + pub parent_id1: i32, + pub parent_id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + ParentId1, + ParentId2, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Parent, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::ParentId1 => ColumnType::Integer.def(), + Self::ParentId2 => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Parent => Entity::belongs_to(super::parent::Entity) + .from((Column::ParentId1, Column::ParentId2)) + .to((super::parent::Column::Id1, super::parent::Column::Id2)) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded/parent.rs b/sea-orm-codegen/tests/expanded/parent.rs new file mode 100644 index 0000000000..2ecaa5916c --- /dev/null +++ b/sea-orm-codegen/tests/expanded/parent.rs @@ -0,0 +1,68 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn table_name(&self) -> &str { + "parent" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)] +pub struct Model { + pub id1: i32, + pub id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id1, + Id2, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id1, + Id2, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = (i32, i32); + fn auto_increment() -> bool { + false + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Child, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id1 => ColumnType::Integer.def(), + Self::Id2 => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Child => Entity::has_many(super::child::Entity).into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Child.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/child.rs b/sea-orm-codegen/tests/expanded_with_schema_name/child.rs new file mode 100644 index 0000000000..05ba5bfa58 --- /dev/null +++ b/sea-orm-codegen/tests/expanded_with_schema_name/child.rs @@ -0,0 +1,77 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn schema_name(&self) -> Option< &str > { + Some("schema_name") + } + + fn table_name(&self) -> &str { + "child" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)] +pub struct Model { + pub id: i32, + pub parent_id1: i32, + pub parent_id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id, + ParentId1, + ParentId2, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = i32; + fn auto_increment() -> bool { + true + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Parent, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id => ColumnType::Integer.def(), + Self::ParentId1 => ColumnType::Integer.def(), + Self::ParentId2 => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Parent => Entity::belongs_to(super::parent::Entity) + .from((Column::ParentId1, Column::ParentId2)) + .to((super::parent::Column::Id1, super::parent::Column::Id2)) + .into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Parent.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/sea-orm-codegen/tests/expanded_with_schema_name/parent.rs b/sea-orm-codegen/tests/expanded_with_schema_name/parent.rs new file mode 100644 index 0000000000..1665c69ed4 --- /dev/null +++ b/sea-orm-codegen/tests/expanded_with_schema_name/parent.rs @@ -0,0 +1,72 @@ +//! SeaORM Entity. Generated by sea-orm-codegen 0.10.0 + +use sea_orm::entity::prelude::*; + +#[derive(Copy, Clone, Default, Debug, DeriveEntity)] +pub struct Entity; + +impl EntityName for Entity { + fn schema_name(&self) -> Option< &str > { + Some("schema_name") + } + + fn table_name(&self) -> &str { + "parent" + } +} + +#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)] +pub struct Model { + pub id1: i32, + pub id2: i32, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] +pub enum Column { + Id1, + Id2, +} + +#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] +pub enum PrimaryKey { + Id1, + Id2, +} + +impl PrimaryKeyTrait for PrimaryKey { + type ValueType = (i32, i32); + fn auto_increment() -> bool { + false + } +} + +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + Child, +} + +impl ColumnTrait for Column { + type EntityName = Entity; + fn def(&self) -> ColumnDef { + match self { + Self::Id1 => ColumnType::Integer.def(), + Self::Id2 => ColumnType::Integer.def(), + } + } +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::Child => Entity::has_many(super::child::Entity).into(), + } + } +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Child.def() + } +} + +impl ActiveModelBehavior for ActiveModel {}