Skip to content

Commit f0ec2b2

Browse files
DexterHill0tyt2y3
andauthored
Generate correct imports in frontend format (#3037)
* fix(codegen): generate correct imports in `frontend` format * chore(codegen): add tests for frontend imports * fix(codegen): update rest of tests for new import entity * Apply suggestion from @tyt2y3 * chore(codegen): move enum out of function --------- Co-authored-by: Chris Tsang <chris.2y3@outlook.com>
1 parent 0677ee5 commit f0ec2b2

11 files changed

Lines changed: 615 additions & 8 deletions

File tree

sea-orm-codegen/src/entity/writer.rs

Lines changed: 147 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,10 @@ mod tests {
879879
use proc_macro2::TokenStream;
880880
use quote::quote;
881881
use sea_query::{Alias, ColumnType, ForeignKeyAction, RcOrArc, SeaRc, StringLen};
882-
use std::io::{self, BufRead, BufReader, Read};
882+
use std::{
883+
io::{self, BufRead, BufReader, Read},
884+
sync::Arc,
885+
};
883886

884887
fn default_column_option() -> ColumnOption {
885888
Default::default()
@@ -1576,6 +1579,106 @@ mod tests {
15761579
name: "id".to_owned(),
15771580
}],
15781581
},
1582+
Entity {
1583+
table_name: "imports".to_owned(),
1584+
columns: vec![
1585+
Column {
1586+
name: "a".to_owned(),
1587+
col_type: ColumnType::Json,
1588+
auto_increment: true,
1589+
not_null: true,
1590+
unique: false,
1591+
unique_key: None,
1592+
},
1593+
Column {
1594+
name: "b".to_owned(),
1595+
col_type: ColumnType::Date,
1596+
auto_increment: true,
1597+
not_null: true,
1598+
unique: false,
1599+
unique_key: None,
1600+
},
1601+
Column {
1602+
name: "c".to_owned(),
1603+
col_type: ColumnType::Time,
1604+
auto_increment: true,
1605+
not_null: true,
1606+
unique: false,
1607+
unique_key: None,
1608+
},
1609+
Column {
1610+
name: "d".to_owned(),
1611+
col_type: ColumnType::DateTime,
1612+
auto_increment: true,
1613+
not_null: true,
1614+
unique: false,
1615+
unique_key: None,
1616+
},
1617+
Column {
1618+
name: "e".to_owned(),
1619+
col_type: ColumnType::TimestampWithTimeZone,
1620+
auto_increment: true,
1621+
not_null: true,
1622+
unique: false,
1623+
unique_key: None,
1624+
},
1625+
Column {
1626+
name: "f".to_owned(),
1627+
col_type: ColumnType::Decimal(None),
1628+
auto_increment: true,
1629+
not_null: true,
1630+
unique: false,
1631+
unique_key: None,
1632+
},
1633+
Column {
1634+
name: "g".to_owned(),
1635+
col_type: ColumnType::Uuid,
1636+
auto_increment: true,
1637+
not_null: true,
1638+
unique: false,
1639+
unique_key: None,
1640+
},
1641+
Column {
1642+
name: "h".to_owned(),
1643+
col_type: ColumnType::Vector(None),
1644+
auto_increment: true,
1645+
not_null: true,
1646+
unique: false,
1647+
unique_key: None,
1648+
},
1649+
Column {
1650+
name: "i".to_owned(),
1651+
col_type: ColumnType::Inet,
1652+
auto_increment: true,
1653+
not_null: true,
1654+
unique: false,
1655+
unique_key: None,
1656+
},
1657+
Column {
1658+
name: "j".to_owned(),
1659+
col_type: ColumnType::Array(Arc::new(ColumnType::Json)),
1660+
auto_increment: true,
1661+
not_null: true,
1662+
unique: false,
1663+
unique_key: None,
1664+
},
1665+
Column {
1666+
name: "k".to_owned(),
1667+
col_type: ColumnType::Array(Arc::new(ColumnType::Array(Arc::new(
1668+
ColumnType::Cidr,
1669+
)))),
1670+
auto_increment: true,
1671+
not_null: true,
1672+
unique: false,
1673+
unique_key: None,
1674+
},
1675+
],
1676+
relations: vec![],
1677+
conjunct_relations: vec![],
1678+
primary_keys: vec![PrimaryKey {
1679+
name: "a".to_owned(),
1680+
}],
1681+
},
15791682
]
15801683
}
15811684

@@ -1618,7 +1721,7 @@ mod tests {
16181721
#[test]
16191722
fn test_gen_expanded_code_blocks() -> io::Result<()> {
16201723
let entities = setup();
1621-
const ENTITY_FILES: [&str; 13] = [
1724+
const ENTITY_FILES: [&str; 14] = [
16221725
include_str!("../../tests/expanded/cake.rs"),
16231726
include_str!("../../tests/expanded/cake_filling.rs"),
16241727
include_str!("../../tests/expanded/cake_filling_price.rs"),
@@ -1632,8 +1735,9 @@ mod tests {
16321735
include_str!("../../tests/expanded/collection_float.rs"),
16331736
include_str!("../../tests/expanded/parent.rs"),
16341737
include_str!("../../tests/expanded/child.rs"),
1738+
include_str!("../../tests/expanded/imports.rs"),
16351739
];
1636-
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
1740+
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
16371741
include_str!("../../tests/expanded_with_schema_name/cake.rs"),
16381742
include_str!("../../tests/expanded_with_schema_name/cake_filling.rs"),
16391743
include_str!("../../tests/expanded_with_schema_name/cake_filling_price.rs"),
@@ -1647,6 +1751,7 @@ mod tests {
16471751
include_str!("../../tests/expanded_with_schema_name/collection_float.rs"),
16481752
include_str!("../../tests/expanded_with_schema_name/parent.rs"),
16491753
include_str!("../../tests/expanded_with_schema_name/child.rs"),
1754+
include_str!("../../tests/expanded_with_schema_name/imports.rs"),
16501755
];
16511756

16521757
assert_eq!(entities.len(), ENTITY_FILES.len());
@@ -1706,7 +1811,7 @@ mod tests {
17061811
#[test]
17071812
fn test_gen_compact_code_blocks() -> io::Result<()> {
17081813
let entities = setup();
1709-
const ENTITY_FILES: [&str; 13] = [
1814+
const ENTITY_FILES: [&str; 14] = [
17101815
include_str!("../../tests/compact/cake.rs"),
17111816
include_str!("../../tests/compact/cake_filling.rs"),
17121817
include_str!("../../tests/compact/cake_filling_price.rs"),
@@ -1720,8 +1825,9 @@ mod tests {
17201825
include_str!("../../tests/compact/collection_float.rs"),
17211826
include_str!("../../tests/compact/parent.rs"),
17221827
include_str!("../../tests/compact/child.rs"),
1828+
include_str!("../../tests/compact/imports.rs"),
17231829
];
1724-
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
1830+
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
17251831
include_str!("../../tests/compact_with_schema_name/cake.rs"),
17261832
include_str!("../../tests/compact_with_schema_name/cake_filling.rs"),
17271833
include_str!("../../tests/compact_with_schema_name/cake_filling_price.rs"),
@@ -1735,6 +1841,7 @@ mod tests {
17351841
include_str!("../../tests/compact_with_schema_name/collection_float.rs"),
17361842
include_str!("../../tests/compact_with_schema_name/parent.rs"),
17371843
include_str!("../../tests/compact_with_schema_name/child.rs"),
1844+
include_str!("../../tests/compact_with_schema_name/imports.rs"),
17381845
];
17391846

17401847
assert_eq!(entities.len(), ENTITY_FILES.len());
@@ -1794,7 +1901,7 @@ mod tests {
17941901
#[test]
17951902
fn test_gen_frontend_code_blocks() -> io::Result<()> {
17961903
let entities = setup();
1797-
const ENTITY_FILES: [&str; 13] = [
1904+
const ENTITY_FILES: [&str; 14] = [
17981905
include_str!("../../tests/frontend/cake.rs"),
17991906
include_str!("../../tests/frontend/cake_filling.rs"),
18001907
include_str!("../../tests/frontend/cake_filling_price.rs"),
@@ -1808,8 +1915,9 @@ mod tests {
18081915
include_str!("../../tests/frontend/collection_float.rs"),
18091916
include_str!("../../tests/frontend/parent.rs"),
18101917
include_str!("../../tests/frontend/child.rs"),
1918+
include_str!("../../tests/frontend/imports.rs"),
18111919
];
1812-
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 13] = [
1920+
const ENTITY_FILES_WITH_SCHEMA_NAME: [&str; 14] = [
18131921
include_str!("../../tests/frontend_with_schema_name/cake.rs"),
18141922
include_str!("../../tests/frontend_with_schema_name/cake_filling.rs"),
18151923
include_str!("../../tests/frontend_with_schema_name/cake_filling_price.rs"),
@@ -1823,6 +1931,7 @@ mod tests {
18231931
include_str!("../../tests/frontend_with_schema_name/collection_float.rs"),
18241932
include_str!("../../tests/frontend_with_schema_name/parent.rs"),
18251933
include_str!("../../tests/frontend_with_schema_name/child.rs"),
1934+
include_str!("../../tests/frontend_with_schema_name/imports.rs"),
18261935
];
18271936

18281937
assert_eq!(entities.len(), ENTITY_FILES.len());
@@ -1879,6 +1988,35 @@ mod tests {
18791988
Ok(())
18801989
}
18811990

1991+
#[test]
1992+
fn test_gen_frontend_imports() -> io::Result<()> {
1993+
let imports_entity = setup().into_iter()
1994+
.find(|e| e.get_table_name_snake_case() == "imports")
1995+
.unwrap();
1996+
1997+
1998+
assert_eq!(imports_entity.get_table_name_snake_case(), "imports");
1999+
2000+
assert_eq!(
2001+
comparable_file_string(include_str!("../../tests/frontend_with_imports/imports.rs"))?,
2002+
generated_to_string(EntityWriter::gen_frontend_code_blocks(
2003+
&imports_entity,
2004+
&WithSerde::None,
2005+
&default_column_option(),
2006+
&None,
2007+
true,
2008+
false,
2009+
&TokenStream::new(),
2010+
&TokenStream::new(),
2011+
&TokenStream::new(),
2012+
false,
2013+
true,
2014+
))
2015+
);
2016+
2017+
Ok(())
2018+
}
2019+
18822020
#[test]
18832021
fn test_gen_with_serde() -> io::Result<()> {
18842022
let cake_entity = setup().get(0).unwrap().clone();
@@ -3032,7 +3170,7 @@ mod tests {
30323170
#[test]
30333171
fn test_gen_dense_code_blocks() -> io::Result<()> {
30343172
let entities = setup();
3035-
const ENTITY_FILES: [&str; 13] = [
3173+
const ENTITY_FILES: [&str; 14] = [
30363174
include_str!("../../tests/dense/cake.rs"),
30373175
include_str!("../../tests/dense/cake_filling.rs"),
30383176
include_str!("../../tests/dense/cake_filling_price.rs"),
@@ -3046,6 +3184,7 @@ mod tests {
30463184
include_str!("../../tests/dense/collection_float.rs"),
30473185
include_str!("../../tests/dense/parent.rs"),
30483186
include_str!("../../tests/dense/child.rs"),
3187+
include_str!("../../tests/dense/imports.rs"),
30493188
];
30503189

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

0 commit comments

Comments
 (0)