Helper: creates a temporary SQLite database with the given schema, then runs sqlx-ts on the given TS content, and returns the generated types.
(
schema_sql: &str,
ts_content: &str,
generate_types: bool,
)
| 12 | /// Helper: creates a temporary SQLite database with the given schema, |
| 13 | /// then runs sqlx-ts on the given TS content, and returns the generated types. |
| 14 | fn run_sqlite_test( |
| 15 | schema_sql: &str, |
| 16 | ts_content: &str, |
| 17 | generate_types: bool, |
| 18 | ) -> Result<(String, String), Box<dyn std::error::Error>> { |
| 19 | let dir = tempdir()?; |
| 20 | let parent_path = dir.path(); |
| 21 | |
| 22 | // Create the SQLite database and populate it with the schema |
| 23 | let db_path = parent_path.join("test.db"); |
| 24 | let conn = rusqlite::Connection::open(&db_path)?; |
| 25 | conn.execute_batch(schema_sql)?; |
| 26 | drop(conn); |
| 27 | |
| 28 | // Write the TS file |
| 29 | let file_path = parent_path.join("index.ts"); |
| 30 | let mut temp_file = fs::File::create(&file_path)?; |
| 31 | writeln!(temp_file, "{}", ts_content)?; |
| 32 | |
| 33 | // Run sqlx-ts |
| 34 | let mut cmd = cargo_bin_cmd!("sqlx-ts"); |
| 35 | cmd |
| 36 | .arg(parent_path.to_str().unwrap()) |
| 37 | .arg("--ext=ts") |
| 38 | .arg("--db-type=sqlite") |
| 39 | .arg(format!("--db-name={}", db_path.display())); |
| 40 | |
| 41 | if generate_types { |
| 42 | cmd.arg("-g"); |
| 43 | } |
| 44 | |
| 45 | let output = cmd.output()?; |
| 46 | let stdout = String::from_utf8_lossy(&output.stdout).to_string(); |
| 47 | let stderr = String::from_utf8_lossy(&output.stderr).to_string(); |
| 48 | |
| 49 | assert!( |
| 50 | output.status.success(), |
| 51 | "sqlx-ts failed!\nstdout: {stdout}\nstderr: {stderr}" |
| 52 | ); |
| 53 | assert!( |
| 54 | stdout.contains("No SQL errors detected!"), |
| 55 | "Expected success message in stdout: {stdout}" |
| 56 | ); |
| 57 | |
| 58 | // Read generated types |
| 59 | let type_file_path = parent_path.join("index.queries.ts"); |
| 60 | let type_file = if type_file_path.exists() { |
| 61 | fs::read_to_string(type_file_path)? |
| 62 | } else { |
| 63 | String::new() |
| 64 | }; |
| 65 | |
| 66 | Ok((stdout, type_file)) |
| 67 | } |
| 68 | |
| 69 | #[test] |
| 70 | fn should_validate_simple_select() -> Result<(), Box<dyn std::error::Error>> { |
no outgoing calls