|
| 1 | +use actix_web::test; |
| 2 | +use sqlpage::AppState; |
| 3 | +use std::time::Duration; |
| 4 | +use tokio::sync::oneshot; |
| 5 | +use tokio::task::JoinHandle; |
| 6 | + |
| 7 | +#[actix_web::test] |
| 8 | +async fn run_all_sql_test_files() { |
| 9 | + let app_data = crate::common::make_app_data().await; |
| 10 | + let test_files = get_sql_test_files(); |
| 11 | + |
| 12 | + // Create a shutdown channel for the echo server |
| 13 | + let (shutdown_tx, shutdown_rx) = oneshot::channel(); |
| 14 | + // Start echo server once for all tests |
| 15 | + let echo_handle = crate::common::start_echo_server(shutdown_rx); |
| 16 | + |
| 17 | + // Wait for echo server to be ready |
| 18 | + wait_for_echo_server().await; |
| 19 | + |
| 20 | + for test_file in test_files { |
| 21 | + let test_result = run_sql_test(&test_file, &app_data, &echo_handle).await; |
| 22 | + assert_test_result(test_result, &test_file); |
| 23 | + } |
| 24 | + |
| 25 | + // Signal the echo server to shut down |
| 26 | + let _ = shutdown_tx.send(()); |
| 27 | + // Wait for echo server to complete after all tests with a timeout |
| 28 | + match tokio::time::timeout(Duration::from_secs(2), echo_handle).await { |
| 29 | + Ok(_) => (), |
| 30 | + Err(_) => panic!("Echo server did not shut down within 2 seconds"), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +async fn wait_for_echo_server() { |
| 35 | + let client = awc::Client::default(); |
| 36 | + let start = std::time::Instant::now(); |
| 37 | + let timeout = Duration::from_secs(5); |
| 38 | + |
| 39 | + while start.elapsed() < timeout { |
| 40 | + match client.get("http://localhost:62802/").send().await { |
| 41 | + Ok(_) => return, |
| 42 | + Err(_) => { |
| 43 | + tokio::time::sleep(Duration::from_millis(100)).await; |
| 44 | + continue; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + panic!("Echo server did not become ready within 5 seconds"); |
| 49 | +} |
| 50 | + |
| 51 | +fn get_sql_test_files() -> Vec<std::path::PathBuf> { |
| 52 | + let path = std::path::Path::new("tests/sql_test_files"); |
| 53 | + std::fs::read_dir(path) |
| 54 | + .unwrap() |
| 55 | + .filter_map(|entry| { |
| 56 | + let entry = entry.ok()?; |
| 57 | + let path = entry.path(); |
| 58 | + if path.extension()? == "sql" { |
| 59 | + Some(path) |
| 60 | + } else { |
| 61 | + None |
| 62 | + } |
| 63 | + }) |
| 64 | + .collect() |
| 65 | +} |
| 66 | + |
| 67 | +async fn run_sql_test( |
| 68 | + test_file: &std::path::Path, |
| 69 | + app_data: &actix_web::web::Data<AppState>, |
| 70 | + _echo_handle: &JoinHandle<()>, |
| 71 | +) -> anyhow::Result<String> { |
| 72 | + let test_file_path = test_file.to_string_lossy().replace('\\', "/"); |
| 73 | + let req_str = format!("/{test_file_path}?x=1"); |
| 74 | + |
| 75 | + let resp = tokio::time::timeout( |
| 76 | + Duration::from_secs(5), |
| 77 | + crate::common::req_path_with_app_data(&req_str, app_data.clone()), |
| 78 | + ) |
| 79 | + .await |
| 80 | + .map_err(|e| anyhow::anyhow!("Test request timed out after 5 seconds: {}", e))??; |
| 81 | + |
| 82 | + let body = test::read_body(resp).await; |
| 83 | + Ok(String::from_utf8(body.to_vec())?) |
| 84 | +} |
| 85 | + |
| 86 | +fn assert_test_result(result: anyhow::Result<String>, test_file: &std::path::Path) { |
| 87 | + let (body, stem) = get_test_body_and_stem(result, test_file); |
| 88 | + assert_html_response(&body, test_file); |
| 89 | + let lowercase_body = body.to_lowercase(); |
| 90 | + |
| 91 | + if stem.starts_with("it_works") { |
| 92 | + assert_it_works_tests(&body, &lowercase_body, test_file); |
| 93 | + } else if stem.starts_with("error_") { |
| 94 | + assert_error_tests(&stem, &lowercase_body, test_file); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn get_test_body_and_stem( |
| 99 | + result: anyhow::Result<String>, |
| 100 | + test_file: &std::path::Path, |
| 101 | +) -> (String, String) { |
| 102 | + let stem = test_file.file_stem().unwrap().to_str().unwrap().to_string(); |
| 103 | + let body = result |
| 104 | + .unwrap_or_else(|e| panic!("Failed to get response for {}: {}", test_file.display(), e)); |
| 105 | + (body, stem) |
| 106 | +} |
| 107 | + |
| 108 | +fn assert_html_response(body: &str, test_file: &std::path::Path) { |
| 109 | + assert!( |
| 110 | + body.starts_with("<!DOCTYPE html>"), |
| 111 | + "Response to {} is not HTML", |
| 112 | + test_file.display() |
| 113 | + ); |
| 114 | +} |
| 115 | + |
| 116 | +fn assert_it_works_tests(body: &str, lowercase_body: &str, test_file: &std::path::Path) { |
| 117 | + assert!( |
| 118 | + body.contains("It works !"), |
| 119 | + "{}\n{}\nexpected to contain: It works !", |
| 120 | + test_file.display(), |
| 121 | + body |
| 122 | + ); |
| 123 | + assert!( |
| 124 | + !lowercase_body.contains("error"), |
| 125 | + "{}\n{}\nexpected to not contain: error", |
| 126 | + test_file.display(), |
| 127 | + body |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +fn assert_error_tests(stem: &str, lowercase_body: &str, test_file: &std::path::Path) { |
| 132 | + let expected_error = stem |
| 133 | + .strip_prefix("error_") |
| 134 | + .unwrap() |
| 135 | + .replace('_', " ") |
| 136 | + .to_lowercase(); |
| 137 | + assert!( |
| 138 | + lowercase_body.contains(&expected_error), |
| 139 | + "{}\n{}\nexpected to contain: {}", |
| 140 | + test_file.display(), |
| 141 | + lowercase_body, |
| 142 | + expected_error |
| 143 | + ); |
| 144 | + assert!( |
| 145 | + lowercase_body.contains("error"), |
| 146 | + "{}\n{}\nexpected to contain: error", |
| 147 | + test_file.display(), |
| 148 | + lowercase_body |
| 149 | + ); |
| 150 | +} |
0 commit comments