use super::{extract_embedded_sql_from_source, EmbeddedSqlOptions, TrustedSqlTag};
use std::path::Path;
#[test]
fn spliced_names_keep_table_and_column_choices_correlated() {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).join(
"../../test-cases/rules/postgres-no-test-state-mutation/fixture/spliced-names/tests/spliced.test.ts",
);
let source = std::fs::read_to_string(&path).unwrap();
let options = EmbeddedSqlOptions::configured("@example/db", &[]).with_trusted_sql_tags(&[
TrustedSqlTag {
module: "@example/db".into(),
name: "sql".into(),
},
]);
let facts = extract_embedded_sql_from_source(&path, &source, &options);
let sql: Vec<_> = facts.calls[0]
.variants
.iter()
.map(|variant| variant.sql_text.as_str())
.collect();
assert_eq!(
sql,
[
"SELECT status, result IS NOT NULL AS has_result FROM runs_a WHERE id = sql_placeholder_1",
"SELECT status, output IS NOT NULL AS has_result FROM runs_b WHERE id = sql_placeholder_1",
]
);
}
#[test]
fn lookup_keys_cover_numeric_string_indices_and_reject_unbounded_or_escaped_forms() {
let source = r#"
import { read, sql } from "@example/db";
const TABLES = ["runs_a", "runs_b"] as const;
const COLUMNS = { runs_a: "result", runs_b: "output" } as const;
const BY_INDEX = { "0": "runs_a", "1": "runs_b" } as const;
const KEY_ZERO = "0";
const KEY_LEADING_ZERO = "01";
const EMPTY = {} as const;
const WITH_SPREAD = { ...COLUMNS } as const;
const WITH_METHOD = { runs_a() { return "x"; } } as const;
const WITH_PROTO = { "__proto__": "x" } as const;
type ParenColKey = keyof (typeof COLUMNS);
type InterfaceKeyof = keyof { runs_a: string };
type EmptyKeyof = keyof typeof EMPTY;
type SpreadKeyof = keyof typeof WITH_SPREAD;
type MethodKeyof = keyof typeof WITH_METHOD;
type ProtoKeyof = keyof typeof WITH_PROTO;
export function okCases(index: (0 | 1), colKey: ParenColKey) {
read(sql`SELECT * FROM `.append(TABLES[index]));
read(sql`SELECT * FROM `.append(BY_INDEX[index]));
read(sql`SELECT * FROM `.append(TABLES[KEY_ZERO]));
read(sql`SELECT * FROM `.append(colKey));
}
export function rejectedCases(
boolLit: true | false,
tooMany: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16,
ifaceKey: InterfaceKeyof,
emptyKey: EmptyKeyof,
spreadKey: SpreadKeyof,
methodKey: MethodKeyof,
protoKey: ProtoKeyof,
) {
read(sql`SELECT * FROM `.append(TABLES[KEY_LEADING_ZERO]));
read(sql`SELECT * FROM `.append(TABLES[boolLit as never]));
read(sql`SELECT * FROM `.append(TABLES[tooMany]));
read(sql`SELECT * FROM `.append(ifaceKey));
read(sql`SELECT * FROM `.append(emptyKey));
read(sql`SELECT * FROM `.append(spreadKey));
read(sql`SELECT * FROM `.append(methodKey));
read(sql`SELECT * FROM `.append(protoKey));
const pushed = ["runs_a", "runs_b"];
pushed.push("runs_c");
read(sql`SELECT * FROM `.append(pushed[0]));
}
"#;
let options = EmbeddedSqlOptions::configured("@example/db", &[]).with_trusted_sql_tags(&[
TrustedSqlTag {
module: "@example/db".into(),
name: "sql".into(),
},
]);
let facts = extract_embedded_sql_from_source(Path::new("tests/edge.test.ts"), source, &options);
assert_eq!(facts.calls.len(), 13);
let variants = |i: usize| -> Vec<&str> {
facts.calls[i]
.variants
.iter()
.map(|v| v.sql_text.as_str())
.collect()
};
assert_eq!(
variants(0),
["SELECT * FROM runs_a", "SELECT * FROM runs_b"]
);
assert_eq!(
variants(1),
["SELECT * FROM runs_a", "SELECT * FROM runs_b"]
);
assert_eq!(variants(2), ["SELECT * FROM runs_a"]);
assert_eq!(
variants(3),
["SELECT * FROM runs_a", "SELECT * FROM runs_b"]
);
for (i, call) in facts.calls.iter().enumerate().skip(4) {
assert!(
call.is_unanalyzable(),
"call {i} should stay unanalyzable: {call:?}"
);
}
}