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")
}

View File

@@ -1,13 +1,12 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
use crate::db;
#[test]
fn test_database_connection() {
// Create a temporary directory for the database
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("test.db");
// Create a temporary database
let (_temp_dir, _conn, db_path) = create_temp_db();
// Try to open the database
let result = db::open(db_path);
@@ -18,12 +17,8 @@ mod tests {
#[test]
fn test_database_item_queries() {
// Create a temporary directory for the database
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("test_items.db");
// Open the database
let conn = db::open(db_path).expect("Failed to open database");
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// Try to query all items (should be empty in new DB)
let items = db::query_all_items(&conn);

View File

@@ -1,26 +1,16 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
use crate::db;
use crate::db::Meta;
#[test]
fn test_database_meta_operations() {
// Create a temporary directory for the database
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("test_meta.db");
// Open the database
let conn = db::open(db_path).expect("Failed to open database");
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// First insert an item to have a valid ID
let item = crate::db::Item {
id: None,
ts: chrono::Utc::now(),
size: Some(100),
compression: crate::compression_engine::CompressionType::None.to_string(),
};
let item_id = db::insert_item(&conn, item).expect("Failed to insert item");
let item_id = create_test_item(&conn);
// Create a test meta with the valid item ID
let meta = Meta {

View File

@@ -1,26 +1,16 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
use crate::db;
use crate::db::Tag;
#[test]
fn test_database_tag_operations() {
// Create a temporary directory for the database
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("test_tags.db");
// Open the database
let conn = db::open(db_path).expect("Failed to open database");
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// First insert an item to have a valid ID
let item = crate::db::Item {
id: None,
ts: chrono::Utc::now(),
size: Some(100),
compression: crate::compression_engine::CompressionType::None.to_string(),
};
let item_id = db::insert_item(&conn, item).expect("Failed to insert item");
let item_id = create_test_item(&conn);
// Create a test tag with the valid item ID
let tag = Tag {
@@ -35,12 +25,8 @@ mod tests {
#[test]
fn test_database_item_tag_operations() {
// Create a temporary directory for the database
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let db_path = temp_dir.path().join("test_item_tags.db");
// Open the database
let conn = db::open(db_path).expect("Failed to open database");
// Create a temporary database
let (_temp_dir, conn, _db_path) = create_temp_db();
// Try to delete tags for non-existent item
let item = crate::db::Item {

View File

@@ -1,27 +1,19 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_dir, create_empty_temp_file};
use std::fs::File;
#[test]
fn test_delete_mode_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
let temp_dir = create_temp_dir();
assert!(temp_dir.path().exists());
}
#[test]
fn test_delete_mode_file_creation_and_deletion() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let test_file = temp_dir.path().join("test_delete.txt");
// Create a test file
{
let _file = File::create(&test_file).expect("Failed to create test file");
}
let temp_dir = create_temp_dir();
let test_file = create_empty_temp_file(&temp_dir, "test_delete.txt");
// Verify file exists before deletion test
assert!(test_file.exists());

View File

@@ -1,13 +1,9 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::test_temp_dir_setup;
#[test]
fn test_diff_mode_basic_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
assert!(temp_dir.path().exists());
test_temp_dir_setup();
}
}

View File

@@ -1,20 +1,16 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_dir, test_temp_dir_setup};
#[test]
fn test_get_mode_basic_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
assert!(temp_dir.path().exists());
test_temp_dir_setup();
}
#[test]
fn test_get_mode_path_operations() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_dir = create_temp_dir();
let test_file = temp_dir.path().join("test_get.txt");
// Test path creation

View File

@@ -1,26 +1,16 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use std::fs::File;
use std::io::Write;
use crate::tests::common::test_helpers::{create_temp_dir, test_file_creation};
use std::path::PathBuf;
#[test]
fn test_info_mode_file_operations() {
// Create a temporary directory for testing
let dir = TempDir::new().expect("Failed to create temp directory");
let file_path = dir.path().join("info_test.txt");
// Create a test file
{
let mut file = File::create(&file_path).expect("Failed to create test file");
writeln!(file, "This is a test file for info mode").expect("Failed to write to test file");
writeln!(file, "With multiple lines").expect("Failed to write second line");
}
// Verify file exists and has content
assert!(file_path.exists());
let dir = create_temp_dir();
let content = "This is a test file for info mode\nWith multiple lines\n";
let file_path = test_file_creation(&dir, "info_test.txt", content);
// Additional verification specific to info mode
let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata");
assert!(metadata.len() > 0);
}

View File

@@ -1,20 +1,16 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::{create_temp_dir, test_temp_dir_setup};
#[test]
fn test_list_mode_basic_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
assert!(temp_dir.path().exists());
test_temp_dir_setup();
}
#[test]
fn test_list_mode_directory_operations() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
let temp_dir = create_temp_dir();
// Test reading directory contents (should be empty)
let entries: Vec<_> = std::fs::read_dir(temp_dir.path())

View File

@@ -1,20 +1,12 @@
#[cfg(test)]
mod tests {
use tempfile::tempdir;
use std::fs::File;
use std::io::Write;
use crate::tests::common::test_helpers::{create_temp_dir, create_temp_file_with_content, create_empty_temp_file};
#[test]
fn test_save_mode_basic_functionality() {
// Create a temporary directory for testing
let dir = tempdir().expect("Failed to create temporary directory");
let file_path = dir.path().join("test_input.txt");
// Create a test file
{
let mut file = File::create(&file_path).expect("Failed to create test file");
writeln!(file, "test content for save mode").expect("Failed to write to test file");
}
let dir = create_temp_dir();
let file_path = create_temp_file_with_content(&dir, "test_input.txt", "test content for save mode\n");
// Verify file was created
assert!(file_path.exists());
@@ -26,14 +18,8 @@ mod tests {
#[test]
fn test_save_mode_empty_file() {
// Create a temporary directory for testing
let dir = tempdir().expect("Failed to create temporary directory");
let file_path = dir.path().join("empty_test.txt");
// Create an empty test file
{
let _file = File::create(&file_path).expect("Failed to create empty test file");
// File is automatically closed when it goes out of scope
}
let dir = create_temp_dir();
let file_path = create_empty_temp_file(&dir, "empty_test.txt");
// Verify empty file was created
assert!(file_path.exists());

View File

@@ -1,13 +1,9 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::test_temp_dir_setup;
#[test]
fn test_status_mode_basic_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
assert!(temp_dir.path().exists());
test_temp_dir_setup();
}
}

View File

@@ -1,13 +1,9 @@
#[cfg(test)]
mod tests {
use tempfile::TempDir;
use crate::tests::common::test_helpers::test_temp_dir_setup;
#[test]
fn test_update_mode_basic_setup() {
// Create a temporary directory for testing
let temp_dir = TempDir::new().expect("Failed to create temp directory");
// Test that we can work with temporary directories
assert!(temp_dir.path().exists());
test_temp_dir_setup();
}
}