test: add method to dump query plans

This commit is contained in:
oxalica 2024-09-21 08:05:58 -04:00
parent 5c4dfd4a96
commit 8876480732
2 changed files with 36 additions and 2 deletions

View file

@ -51,7 +51,7 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install sqlite - name: Install sqlite
run: sudo apt-get install --no-install-recommends -y libsqlite3-dev run: sudo apt-get install --no-install-recommends -y libsqlite3-dev sqlite3
- name: Disable high-priority rust-toolchain.toml - name: Disable high-priority rust-toolchain.toml
run: rm rust-toolchain.toml run: rm rust-toolchain.toml

View file

@ -1,6 +1,12 @@
#![expect(clippy::print_stdout, reason = "allowed in tests for debugging")] #![expect(clippy::print_stdout, reason = "allowed in tests for debugging")]
use std::fmt::Write;
use std::fs;
use std::process::{Command, Stdio};
use super::*; use super::*;
const SRC_PATH: &str = "src/database.rs";
#[test] #[test]
fn init_sql_valid() { fn init_sql_valid() {
let conn = Connection::open_in_memory().unwrap(); let conn = Connection::open_in_memory().unwrap();
@ -20,7 +26,7 @@ fn init_sql_valid() {
#[test] #[test]
fn stmt_cache_capacity() { fn stmt_cache_capacity() {
let src = std::fs::read_to_string("src/database.rs").unwrap(); let src = fs::read_to_string(SRC_PATH).unwrap();
let sql_cnt = src.matches("prepare_cached_and_bind!").count(); let sql_cnt = src.matches("prepare_cached_and_bind!").count();
println!("found {sql_cnt} SQLs"); println!("found {sql_cnt} SQLs");
assert_ne!(sql_cnt, 0); assert_ne!(sql_cnt, 0);
@ -29,3 +35,31 @@ fn stmt_cache_capacity() {
"stmt cache capacity {STMT_CACHE_CAPACITY} is too small, found {sql_cnt} SQLs", "stmt cache capacity {STMT_CACHE_CAPACITY} is too small, found {sql_cnt} SQLs",
); );
} }
#[test]
#[ignore = "only for debugging"]
fn dump_query_plan() {
let src = fs::read_to_string(SRC_PATH).unwrap();
let mut cmds = String::new();
for (pos, _) in src.match_indices("prepare_cached_and_bind!") {
let line = src[..pos].matches('\n').count() + 1;
let sql = src[pos..]
.lines()
// Skip macro call, first argument, `r"`.
.skip(3)
.take_while(|line| line.trim() != "\"")
.flat_map(|line| [line.trim(), "\n"])
.collect::<String>();
writeln!(cmds, "SELECT '{SRC_PATH}:{line}';").unwrap();
writeln!(cmds, "EXPLAIN QUERY PLAN {sql};").unwrap();
}
let st = Command::new("sqlite3")
.stdin(Stdio::null())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.args([":memory:", ".read ./schema.sql", &cmds])
.status()
.unwrap();
assert!(st.success());
}