mirror of
				https://github.com/Blah-IM/blahrs.git
				synced 2025-10-31 02:31:37 +00:00 
			
		
		
		
	test: add method to dump query plans
This commit is contained in:
		
							parent
							
								
									5c4dfd4a96
								
							
						
					
					
						commit
						8876480732
					
				
					 2 changed files with 36 additions and 2 deletions
				
			
		
							
								
								
									
										2
									
								
								.github/workflows/ci.yaml
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci.yaml
									
										
									
									
										vendored
									
									
								
							|  | @ -51,7 +51,7 @@ jobs: | |||
|         uses: actions/checkout@v4 | ||||
| 
 | ||||
|       - 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 | ||||
|         run: rm rust-toolchain.toml | ||||
|  |  | |||
|  | @ -1,6 +1,12 @@ | |||
| #![expect(clippy::print_stdout, reason = "allowed in tests for debugging")] | ||||
| use std::fmt::Write; | ||||
| use std::fs; | ||||
| use std::process::{Command, Stdio}; | ||||
| 
 | ||||
| use super::*; | ||||
| 
 | ||||
| const SRC_PATH: &str = "src/database.rs"; | ||||
| 
 | ||||
| #[test] | ||||
| fn init_sql_valid() { | ||||
|     let conn = Connection::open_in_memory().unwrap(); | ||||
|  | @ -20,7 +26,7 @@ fn init_sql_valid() { | |||
| 
 | ||||
| #[test] | ||||
| 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(); | ||||
|     println!("found {sql_cnt} SQLs"); | ||||
|     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", | ||||
|     ); | ||||
| } | ||||
| 
 | ||||
| #[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()); | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 oxalica
						oxalica