Skip to content

Commit

Permalink
Add Postgres test section, test binary json
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelOnFira committed Dec 30, 2022
1 parent 1fcba45 commit 8aadc9b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
90 changes: 90 additions & 0 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1959,4 +1959,94 @@ mod tests {

Ok(expected.to_string())
}

#[test]
fn test_gen_postgres() -> io::Result<()> {
let entities = vec![
// This tests that the JsonBinary column type is annotated
// correctly in compact entity form. More information can be found
// in this issue:
//
// https://github.com/SeaQL/sea-orm/issues/1344
Entity {
table_name: "task".to_owned(),
columns: vec![
Column {
name: "id".to_owned(),
col_type: ColumnType::Integer(Some(11)),
auto_increment: true,
not_null: true,
unique: false,
},
Column {
name: "payload".to_owned(),
col_type: ColumnType::Json,
auto_increment: false,
not_null: true,
unique: false,
},
Column {
name: "payload_binary".to_owned(),
col_type: ColumnType::JsonBinary,
auto_increment: false,
not_null: true,
unique: false,
},
],
relations: vec![],
conjunct_relations: vec![],
primary_keys: vec![PrimaryKey {
name: "id".to_owned(),
}],
},
];
const ENTITY_FILES: [&str; 1] = [include_str!("../../tests/postgres/binary_json.rs")];

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

for (i, entity) in entities.iter().enumerate() {
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&None,
false,
false,
&TokenStream::new(),
&TokenStream::new(),
)
.into_iter()
.skip(1)
.fold(TokenStream::new(), |mut acc, tok| {
acc.extend(tok);
acc
})
.to_string()
);
assert_eq!(
parse_from_file(ENTITY_FILES[i].as_bytes())?.to_string(),
EntityWriter::gen_compact_code_blocks(
entity,
&crate::WithSerde::None,
&crate::DateTimeCrate::Chrono,
&Some("public".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(())
}
}
21 changes: 21 additions & 0 deletions sea-orm-codegen/tests/postgres/binary_json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! SeaORM Entity. Generated by sea-orm-codegen 0.1.0
//!
//! This file tests that the JsonBinary column type is annotated correctly is
//! compact entity form. More information can be found in this issue:
//!
//! https://github.com/SeaQL/sea-orm/issues/1344
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "task")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub payload: Json,
#[sea_orm(column_type = "JsonBinary")]
pub payload_binary: Json,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

0 comments on commit 8aadc9b

Please sign in to comment.