Skip to content

Commit fd3a323

Browse files
0xrinegadeclaude
andcommitted
test(investigation_db): Add open_path for isolated test databases
- Add open_path() method for test isolation - Tests use temp directories instead of user paths 🤖 Generated with [Claude Code](https://claude.ai/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a6a4515 commit fd3a323

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/utils/investigation_db.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,24 @@ impl InvestigationDB {
8080
Ok(home.join(".osvm").join("investigations.db"))
8181
}
8282

83+
/// Open investigation database at a specific path (useful for testing)
84+
#[cfg(test)]
85+
pub fn open_path(path: &std::path::Path) -> Result<Self> {
86+
// Ensure parent directory exists
87+
if let Some(parent) = path.parent() {
88+
std::fs::create_dir_all(parent)
89+
.context("Failed to create database directory")?;
90+
}
91+
92+
let conn = Connection::open(path)
93+
.context("Failed to open investigation database")?;
94+
95+
let mut db = Self { conn };
96+
db.init_schema()?;
97+
98+
Ok(db)
99+
}
100+
83101
/// Initialize database schema
84102
fn init_schema(&mut self) -> Result<()> {
85103
self.conn.execute_batch(
@@ -384,10 +402,18 @@ impl InvestigationDB {
384402
#[cfg(test)]
385403
mod tests {
386404
use super::*;
405+
use tempfile::TempDir;
406+
407+
fn setup_test_db() -> (InvestigationDB, TempDir) {
408+
let tmp_dir = TempDir::new().expect("Failed to create temp dir");
409+
let db_path = tmp_dir.path().join("test_investigations.db");
410+
let db = InvestigationDB::open_path(&db_path).expect("Failed to open test db");
411+
(db, tmp_dir)
412+
}
387413

388414
#[test]
389415
fn test_database_operations() {
390-
let mut db = InvestigationDB::open().unwrap();
416+
let (mut db, _tmp_dir) = setup_test_db();
391417

392418
let inv = Investigation {
393419
id: None,

0 commit comments

Comments
 (0)