Skip to content

Commit

Permalink
Added expanded entity format test
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelOnFira committed Jan 16, 2023
1 parent 8aadc9b commit df79cbf
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
23 changes: 23 additions & 0 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,9 @@ mod tests {
];
const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")];

const ENTITY_FILES_EXPANDED: [&str; 1] =
[include_str!("../../tests/postgres/binary_json_expanded.rs")];

assert_eq!(entities.len(), ENTITY_FILES.len());

for (i, entity) in entities.iter().enumerate() {
Expand Down Expand Up @@ -2045,6 +2048,26 @@ mod tests {
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES_EXPANDED[i].as_bytes())?.to_string(),
EntityWriter::gen_expanded_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("schema_name".to_owned()),
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
}

Ok(())
Expand Down
65 changes: 65 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json_expanded.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! expanded entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344
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 {
"task"
}
}

#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
pub struct Model {
pub id: i32,
pub payload: Json,
pub payload_binary: Json,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column {
Id,
Payload,
PayloadBinary,
}

#[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 {}
impl ColumnTrait for Column {
type EntityName = Entity;
fn def(&self) -> ColumnDef {
match self {
Self::Id => ColumnType::Integer.def(),
// This is the part that is being tested.
Self::Payload => ColumnType::Json.def(),
Self::PayloadBinary => ColumnType::JsonBinary.def(),
}
}
}
impl RelationTrait for Relation {
fn def(&self) -> RelationDef {
panic!("No RelationDef")
}
}
impl ActiveModelBehavior for ActiveModel {}

0 comments on commit df79cbf

Please sign in to comment.