Files
sqlpage--sqlpage/tests/server_timing/mod.rs
Ophir LOJKINE 95f228f383 simplify SQL test files (#1129)
* simplify SQL test files

- Simplified SQL test files by removing unnecessary components and restructuring queries to focus on expected vs actual results.
- Updated the request handling in `run_sql_test` to differentiate between JSON and HTML responses based on test file content.
- Enhanced error handling and assertions for both JSON and HTML responses to improve test reliability and clarity.
- Removed redundant code and improved readability in the test execution flow.

* move more tests to the new expected/actual format

* migrate more tests to the expected/actual format

- Changed references in various test files to point to the new `simple.sql` instead of the outdated `it_works_simple.sql`.
- Removed several obsolete SQL test files that are no longer needed, streamlining the test suite.
- Updated assertions and request paths in the test cases to reflect the new structure and improve clarity.

* Enhance error handling in SQL tests and update SQL syntax

- Added a new function `format_error` to improve error reporting in SQL test assertions, capturing detailed error descriptions and backtraces.
- Updated SQL syntax in `sqrt.sql` to use `INT` instead of `integer` for consistency with SQL standards.

* sqrt test
2025-11-26 00:45:13 +01:00

140 lines
4.3 KiB
Rust

use actix_web::http::StatusCode;
use sqlpage::webserver::http::main_handler;
use crate::common::{get_request_to, make_app_data_from_config, test_config};
#[actix_web::test]
async fn test_server_timing_disabled_in_production() -> actix_web::Result<()> {
let mut config = test_config();
config.environment = sqlpage::app_config::DevOrProd::Production;
let app_data = make_app_data_from_config(config).await;
let req = crate::common::get_request_to_with_data(
"/tests/sql_test_files/component_rendering/simple.sql",
app_data,
)
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(resp.status(), StatusCode::OK);
assert!(
resp.headers().get("Server-Timing").is_none(),
"Server-Timing header should not be present in production mode"
);
Ok(())
}
#[actix_web::test]
async fn test_server_timing_enabled_in_development() -> actix_web::Result<()> {
let mut config = test_config();
config.environment = sqlpage::app_config::DevOrProd::Development;
let app_data = make_app_data_from_config(config).await;
let req = crate::common::get_request_to_with_data(
"/tests/sql_test_files/data/postgres_cast_syntax.sql",
app_data,
)
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(resp.status(), StatusCode::OK);
let server_timing_header = resp
.headers()
.get("Server-Timing")
.expect("Server-Timing header should be present in development mode");
let header_value = server_timing_header.to_str().unwrap();
assert!(
header_value.contains("sql_file;dur="),
"Should contain sql_file timing: {header_value}"
);
assert!(
header_value.contains("parse_req;dur="),
"Should contain parse_req timing: {header_value}"
);
assert!(
header_value.contains("bind_params;dur="),
"Should contain bind_params timing: {header_value}"
);
assert!(
header_value.contains("db_conn;dur="),
"Should contain db_conn timing: {header_value}"
);
assert!(
header_value.contains("row;dur="),
"Should contain row timing: {header_value}"
);
Ok(())
}
#[actix_web::test]
async fn test_server_timing_format() -> actix_web::Result<()> {
let req = get_request_to("/tests/sql_test_files/data/postgres_cast_syntax.sql")
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(resp.status(), StatusCode::OK);
let server_timing_header = resp.headers().get("Server-Timing").unwrap();
let header_value = server_timing_header.to_str().unwrap();
let parts: Vec<&str> = header_value.split(", ").collect();
assert!(parts.len() >= 5, "Should have at least 5 timing events");
for part in parts {
assert!(
part.contains(";dur="),
"Each part should have name;dur= format: {part}"
);
let dur_parts: Vec<&str> = part.split(";dur=").collect();
assert_eq!(dur_parts.len(), 2, "Should have name and duration: {part}");
let duration: f64 = dur_parts[1]
.parse()
.expect("Duration should be a valid number");
assert!(
duration >= 0.0,
"Duration should be non-negative: {duration}"
);
}
Ok(())
}
#[actix_web::test]
async fn test_server_timing_in_redirect() -> actix_web::Result<()> {
let mut config = test_config();
config.environment = sqlpage::app_config::DevOrProd::Development;
let app_data = make_app_data_from_config(config).await;
let req =
crate::common::get_request_to_with_data("/tests/server_timing/redirect_test.sql", app_data)
.await?
.to_srv_request();
let resp = main_handler(req).await?;
assert_eq!(
resp.status(),
StatusCode::FOUND,
"Response should be a redirect"
);
let server_timing_header = resp
.headers()
.get("Server-Timing")
.expect("Server-Timing header should be present in redirect responses");
let header_value = server_timing_header.to_str().unwrap();
assert!(
!header_value.is_empty(),
"Server-Timing header should not be empty: {header_value}"
);
assert!(
header_value.contains(";dur="),
"Server-Timing header should contain timing events: {header_value}"
);
Ok(())
}