Skip to content

Commit 12daf3e

Browse files
committed
Refactor test files for improved clarity and consistency
- Updated SQL queries in CSV data tests to use uppercase column names. - Simplified database table management in core tests by separating drop and create statements. - Enhanced transaction tests to utilize an enum for database type handling.
1 parent f5a679d commit 12daf3e

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

tests/core/mod.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use actix_web::{http::StatusCode, test};
2-
use sqlpage::{webserver, AppState};
2+
use sqlpage::{
3+
webserver::{self, make_placeholder},
4+
AppState,
5+
};
36
use sqlx::Executor as _;
47

58
use crate::common::{make_app_data_from_config, req_path, req_path_with_app_data, test_config};
@@ -47,21 +50,21 @@ async fn test_routing_with_db_fs() {
4750
config.site_prefix = "/prefix/".to_string();
4851
let state = AppState::init(&config).await.unwrap();
4952

53+
let drop_sql = "DROP TABLE IF EXISTS sqlpage_files";
54+
state.db.connection.execute(drop_sql).await.unwrap();
5055
let create_table_sql =
5156
sqlpage::filesystem::DbFsQueries::get_create_table_sql(state.db.info.database_type);
52-
state
53-
.db
54-
.connection
55-
.execute(format!("DROP TABLE IF EXISTS sqlpage_files; {create_table_sql}").as_ref())
57+
state.db.connection.execute(create_table_sql).await.unwrap();
58+
let insert_sql = format!(
59+
"INSERT INTO sqlpage_files(path, contents) VALUES ('on_db.sql', {})",
60+
make_placeholder(state.db.info.kind, 1)
61+
);
62+
sqlx::query(&insert_sql)
63+
.bind("select ''text'' as component, ''Hi from db !'' AS contents;".as_bytes())
64+
.execute(&state.db.connection)
5665
.await
5766
.unwrap();
5867

59-
let insert_sql = match state.db.connection.any_kind() {
60-
sqlx::any::AnyKind::Mssql => "INSERT INTO sqlpage_files(path, contents) VALUES ('on_db.sql', CONVERT(VARBINARY(MAX), 'select ''text'' as component, ''Hi from db !'' AS contents;'))",
61-
_ => "INSERT INTO sqlpage_files(path, contents) VALUES ('on_db.sql', 'select ''text'' as component, ''Hi from db !'' AS contents;')"
62-
};
63-
state.db.connection.execute(insert_sql).await.unwrap();
64-
6568
let state = AppState::init(&config).await.unwrap();
6669
let app_data = actix_web::web::Data::new(state);
6770

tests/data_formats/csv_data.sql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
select 'csv' as component, ';' as separator;
2-
select 0 as id, 'Hello World !' as msg
1+
select
2+
'csv' as component,
3+
';' as separator;
4+
5+
select
6+
0 as ID,
7+
'Hello World !' as MSG
38
union all
4-
select 1 as id, 'Tu gères '';'' et ''"'' ?' as msg;
9+
select
10+
1 as ID,
11+
'Tu gères '';'' et ''"'' ?' as MSG;

tests/data_formats/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async fn test_csv_body() -> actix_web::Result<()> {
4343
let body_str = String::from_utf8(body.to_vec()).unwrap();
4444
assert_eq!(
4545
body_str,
46-
"id;msg\n0;Hello World !\n1;\"Tu gères ';' et '\"\"' ?\"\n"
46+
"ID;MSG\n0;Hello World !\n1;\"Tu gères ';' et '\"\"' ?\"\n"
4747
);
4848
Ok(())
4949
}

tests/transactions/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use actix_web::{http::StatusCode, test};
2-
use sqlpage::webserver::http::main_handler;
2+
use sqlpage::webserver::{database::SupportedDatabase, http::main_handler};
33

44
use crate::common::{get_request_to_with_data, make_app_data};
55

66
#[actix_web::test]
77
async fn test_transaction_error() -> actix_web::Result<()> {
88
let data = make_app_data().await;
9-
let path = match data.db.to_string().to_lowercase().as_str() {
10-
"mysql" => "/tests/transactions/failed_transaction_mysql.sql",
11-
"mssql" => "/tests/transactions/failed_transaction_mssql.sql",
9+
let path = match data.db.info.database_type {
10+
SupportedDatabase::MySql => "/tests/transactions/failed_transaction_mysql.sql",
11+
SupportedDatabase::Mssql => "/tests/transactions/failed_transaction_mssql.sql",
12+
SupportedDatabase::Snowflake => {
13+
return Ok(()); //snowflake doesn't support transactions
14+
}
1215
_ => "/tests/transactions/failed_transaction.sql",
1316
};
1417
let req = get_request_to_with_data(path, data.clone())

0 commit comments

Comments
 (0)