Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 147 additions & 8 deletions sea-orm-codegen/src/entity/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,10 @@ mod tests {
use proc_macro2::TokenStream;
use quote::quote;
use sea_query::{Alias, ColumnType, ForeignKeyAction, RcOrArc, SeaRc, StringLen};
use std::io::{self, BufRead, BufReader, Read};
use std::{
io::{self, BufRead, BufReader, Read},
sync::Arc,
};

fn default_column_option() -> ColumnOption {
Default::default()
Expand Down Expand Up @@ -1576,6 +1579,106 @@ mod tests {
name: "id".to_owned(),
}],
},
Entity {
table_name: "imports".to_owned(),
columns: vec![
Column {
name: "a".to_owned(),
col_type: ColumnType::Json,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "b".to_owned(),
col_type: ColumnType::Date,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "c".to_owned(),
col_type: ColumnType::Time,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "d".to_owned(),
col_type: ColumnType::DateTime,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "e".to_owned(),
col_type: ColumnType::TimestampWithTimeZone,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "f".to_owned(),
col_type: ColumnType::Decimal(None),
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "g".to_owned(),
col_type: ColumnType::Uuid,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "h".to_owned(),
col_type: ColumnType::Vector(None),
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "i".to_owned(),
col_type: ColumnType::Inet,
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "j".to_owned(),
col_type: ColumnType::Array(Arc::new(ColumnType::Json)),
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
Column {
name: "k".to_owned(),
col_type: ColumnType::Array(Arc::new(ColumnType::Array(Arc::new(
ColumnType::Cidr,
)))),
auto_increment: true,
not_null: true,
unique: false,
unique_key: None,
},
],
relations: vec![],
conjunct_relations: vec![],
primary_keys: vec![PrimaryKey {
name: "a".to_owned(),
}],
},
]
}

Expand Down Expand Up @@ -1618,7 +1721,7 @@ mod tests {
#[test]
fn test_gen_expanded_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 13] = [
const ENTITY_FILES: [&str; 14] = [
include_str!("../../tests/expanded/cake.rs"),
include_str!("../../tests/expanded/cake_filling.rs"),
include_str!("../../tests/expanded/cake_filling_price.rs"),
Expand All @@ -1632,8 +1735,9 @@ mod tests {
include_str!("../../tests/expanded/collection_float.rs"),
include_str!("../../tests/expanded/parent.rs"),
include_str!("../../tests/expanded/child.rs"),
include_str!("../../tests/expanded/imports.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
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"),
Expand All @@ -1647,6 +1751,7 @@ mod tests {
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"),
include_str!("../../tests/expanded_with_schema_name/imports.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down Expand Up @@ -1706,7 +1811,7 @@ mod tests {
#[test]
fn test_gen_compact_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 13] = [
const ENTITY_FILES: [&str; 14] = [
include_str!("../../tests/compact/cake.rs"),
include_str!("../../tests/compact/cake_filling.rs"),
include_str!("../../tests/compact/cake_filling_price.rs"),
Expand All @@ -1720,8 +1825,9 @@ mod tests {
include_str!("../../tests/compact/collection_float.rs"),
include_str!("../../tests/compact/parent.rs"),
include_str!("../../tests/compact/child.rs"),
include_str!("../../tests/compact/imports.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
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"),
Expand All @@ -1735,6 +1841,7 @@ mod tests {
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"),
include_str!("../../tests/compact_with_schema_name/imports.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down Expand Up @@ -1794,7 +1901,7 @@ mod tests {
#[test]
fn test_gen_frontend_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 13] = [
const ENTITY_FILES: [&str; 14] = [
include_str!("../../tests/frontend/cake.rs"),
include_str!("../../tests/frontend/cake_filling.rs"),
include_str!("../../tests/frontend/cake_filling_price.rs"),
Expand All @@ -1808,8 +1915,9 @@ mod tests {
include_str!("../../tests/frontend/collection_float.rs"),
include_str!("../../tests/frontend/parent.rs"),
include_str!("../../tests/frontend/child.rs"),
include_str!("../../tests/frontend/imports.rs"),
];
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
include_str!("../../tests/frontend_with_schema_name/cake.rs"),
include_str!("../../tests/frontend_with_schema_name/cake_filling.rs"),
include_str!("../../tests/frontend_with_schema_name/cake_filling_price.rs"),
Expand All @@ -1823,6 +1931,7 @@ mod tests {
include_str!("../../tests/frontend_with_schema_name/collection_float.rs"),
include_str!("../../tests/frontend_with_schema_name/parent.rs"),
include_str!("../../tests/frontend_with_schema_name/child.rs"),
include_str!("../../tests/frontend_with_schema_name/imports.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down Expand Up @@ -1879,6 +1988,35 @@ mod tests {
Ok(())
}

#[test]
fn test_gen_frontend_imports() -> io::Result<()> {
let imports_entity = setup().into_iter()
.find(|e| e.get_table_name_snake_case() == "imports")
.unwrap();


assert_eq!(imports_entity.get_table_name_snake_case(), "imports");

assert_eq!(
comparable_file_string(include_str!("../../tests/frontend_with_imports/imports.rs"))?,
generated_to_string(EntityWriter::gen_frontend_code_blocks(
&imports_entity,
&WithSerde::None,
&default_column_option(),
&None,
true,
false,
&TokenStream::new(),
&TokenStream::new(),
&TokenStream::new(),
false,
true,
))
);

Ok(())
}

#[test]
fn test_gen_with_serde() -> io::Result<()> {
let cake_entity = setup().get(0).unwrap().clone();
Expand Down Expand Up @@ -3032,7 +3170,7 @@ mod tests {
#[test]
fn test_gen_dense_code_blocks() -> io::Result<()> {
let entities = setup();
const ENTITY_FILES: [&str; 13] = [
const ENTITY_FILES: [&str; 14] = [
include_str!("../../tests/dense/cake.rs"),
include_str!("../../tests/dense/cake_filling.rs"),
include_str!("../../tests/dense/cake_filling_price.rs"),
Expand All @@ -3046,6 +3184,7 @@ mod tests {
include_str!("../../tests/dense/collection_float.rs"),
include_str!("../../tests/dense/parent.rs"),
include_str!("../../tests/dense/child.rs"),
include_str!("../../tests/dense/imports.rs"),
];

assert_eq!(entities.len(), ENTITY_FILES.len());
Expand Down
Loading
Loading