Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
fankaiLiu committed Nov 17, 2023
1 parent 41123df commit 12d4a65
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 3 deletions.
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
unicode-xid = "0.2.4"
rust-i18n = "2"
itertools = "0.12.0"
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::Parser;

mod test;
mod utils;
use i18n::set_locale;
mod i18n;
Expand Down
1 change: 1 addition & 0 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod test_write_project;
86 changes: 86 additions & 0 deletions src/test/test_write_project.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#[cfg(test)]
mod tests {
use crate::{
utils::{
create_project::write_project_file,
get_selection::{DbConnectionType, DbType, TemplateType, UserSelected},
},
Project,
};
use itertools::Itertools;
use std::path::Path;

#[test]
fn test_write_project_all_combinations() {
let template_types = vec![TemplateType::SalvoWebSite, TemplateType::SalvoWebApi];
//let db_types = vec![DbType::Sqlite, DbType::Mysql, DbType::Postgres, DbType::Mssql];
let db_types = vec![DbType::Sqlite];
let db_conn_types = vec![
DbConnectionType::Sqlx,
DbConnectionType::SeaOrm,
DbConnectionType::Diesel,
DbConnectionType::Rbatis,
DbConnectionType::Mongodb,
DbConnectionType::Nothing,
];

// Generate all combinations
let combinations = template_types
.iter()
.cartesian_product(db_types.iter())
.cartesian_product(db_conn_types.iter())
.map(|((template_type, db_type), db_conn_type)| (template_type, db_type, db_conn_type))
.collect::<Vec<_>>();

// Test each combination
for (template_type, db_type, db_conn_type) in combinations {
// Generate a unique project name for each combination
let project_name = format!(
"test_{}_{}_{}",
format!("{:?}", template_type),
format!("{:?}", db_type),
format!("{:?}", db_conn_type)
);

let path_str = format!("target/{}", project_name);
std::fs::remove_dir_all(&path_str).unwrap_or_else(|_| {});
let path = Path::new(&path_str);

let user_selected = UserSelected {
template_type: *template_type,
db_type: *db_type,
db_conn_type: *db_conn_type,
};
let project = Project {
project_name: project_name.clone(),
lang: Some("zh".to_string()),
};
match write_project_file(path, user_selected, project) {
Ok(()) => {
let output = std::process::Command::new("cargo")
.arg("check")
.current_dir(&path_str)
.output()
.expect("failed to execute process");
if !output.status.success() {
eprintln!(
"Failed on combination: template_type={:?}, db_type={:?}, db_conn_type={:?}",
template_type, db_type, db_conn_type
);
eprintln!("Output: {:?}", output);
assert!(false);
}
}
Err(e) => {
eprintln!(
"Failed to write project file on combination: template_type={:?}, db_type={:?}, db_conn_type={:?}",
template_type, db_type, db_conn_type
);
eprintln!("Error: {:?}", e);
assert!(false);
}
}
std::fs::remove_dir_all(&path_str).unwrap_or_else(|_| {});
}
}
}
2 changes: 1 addition & 1 deletion src/utils/create_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn after_print_info(project_name: &String, config: UserSelected) {
}
}

fn write_project_file(
pub fn write_project_file(
project_path: &Path,
user_selected: UserSelected,
project: Project,
Expand Down

0 comments on commit 12d4a65

Please sign in to comment.