Skip to content

Commit ecaf6ce

Browse files
committed
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.
1 parent 2af95a0 commit ecaf6ce

26 files changed

+194
-271
lines changed

tests/common/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,34 @@ pub async fn req_path(
5353
main_handler(req).await
5454
}
5555

56-
pub async fn srv_req_path_with_app_data(
56+
const REQ_TIMEOUT: Duration = Duration::from_secs(8);
57+
pub async fn req_path_with_app_data(
5758
path: impl AsRef<str>,
5859
app_data: Data<AppState>,
59-
) -> actix_web::dev::ServiceRequest {
60-
test::TestRequest::get()
61-
.uri(path.as_ref())
62-
.app_data(app_data)
63-
.insert_header(("cookie", "test_cook=123"))
64-
.insert_header(("authorization", "Basic dGVzdDp0ZXN0")) // test:test
65-
.to_srv_request()
60+
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
61+
req_path_with_app_data_and_accept(path, app_data, header::Accept::html()).await
6662
}
6763

68-
const REQ_TIMEOUT: Duration = Duration::from_secs(8);
69-
pub async fn req_path_with_app_data(
64+
pub async fn req_path_with_app_data_json(
7065
path: impl AsRef<str>,
7166
app_data: Data<AppState>,
67+
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
68+
req_path_with_app_data_and_accept(path, app_data, header::Accept::json()).await
69+
}
70+
71+
async fn req_path_with_app_data_and_accept(
72+
path: impl AsRef<str>,
73+
app_data: Data<AppState>,
74+
accept: header::Accept,
7275
) -> anyhow::Result<actix_web::dev::ServiceResponse> {
7376
let path = path.as_ref();
74-
let req = srv_req_path_with_app_data(path, app_data).await;
77+
let req = test::TestRequest::get()
78+
.uri(path)
79+
.app_data(app_data)
80+
.insert_header(("cookie", "test_cook=123"))
81+
.insert_header(("authorization", "Basic dGVzdDp0ZXN0"))
82+
.insert_header(accept)
83+
.to_srv_request();
7584
let resp = tokio::time::timeout(REQ_TIMEOUT, main_handler(req))
7685
.await
7786
.map_err(|e| anyhow::anyhow!("Request to {path} timed out: {e}"))?

tests/server_timing/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async fn test_server_timing_enabled_in_development() -> actix_web::Result<()> {
3232
let app_data = make_app_data_from_config(config).await;
3333

3434
let req = crate::common::get_request_to_with_data(
35-
"/tests/sql_test_files/it_works_sqrt.sql",
35+
"/tests/sql_test_files/it_works_postgres_cast_syntax.sql",
3636
app_data,
3737
)
3838
.await?
@@ -72,7 +72,7 @@ async fn test_server_timing_enabled_in_development() -> actix_web::Result<()> {
7272

7373
#[actix_web::test]
7474
async fn test_server_timing_format() -> actix_web::Result<()> {
75-
let req = get_request_to("/tests/sql_test_files/it_works_sqrt.sql")
75+
let req = get_request_to("/tests/sql_test_files/it_works_postgres_cast_syntax.sql")
7676
.await?
7777
.to_srv_request();
7878
let resp = main_handler(req).await?;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
select 'text' as component,
2-
case sqlpage.basic_auth_password()
3-
when 'test' then 'It works !'
4-
else 'error: ' || coalesce(sqlpage.basic_auth_password(), 'NULL')
5-
end as contents;
1+
select 'test' as expected, sqlpage.basic_auth_password() as actual;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
select 'text' as component,
2-
case sqlpage.basic_auth_username()
3-
when 'test' then 'It works !'
4-
else 'error: ' || coalesce(sqlpage.basic_auth_username(), 'NULL')
5-
end as contents;
1+
select 'test' as expected, sqlpage.basic_auth_username() as actual;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
select 'text' as component,
2-
case sqlpage.link(coalesce($i_do_not_exist, 'https://example.com'))
3-
when 'https://example.com' then 'It works !'
4-
else 'error: ' || coalesce(sqlpage.link(coalesce($i_do_not_exist, 'https://example.com')), 'NULL')
5-
end AS contents;
1+
select 'https://example.com' as expected, sqlpage.link(coalesce($i_do_not_exist, 'https://example.com')) as actual;
Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
select 'text' as component,
2-
'With "||": ' ||
3-
CASE sqlpage.url_encode('/' || $x)
4-
WHEN '%2F1' THEN 'It works !'
5-
ELSE 'Error: "/1" should be urlencoded to "%2F1"'
6-
END
7-
|| ' | With CONCAT: ' ||
8-
CASE sqlpage.url_encode(CONCAT('/', $x)) -- $x is set to '1' in the test
9-
WHEN '%2F1' THEN 'With CONCAT: It works !'
10-
ELSE 'Error: "/1" should be urlencoded to "%2F1"'
11-
END
12-
|| ' | With a null value: ' ||
13-
CASE COALESCE(sqlpage.url_encode(CONCAT('/', $thisisnull)), 'expected')
14-
WHEN 'expected' THEN 'With a null value: It works !'
15-
ELSE 'Error: a null value concatenated with "/" should be null, and urlencoded to NULL'
16-
END
17-
AS contents;
1+
select '%2F1' as expected, sqlpage.url_encode('/' || $x) as actual;
2+
select '%2F1' as expected, sqlpage.url_encode(CONCAT('/', $x)) as actual;
3+
select 'NULL' as expected, coalesce(sqlpage.url_encode(CONCAT('/', $thisisnull)), 'NULL') as actual;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
select 'text' as component,
2-
case sqlpage.cookie('test_cook')
3-
when '123' then 'It works !'
4-
else 'error: ' || coalesce(sqlpage.cookie('test_cook'), 'NULL')
5-
end AS contents;
1+
select '123' as expected, sqlpage.cookie('test_cook') as actual;
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
set my_decimal = CAST(0.47 AS DECIMAL(3,2));
2-
3-
select 'text' as component,
4-
case $my_decimal
5-
when '0.47' then 'It works !'
6-
else 'error: ' || coalesce($my_decimal, 'NULL')
7-
end AS contents;
1+
set result = CAST(0.47 AS DECIMAL(3,2));
2+
select '0.47' as expected, $result as actual;
Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
11
set res = sqlpage.fetch_with_meta('http://not-a-real-url');
22

3-
select 'text' as component,
4-
case
5-
when $res LIKE '%"error":"Request failed%' then 'It works !'
6-
else CONCAT('Error! Got: ', $res)
7-
end as contents;
3+
select '"error":"Request failed' as expected_contains, $res as actual;

tests/sql_test_files/it_works_fetch_with_meta_simple.sql

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ set fetch_req = '{
88
}';
99
set res = sqlpage.fetch_with_meta($fetch_req);
1010

11-
select 'text' as component,
12-
case
13-
when $res LIKE '%"status":200%' AND $res LIKE '%"headers":{%' AND $res LIKE '%"body":"%' then 'It works !'
14-
else 'Error! Got: ' || $res
15-
end as contents;
11+
select '"status":200' as expected_contains,
12+
'"headers":{' as expected_contains,
13+
'"body":"' as expected_contains,
14+
$res as actual;

0 commit comments

Comments
 (0)