| 65 | |
| 66 | #[tokio::test] |
| 67 | async fn test_fast_path_detection() -> Result<(), Box<dyn std::error::Error>> { |
| 68 | // Use a temporary file instead of in-memory database |
| 69 | let test_id = Uuid::new_v4().to_string().replace("-", ""); |
| 70 | let db_path = format!("/tmp/pgsqlite_test_{test_id}.db"); |
| 71 | |
| 72 | let db_handler = Arc::new(DbHandler::new(&db_path)?); |
| 73 | |
| 74 | // Create tables |
| 75 | db_handler.execute("CREATE TABLE simple (id INTEGER, name TEXT)").await?; |
| 76 | db_handler.execute("CREATE TABLE with_decimal (id INTEGER, price DECIMAL)").await?; |
| 77 | |
| 78 | // These should use fast path (simple queries on non-DECIMAL tables) |
| 79 | let fast_queries = vec![ |
| 80 | "INSERT INTO simple (id, name) VALUES (1, 'test')", |
| 81 | "SELECT * FROM simple", |
| 82 | "UPDATE simple SET name = 'updated' WHERE id = 1", |
| 83 | "DELETE FROM simple WHERE id = 1", |
| 84 | ]; |
| 85 | |
| 86 | for query in fast_queries { |
| 87 | println!("Testing fast path for: {query}"); |
| 88 | let start = Instant::now(); |
| 89 | if query.starts_with("SELECT") { |
| 90 | db_handler.query(query).await?; |
| 91 | } else { |
| 92 | db_handler.execute(query).await?; |
| 93 | } |
| 94 | let duration = start.elapsed(); |
| 95 | println!(" Executed in: {duration:?}"); |
| 96 | } |
| 97 | |
| 98 | // These should NOT use fast path (queries on DECIMAL tables) |
| 99 | let slow_queries = vec![ |
| 100 | "INSERT INTO with_decimal (id, price) VALUES (1, 9.99)", |
| 101 | "SELECT * FROM with_decimal", |
| 102 | "UPDATE with_decimal SET price = 19.99 WHERE id = 1", |
| 103 | ]; |
| 104 | |
| 105 | for query in slow_queries { |
| 106 | println!("Testing non-fast path for: {query}"); |
| 107 | let start = Instant::now(); |
| 108 | if query.starts_with("SELECT") { |
| 109 | db_handler.query(query).await?; |
| 110 | } else { |
| 111 | db_handler.execute(query).await?; |
| 112 | } |
| 113 | let duration = start.elapsed(); |
| 114 | println!(" Executed in: {duration:?}"); |
| 115 | } |
| 116 | |
| 117 | // Clean up |
| 118 | drop(db_handler); |
| 119 | let _ = std::fs::remove_file(&db_path); |
| 120 | let _ = std::fs::remove_file(format!("{db_path}-journal")); |
| 121 | let _ = std::fs::remove_file(format!("{db_path}-wal")); |
| 122 | let _ = std::fs::remove_file(format!("{db_path}-shm")); |
| 123 | |
| 124 | Ok(()) |