feat: create common test helpers to reduce duplication across test modules

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-15 11:23:17 -03:00
parent d0e62ad980
commit d5c956e626
13 changed files with 107 additions and 122 deletions

View File

@@ -4,3 +4,5 @@
pub mod is_binary_tests;
#[cfg(test)]
pub mod status_tests;
#[cfg(test)]
pub mod test_helpers;

View File

@@ -0,0 +1,64 @@
//! Common test utilities and helper functions to reduce duplication in tests
use tempfile::{TempDir, TempPath};
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use rusqlite::Connection;
use crate::db;
/// Create a temporary directory for testing
pub fn create_temp_dir() -> TempDir {
TempDir::new().expect("Failed to create temporary directory")
}
/// Create a temporary file with the given content
pub fn create_temp_file_with_content(dir: &TempDir, filename: &str, content: &str) -> PathBuf {
let file_path = dir.path().join(filename);
let mut file = File::create(&file_path).expect("Failed to create test file");
write!(file, "{}", content).expect("Failed to write to test file");
file_path
}
/// Create an empty temporary file
pub fn create_empty_temp_file(dir: &TempDir, filename: &str) -> PathBuf {
let file_path = dir.path().join(filename);
File::create(&file_path).expect("Failed to create empty test file");
file_path
}
/// Helper to test basic temporary directory setup
pub fn test_temp_dir_setup() {
let temp_dir = create_temp_dir();
assert!(temp_dir.path().exists());
}
/// Helper to test file creation and verification
pub fn test_file_creation(dir: &TempDir, filename: &str, content: &str) -> PathBuf {
let file_path = create_temp_file_with_content(dir, filename, content);
assert!(file_path.exists());
let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata");
assert!(metadata.len() > 0);
file_path
}
/// Create a temporary database for testing
pub fn create_temp_db() -> (TempDir, Connection, PathBuf) {
let temp_dir = create_temp_dir();
let db_path = temp_dir.path().join("test.db");
let conn = db::open(db_path.clone()).expect("Failed to open database");
(temp_dir, conn, db_path)
}
/// Create a test item in the database
pub fn create_test_item(conn: &Connection) -> i64 {
let item = crate::db::Item {
id: None,
ts: chrono::Utc::now(),
size: Some(100),
compression: crate::compression_engine::CompressionType::None.to_string(),
};
db::insert_item(conn, item).expect("Failed to insert item")
}