Skip to content

Commit

Permalink
add a test for --generate-path= (#115)
Browse files Browse the repository at this point in the history
* wip: adding generate_path tests

* tests: fixing

* chore: fixed compilation

* fix: generate path lifetime

* chore: adding generate path test

* chore: change test name

* fix
  • Loading branch information
JasonShin authored Jun 10, 2024
1 parent 217d5b1 commit 9557940
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 42 deletions.
5 changes: 1 addition & 4 deletions src/common/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ impl Config {
pub fn get_log_level(file_config_path: &PathBuf) -> LogLevel {
let file_based_config = fs::read_to_string(file_config_path);
let file_based_config = &file_based_config.map(|f| serde_json::from_str::<SqlxConfig>(f.as_str()).unwrap());
let log_level_from_file = file_based_config
.as_ref()
.ok()
.and_then(|config| config.log_level);
let log_level_from_file = file_based_config.as_ref().ok().and_then(|config| config.log_level);

CLI_ARGS.log_level.or(log_level_from_file).unwrap_or(LogLevel::Info)
}
Expand Down
1 change: 0 additions & 1 deletion src/common/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,3 @@ macro_rules! error {
}
});
}

4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ async fn main() -> Result<()> {
let ext = &CLI_ARGS.ext;

info!("Scanning {:?} for SQLs with extension {:?}", source_folder, ext);
// If CLI_ARGS.generate_types is true, it will clear the single TS file so `execute` will generate a new one from scratch

// If CLI_ARGS.generate_types is true, it will clear the single TS file so `execute` will generate a new one from scratch
clear_single_ts_file_if_exists()?;

let files = scan_folder(source_folder, ext);
Expand Down
4 changes: 1 addition & 3 deletions src/parser/tag.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::common::SQL;
use swc_common::MultiSpan;
use swc_ecma_ast::{
BlockStmt, ClassMember, Expr, OptChainBase, Pat, Prop, PropOrSpread, SuperProp, VarDeclarator,
};
use swc_ecma_ast::{BlockStmt, ClassMember, Expr, OptChainBase, Pat, Prop, PropOrSpread, SuperProp, VarDeclarator};

use super::{get_var_decl_name_from_key, recurse_and_find_sql};

Expand Down
1 change: 0 additions & 1 deletion src/ts_generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ pub fn write_single_ts_file(sqls_to_write: String) -> Result<()> {
let mut file_to_write = OpenOptions::new()
.create(true)
.read(true)

.append(true)
.open(&output)?;

Expand Down
31 changes: 29 additions & 2 deletions test-utils/src/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use serde;
use serde::{Deserialize, Serialize};

Expand All @@ -21,12 +23,14 @@ pub struct TestConfig {
pub db_user: String,
pub db_pass: Option<String>,
pub db_name: String,
pub generate_path: Option<PathBuf>,
pub generate_types: bool,
pub config_file_name: Option<String>,
}

impl TestConfig {
pub fn new(db_type: &str, generate_types: bool, config_file_name: Option<String>) -> Self {
pub fn new(db_type: &str, generate_types: bool, generate_path: Option<PathBuf>, config_file_name: Option<String>) -> Self {
let generate_path = generate_path.clone();
if db_type == "mysql" {
return TestConfig {
db_type: "mysql".into(),
Expand All @@ -36,6 +40,7 @@ impl TestConfig {
db_user: "root".to_string(),
db_pass: None,
db_name: "sqlx-ts".to_string(),
generate_path,
generate_types,
config_file_name,
}
Expand All @@ -48,6 +53,7 @@ impl TestConfig {
db_user: "postgres".to_string(),
db_pass: Some("postgres".to_string()),
db_name: "postgres".to_string(),
generate_path,
generate_types,
config_file_name,
}
Expand Down Expand Up @@ -107,6 +113,7 @@ $(
let db_pass = test_config.db_pass;
let db_name = test_config.db_name;
let config_file_name = test_config.config_file_name;
let generate_path = test_config.generate_path;

// SETUP
let dir = tempdir()?;
Expand All @@ -128,7 +135,15 @@ $(
.arg(format!("--db-user={db_user}"))
.arg(format!("--db-name={db_name}"));

println!("checking generate types {:?}", test_config.generate_types);
if &generate_path.is_some() == &true {
let generate_path = generate_path.clone();
let generate_path = generate_path.unwrap();
let generate_path = generate_path.as_path();
let generate_path = parent_path.join(generate_path);
let generate_path = generate_path.display();
cmd.arg(format!("--generate-path={generate_path}"));
}

if (test_config.generate_types) {
cmd.arg("-g");
}
Expand All @@ -152,6 +167,18 @@ $(

let generated_types: &str = $generated_types.clone();

if generate_path.is_some() {
let generate_path = parent_path.join(generate_path.unwrap().as_path());
let type_file = fs::read_to_string(generate_path);
let type_file = type_file.unwrap();

assert_eq!(
generated_types.trim().to_string().flatten(),
type_file.trim().to_string().flatten()
);
return Ok(());
}

let type_file = fs::read_to_string(parent_path.join("index.queries.ts"));
if type_file.is_ok() {
let type_file = type_file.unwrap().clone();
Expand Down
8 changes: 4 additions & 4 deletions tests/covert_to_camelcase.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Test suites for converting any case to camelCase if generateTypes.convertToCamelCase is true
///
#[cfg(test)]
mod string_functions_tests {
mod convert_camelcase_tests {
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::env;
Expand All @@ -15,7 +15,7 @@ mod string_functions_tests {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(retain_original, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase1.json".to_string())),
run_test!(retain_original, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase1.json".to_string())),
//// TS query ////
r#"
const someQuery = sql`
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface ISomeQueryQuery {
);

#[rustfmt::skip]
run_test!(convert_camelcase, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase2.json".to_string())),
run_test!(convert_camelcase, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase2.json".to_string())),

//// TS query ////
r#"
Expand Down Expand Up @@ -76,7 +76,7 @@ export interface ISomeQueryQuery {
);

#[rustfmt::skip]
run_test!(retain_original_on_missing_config, TestConfig::new("postgres", true, Some(".sqlxrc.camelcase3.json".to_string())),
run_test!(retain_original_on_missing_config, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase3.json".to_string())),

//// TS query ////
r#"
Expand Down
48 changes: 48 additions & 0 deletions tests/generate_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/// Test suites for converting any case to camelCase if generateTypes.convertToCamelCase is true
///
#[cfg(test)]
mod generate_path_tests {
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::env;
use std::fs;
use std::io::Write;
use std::process::Command;
use std::path::PathBuf;
use tempfile::tempdir;

use pretty_assertions::assert_eq;
use test_utils::test_utils::TSString;
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_generate_path, TestConfig::new("postgres", true, Some(PathBuf::from("types/types.ts")), Some(".sqlxrc.camelcase1.json".to_string())),
//// TS query ////
r#"
const someQuery = sql`
SELECT
food_type,
id AS HelloWorld,
id AS hello_world
FROM items;
`
"#,

//// Generated TS interfaces ////
r#"
export type SomeQueryParams = [];
export interface ISomeQueryResult {
HelloWorld: number;
food_type: string;
hello_world: number;
};
export interface ISomeQueryQuery {
params: SomeQueryParams;
result: ISomeQueryResult;
};
"#
);

}
4 changes: 2 additions & 2 deletions tests/mysql_delete_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod mysql_delete_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand All @@ -40,7 +40,7 @@ export interface ISomeDeleteQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_subquery, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_subquery, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down
4 changes: 2 additions & 2 deletions tests/mysql_insert_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod mysql_insert_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand All @@ -39,7 +39,7 @@ export interface ISomeInputQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down
8 changes: 4 additions & 4 deletions tests/mysql_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod mysql_query_parameters_tests {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down Expand Up @@ -45,7 +45,7 @@ export interface ISomeQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_in_list, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_in_list, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down Expand Up @@ -75,7 +75,7 @@ export interface ISomeQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_in_subqueries, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_in_subqueries, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down Expand Up @@ -114,7 +114,7 @@ export interface ISomeQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_subqueries, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_subqueries, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down
4 changes: 2 additions & 2 deletions tests/mysql_update_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod mysql_update_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand All @@ -40,7 +40,7 @@ export interface ISomeUpdateQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None),
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("mysql", true, None, None),

//// TS query ////
r#"
Expand Down
4 changes: 2 additions & 2 deletions tests/postgres_delete_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod mysql_delete_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_binary_ops, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand All @@ -40,7 +40,7 @@ export interface ISomeDeleteQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_subquery, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_subquery, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand Down
4 changes: 2 additions & 2 deletions tests/postgres_insert_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod postgres_insert_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand All @@ -39,7 +39,7 @@ export interface ISomeInputQueryQuery {
"#);

#[rustfmt::skip]
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_multiple_rows_of_values, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand Down
2 changes: 1 addition & 1 deletion tests/postgres_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod postgres_query_parameters_tests {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_flat_list_of_binary_ops, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand Down
2 changes: 1 addition & 1 deletion tests/postgres_update_query_parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod postgres_update_query_parameters {
use test_utils::{run_test, sandbox::TestConfig};

#[rustfmt::skip]
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None),
run_test!(should_pick_query_params_from_single_row_of_values, TestConfig::new("postgres", true, None, None),

//// TS query ////
r#"
Expand Down
Loading

0 comments on commit 9557940

Please sign in to comment.