Skip to content

Commit

Permalink
chore: adding generate path test
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Jun 10, 2024
1 parent 08418bc commit b4b1f59
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 64 deletions.
21 changes: 19 additions & 2 deletions test-utils/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,11 @@ $(
.arg(format!("--db-user={db_user}"))
.arg(format!("--db-name={db_name}"));

if generate_path.is_some() {
let generate_path = generate_path.clone().unwrap();
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}"));
}
Expand Down Expand Up @@ -164,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 All @@ -172,6 +187,8 @@ $(
generated_types.trim().to_string().flatten(),
type_file.to_string().flatten()
);
} else {
panic!("Type file not found!");
}
Ok(())
}
Expand Down
64 changes: 2 additions & 62 deletions tests/generate_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ mod generate_path_tests {
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!(retain_original, TestConfig::new("postgres", true, None, Some(".sqlxrc.camelcase1.json".to_string())),
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`
Expand Down Expand Up @@ -44,65 +45,4 @@ export interface ISomeQueryQuery {
"#
);

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

//// TS query ////
r#"
const someQuery = sql`
SELECT
food_type,
id AS HelloWorld1,
id AS hello_world2
FROM items;
`
"#,

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

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

//// TS query ////
r#"
const someQuery = sql`
SELECT
food_type,
id AS HelloWorld1,
id AS hello_world2
FROM items;
`
"#,

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

0 comments on commit b4b1f59

Please sign in to comment.