Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codegen unit test #38

Merged
merged 1 commit into from
Jul 12, 2021
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
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --all
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ futures = { version = "^0.3" }
futures-util = { version = "^0.3" }
sea-query = { version = "^0.12" }
sea-orm-macros = { path = "sea-orm-macros", optional = true }
sea-orm-codegen = { path = "sea-orm-codegen", optional = true }
serde = { version = "^1.0", features = [ "derive" ] }
sqlx = { version = "^0.5", optional = true }
strum = { git = "https://github.com/SeaQL/strum.git", branch = "sea-orm", version = "^0.21", features = [ "derive", "sea-orm" ] }
Expand All @@ -49,8 +50,9 @@ sea-orm = { path = ".", features = ["sqlx-sqlite", "sqlx-json", "sqlx-chrono", "

[features]
debug-print = []
default = [ "macros", "with-json", "with-chrono", "with-rust_decimal", "mock" ]
default = [ "macros", "codegen", "with-json", "with-chrono", "with-rust_decimal", "mock" ]
macros = [ "sea-orm-macros" ]
codegen = [ "sea-orm-codegen" ]
mock = []
with-json = [ "serde_json", "sea-query/with-json" ]
with-chrono = [ "chrono", "sea-query/with-chrono" ]
Expand All @@ -67,4 +69,4 @@ runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls" ]
runtime-tokio-native-tls = [ "sqlx/runtime-tokio-native-tls" ]
runtime-actix-rustls = [ "sqlx/runtime-actix-rustls" ]
runtime-async-std-rustls = [ "sqlx/runtime-async-std-rustls" ]
runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls" ]
runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls" ]
4 changes: 4 additions & 0 deletions sea-orm-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ syn = { version = "^1", default-features = false, features = [ "derive", "parsin
quote = "^1"
heck = "^0.3"
proc-macro2 = "^1"

[dev-dependencies]
async-std = { version = "^1.9", features = [ "attributes" ] }
sea-orm = { path = "../", features = ["mock", "sqlx-json", "sqlx-chrono", "runtime-async-std-native-tls"] }
193 changes: 180 additions & 13 deletions sea-orm-codegen/src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Column {
}

pub fn get_rs_type(&self) -> TokenStream {
let ident = match self.col_type {
let ident: TokenStream = match self.col_type {
ColumnType::Char(_)
| ColumnType::String(_)
| ColumnType::Text
Expand All @@ -32,18 +32,18 @@ impl Column {
| ColumnType::Date
| ColumnType::Json
| ColumnType::JsonBinary
| ColumnType::Custom(_) => format_ident!("String"),
ColumnType::TinyInteger(_) => format_ident!("i8"),
ColumnType::SmallInteger(_) => format_ident!("i16"),
ColumnType::Integer(_) => format_ident!("i32"),
ColumnType::BigInteger(_) => format_ident!("i64"),
ColumnType::Float(_) | ColumnType::Decimal(_) | ColumnType::Money(_) => {
format_ident!("f32")
}
ColumnType::Double(_) => format_ident!("f64"),
ColumnType::Binary(_) => format_ident!("Vec<u8>"),
ColumnType::Boolean => format_ident!("bool"),
};
| ColumnType::Custom(_) => "String",
ColumnType::TinyInteger(_) => "i8",
ColumnType::SmallInteger(_) => "i16",
ColumnType::Integer(_) => "i32",
ColumnType::BigInteger(_) => "i64",
ColumnType::Float(_) | ColumnType::Decimal(_) | ColumnType::Money(_) => "f32",
ColumnType::Double(_) => "f64",
ColumnType::Binary(_) => "Vec<u8>",
ColumnType::Boolean => "bool",
}
.parse()
.unwrap();
match self.not_null {
true => quote! { #ident },
false => quote! { Option<#ident> },
Expand Down Expand Up @@ -102,6 +102,12 @@ impl Column {
}
}

impl From<ColumnDef> for Column {
fn from(col_def: ColumnDef) -> Self {
(&col_def).into()
}
}

impl From<&ColumnDef> for Column {
fn from(col_def: &ColumnDef) -> Self {
let name = col_def.get_column_name();
Expand Down Expand Up @@ -145,3 +151,164 @@ impl From<&ColumnDef> for Column {
}
}
}

#[cfg(test)]
mod tests {
use crate::Column;
use proc_macro2::TokenStream;
use quote::quote;
use sea_query::{Alias, ColumnDef, ColumnType, SeaRc};

fn setup() -> Vec<Column> {
macro_rules! make_col {
($name:expr, $col_type:expr) => {
Column {
name: $name.to_owned(),
col_type: $col_type,
auto_increment: false,
not_null: false,
unique: false,
}
};
}
vec![
make_col!("id", ColumnType::String(Some(255))),
make_col!(
"cake_id",
ColumnType::Custom(SeaRc::new(Alias::new("cus_col")))
),
make_col!("CakeId", ColumnType::TinyInteger(None)),
make_col!("CakeId", ColumnType::SmallInteger(None)),
make_col!("CakeId", ColumnType::Integer(Some(11))),
make_col!("CakeFillingId", ColumnType::BigInteger(None)),
make_col!("cake-filling-id", ColumnType::Float(None)),
make_col!("CAKE_FILLING_ID", ColumnType::Double(None)),
make_col!("CAKE-FILLING-ID", ColumnType::Binary(None)),
make_col!("CAKE", ColumnType::Boolean),
]
}

#[test]
fn test_get_name_snake_case() {
let columns = setup();
let snack_cases = vec![
"id",
"cake_id",
"cake_id",
"cake_id",
"cake_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake_filling_id",
"cake",
];
for (col, snack_case) in columns.into_iter().zip(snack_cases) {
assert_eq!(col.get_name_snake_case().to_string(), snack_case);
}
}

#[test]
fn test_get_name_camel_case() {
let columns = setup();
let camel_cases = vec![
"Id",
"CakeId",
"CakeId",
"CakeId",
"CakeId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"CakeFillingId",
"Cake",
];
for (col, camel_case) in columns.into_iter().zip(camel_cases) {
assert_eq!(col.get_name_camel_case().to_string(), camel_case);
}
}

#[test]
fn test_get_rs_type() {
let columns = setup();
let rs_types = vec![
"String", "String", "i8", "i16", "i32", "i64", "f32", "f64", "Vec<u8>", "bool",
];
for (mut col, rs_type) in columns.into_iter().zip(rs_types) {
let rs_type: TokenStream = rs_type.parse().unwrap();

col.not_null = true;
assert_eq!(col.get_rs_type().to_string(), quote!(#rs_type).to_string());

col.not_null = false;
assert_eq!(
col.get_rs_type().to_string(),
quote!(Option<#rs_type>).to_string()
);
}
}

#[test]
fn test_get_def() {
let columns = setup();
let col_defs = vec![
"ColumnType::String(Some(255u32)).def()",
"ColumnType::Custom(\"cus_col\".to_owned()).def()",
"ColumnType::TinyInteger.def()",
"ColumnType::SmallInteger.def()",
"ColumnType::Integer.def()",
"ColumnType::BigInteger.def()",
"ColumnType::Float.def()",
"ColumnType::Double.def()",
"ColumnType::Binary.def()",
"ColumnType::Boolean.def()",
];
for (mut col, col_def) in columns.into_iter().zip(col_defs) {
let mut col_def: TokenStream = col_def.parse().unwrap();

col.not_null = true;
assert_eq!(col.get_def().to_string(), col_def.to_string());

col.not_null = false;
col_def.extend(quote!(.null()));
assert_eq!(col.get_def().to_string(), col_def.to_string());

col.unique = true;
col_def.extend(quote!(.unique()));
assert_eq!(col.get_def().to_string(), col_def.to_string());
}
}

#[test]
fn test_from_column_def() {
let column: Column = ColumnDef::new(Alias::new("id")).string().into();
assert_eq!(
column.get_def().to_string(),
quote! {
ColumnType::String(None).def().null()
}
.to_string()
);

let column: Column = ColumnDef::new(Alias::new("id")).string().not_null().into();
assert!(column.not_null);

let column: Column = ColumnDef::new(Alias::new("id"))
.string()
.unique_key()
.not_null()
.into();
assert!(column.unique);
assert!(column.not_null);

let column: Column = ColumnDef::new(Alias::new("id"))
.string()
.auto_increment()
.unique_key()
.not_null()
.into();
assert!(column.auto_increment);
assert!(column.unique);
assert!(column.not_null);
}
}
Loading