|
| 1 | +use actix_web::{http::StatusCode, test}; |
| 2 | +use sqlx::any::AnyKind; |
| 3 | +use sqlx::connection::Connection as _; |
| 4 | + |
| 5 | +use sqlpage::webserver::database::SupportedDatabase; |
| 6 | +use sqlpage::webserver::http::main_handler; |
| 7 | + |
| 8 | +use crate::common::{get_request_to_with_data, make_app_data}; |
| 9 | + |
| 10 | +async fn rendered_page( |
| 11 | + path: &str, |
| 12 | + data: actix_web::web::Data<sqlpage::AppState>, |
| 13 | +) -> actix_web::Result<String> { |
| 14 | + let req = get_request_to_with_data(path, data).await?.to_srv_request(); |
| 15 | + let resp = main_handler(req).await?; |
| 16 | + assert_eq!(resp.status(), StatusCode::OK); |
| 17 | + Ok(String::from_utf8(test::read_body(resp).await.to_vec()).unwrap()) |
| 18 | +} |
| 19 | + |
| 20 | +#[actix_web::test] |
| 21 | +async fn test_a_parameter_in_a_projection_reaches_the_database() -> actix_web::Result<()> { |
| 22 | + let data = make_app_data().await; |
| 23 | + let path = match data.db.info.database_type { |
| 24 | + SupportedDatabase::Mssql => "/tests/parameter_binding/parameter_in_projection_mssql.sql", |
| 25 | + SupportedDatabase::Oracle => return Ok(()), // no CREATE TEMPORARY TABLE |
| 26 | + _ => "/tests/parameter_binding/parameter_in_projection.sql", |
| 27 | + }; |
| 28 | + |
| 29 | + let page = rendered_page(&format!("{path}?x=1447"), data).await?; |
| 30 | + assert!( |
| 31 | + page.contains("1447"), |
| 32 | + "{page}\nexpected the bound parameter to reach the inserted row" |
| 33 | + ); |
| 34 | + Ok(()) |
| 35 | +} |
| 36 | + |
| 37 | +#[actix_web::test] |
| 38 | +async fn test_parameterized_pages_leave_a_prepared_statement_in_the_cache() -> actix_web::Result<()> |
| 39 | +{ |
| 40 | + let data = make_app_data().await; |
| 41 | + if data.db.info.kind == AnyKind::Mssql { |
| 42 | + return Ok(()); // the MSSQL backend keeps no statement cache |
| 43 | + } |
| 44 | + |
| 45 | + for _ in 0..3 { |
| 46 | + let page = rendered_page( |
| 47 | + "/tests/parameter_binding/echo_parameter.sql?x=1447", |
| 48 | + data.clone(), |
| 49 | + ) |
| 50 | + .await?; |
| 51 | + assert!( |
| 52 | + page.contains("1447"), |
| 53 | + "{page}\nexpected the bound parameter to reach the query" |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + let connection = data.db.connection.acquire().await.unwrap(); |
| 58 | + assert!( |
| 59 | + connection.cached_statements_size() > 0, |
| 60 | + "{:?} ran a parameterized query three times without caching a prepared statement", |
| 61 | + data.db.info.kind |
| 62 | + ); |
| 63 | + Ok(()) |
| 64 | +} |
0 commit comments