Skip to content

Commit 10b127f

Browse files
committed
add a test for #64
1 parent 901e97f commit 10b127f

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/webserver/database/sql.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,20 @@ fn sqlpage_func_name(func_name_parts: &[Ident]) -> &str {
449449
mod test {
450450
use super::*;
451451

452-
fn parse_stmt(sql: &str) -> Statement {
453-
let mut ast = Parser::parse_sql(&PostgreSqlDialect {}, sql).unwrap();
452+
fn parse_stmt<D: Dialect>(sql: &str, dialect: D) -> Statement {
453+
let mut ast = Parser::parse_sql(&dialect, sql).unwrap();
454454
assert_eq!(ast.len(), 1);
455455
ast.pop().unwrap()
456456
}
457457

458+
fn parse_postgres_stmt(sql: &str) -> Statement {
459+
parse_stmt(sql, PostgreSqlDialect {})
460+
}
461+
458462
#[test]
459463
fn test_statement_rewrite() {
460-
let mut ast = parse_stmt("select $a from t where $x > $a OR $x = sqlpage.cookie('cookoo')");
464+
let mut ast =
465+
parse_postgres_stmt("select $a from t where $x > $a OR $x = sqlpage.cookie('cookoo')");
461466
let parameters = ParameterExtractor::extract_parameters(&mut ast, AnyKind::Postgres);
462467
assert_eq!(
463468
ast.to_string(),
@@ -477,19 +482,19 @@ mod test {
477482

478483
#[test]
479484
fn test_mssql_statement_rewrite() {
480-
let mut ast = parse_stmt("select '' || $1 from t");
485+
let mut ast = parse_stmt("select '' || $1 from [a schema].[a table]", MsSqlDialect {});
481486
let parameters = ParameterExtractor::extract_parameters(&mut ast, AnyKind::Mssql);
482487
assert_eq!(
483488
ast.to_string(),
484-
"SELECT CONCAT('', CAST(@p1 AS VARCHAR)) FROM t"
489+
"SELECT CONCAT('', CAST(@p1 AS VARCHAR)) FROM [a schema].[a table]"
485490
);
486491
assert_eq!(parameters, [StmtParam::GetOrPost("1".to_string()),]);
487492
}
488493

489494
#[test]
490495
fn test_static_extract() {
491496
assert_eq!(
492-
extract_static_simple_select(&parse_stmt(
497+
extract_static_simple_select(&parse_postgres_stmt(
493498
"select 'hello' as hello, 42 as answer, null as nothing, 'world' as hello"
494499
)),
495500
Some(
@@ -508,37 +513,39 @@ mod test {
508513
#[test]
509514
fn test_static_extract_doesnt_match() {
510515
assert_eq!(
511-
extract_static_simple_select(&parse_stmt(
516+
extract_static_simple_select(&parse_postgres_stmt(
512517
"select 'hello' as hello, 42 as answer limit 0"
513518
)),
514519
None
515520
);
516521
assert_eq!(
517-
extract_static_simple_select(&parse_stmt(
522+
extract_static_simple_select(&parse_postgres_stmt(
518523
"select 'hello' as hello, 42 as answer order by 1"
519524
)),
520525
None
521526
);
522527
assert_eq!(
523-
extract_static_simple_select(&parse_stmt(
528+
extract_static_simple_select(&parse_postgres_stmt(
524529
"select 'hello' as hello, 42 as answer offset 1"
525530
)),
526531
None
527532
);
528533
assert_eq!(
529-
extract_static_simple_select(&parse_stmt(
534+
extract_static_simple_select(&parse_postgres_stmt(
530535
"select 'hello' as hello, 42 as answer where 1 = 0"
531536
)),
532537
None
533538
);
534539
assert_eq!(
535-
extract_static_simple_select(&parse_stmt(
540+
extract_static_simple_select(&parse_postgres_stmt(
536541
"select 'hello' as hello, 42 as answer FROM t"
537542
)),
538543
None
539544
);
540545
assert_eq!(
541-
extract_static_simple_select(&parse_stmt("select x'CAFEBABE' as hello, 42 as answer")),
546+
extract_static_simple_select(&parse_postgres_stmt(
547+
"select x'CAFEBABE' as hello, 42 as answer"
548+
)),
542549
None
543550
);
544551
}

0 commit comments

Comments
 (0)