Skip to content

Commit

Permalink
<feat>(sea-orm-cli) added column type and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ProbablyClem committed Jun 11, 2023
1 parent d154e70 commit f1c491c
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 46 deletions.
27 changes: 18 additions & 9 deletions sea-orm-codegen/src/entity/base_entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use quote::format_ident;
use quote::quote;
use sea_query::ColumnType;

use crate::DecimalCrate;
use crate::{
util::escape_rust_keyword, Column, ConjunctRelation, DateTimeCrate, PrimaryKey, Relation,
};
Expand Down Expand Up @@ -48,11 +49,15 @@ impl Entity {
.collect()
}

pub fn get_column_rs_types(&self, date_time_crate: &DateTimeCrate) -> Vec<TokenStream> {
pub fn get_column_rs_types(
&self,
date_time_crate: &DateTimeCrate,
decimal_crate: &DecimalCrate,
) -> Vec<TokenStream> {
self.columns
.clone()
.into_iter()
.map(|col| col.get_rs_type(date_time_crate))
.map(|col| col.get_rs_type(date_time_crate, decimal_crate))
.collect()
}

Expand Down Expand Up @@ -183,7 +188,11 @@ impl Entity {
format_ident!("{}", auto_increment)
}

pub fn get_primary_key_rs_type(&self, date_time_crate: &DateTimeCrate) -> TokenStream {
pub fn get_primary_key_rs_type(
&self,
date_time_crate: &DateTimeCrate,
decimal_crate: &DecimalCrate,
) -> TokenStream {
let types = self
.primary_keys
.iter()
Expand All @@ -192,7 +201,7 @@ impl Entity {
.iter()
.find(|col| col.name.eq(&primary_key.name))
.unwrap()
.get_rs_type(date_time_crate)
.get_rs_type(date_time_crate, decimal_crate)
.to_string()
})
.collect::<Vec<_>>();
Expand Down Expand Up @@ -269,7 +278,7 @@ mod tests {
use quote::{format_ident, quote};
use sea_query::{ColumnType, ForeignKeyAction};

use crate::{Column, DateTimeCrate, Entity, PrimaryKey, Relation, RelationType};
use crate::{Column, DateTimeCrate, DecimalCrate, Entity, PrimaryKey, Relation, RelationType};

fn setup() -> Entity {
Entity {
Expand Down Expand Up @@ -381,14 +390,14 @@ mod tests {
let entity = setup();

for (i, elem) in entity
.get_column_rs_types(&DateTimeCrate::Chrono)
.get_column_rs_types(&DateTimeCrate::Chrono, &crate::DecimalCrate::Decimal)
.into_iter()
.enumerate()
{
assert_eq!(
elem.to_string(),
entity.columns[i]
.get_rs_type(&DateTimeCrate::Chrono)
.get_rs_type(&DateTimeCrate::Chrono, &DecimalCrate::Decimal)
.to_string()
);
}
Expand Down Expand Up @@ -490,10 +499,10 @@ mod tests {

assert_eq!(
entity
.get_primary_key_rs_type(&DateTimeCrate::Chrono)
.get_primary_key_rs_type(&DateTimeCrate::Chrono, &DecimalCrate::Decimal)
.to_string(),
entity.columns[0]
.get_rs_type(&DateTimeCrate::Chrono)
.get_rs_type(&DateTimeCrate::Chrono, &DecimalCrate::Decimal)
.to_string()
);
}
Expand Down
Loading

0 comments on commit f1c491c

Please sign in to comment.