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:
@@ -4,3 +4,5 @@
|
|||||||
pub mod is_binary_tests;
|
pub mod is_binary_tests;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod status_tests;
|
pub mod status_tests;
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod test_helpers;
|
||||||
|
|||||||
64
src/tests/common/test_helpers.rs
Normal file
64
src/tests/common/test_helpers.rs
Normal 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")
|
||||||
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_database_connection() {
|
fn test_database_connection() {
|
||||||
// Create a temporary directory for the database
|
// Create a temporary database
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
let (_temp_dir, _conn, db_path) = create_temp_db();
|
||||||
let db_path = temp_dir.path().join("test.db");
|
|
||||||
|
|
||||||
// Try to open the database
|
// Try to open the database
|
||||||
let result = db::open(db_path);
|
let result = db::open(db_path);
|
||||||
@@ -18,12 +17,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_database_item_queries() {
|
fn test_database_item_queries() {
|
||||||
// Create a temporary directory for the database
|
// Create a temporary database
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
let (_temp_dir, conn, _db_path) = create_temp_db();
|
||||||
let db_path = temp_dir.path().join("test_items.db");
|
|
||||||
|
|
||||||
// Open the database
|
|
||||||
let conn = db::open(db_path).expect("Failed to open database");
|
|
||||||
|
|
||||||
// Try to query all items (should be empty in new DB)
|
// Try to query all items (should be empty in new DB)
|
||||||
let items = db::query_all_items(&conn);
|
let items = db::query_all_items(&conn);
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::db::Meta;
|
use crate::db::Meta;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_database_meta_operations() {
|
fn test_database_meta_operations() {
|
||||||
// Create a temporary directory for the database
|
// Create a temporary database
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
let (_temp_dir, conn, _db_path) = create_temp_db();
|
||||||
let db_path = temp_dir.path().join("test_meta.db");
|
|
||||||
|
|
||||||
// Open the database
|
|
||||||
let conn = db::open(db_path).expect("Failed to open database");
|
|
||||||
|
|
||||||
// First insert an item to have a valid ID
|
// First insert an item to have a valid ID
|
||||||
let item = crate::db::Item {
|
let item_id = create_test_item(&conn);
|
||||||
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");
|
|
||||||
|
|
||||||
// Create a test meta with the valid item ID
|
// Create a test meta with the valid item ID
|
||||||
let meta = Meta {
|
let meta = Meta {
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_db, create_test_item};
|
||||||
use crate::db;
|
use crate::db;
|
||||||
use crate::db::Tag;
|
use crate::db::Tag;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_database_tag_operations() {
|
fn test_database_tag_operations() {
|
||||||
// Create a temporary directory for the database
|
// Create a temporary database
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
let (_temp_dir, conn, _db_path) = create_temp_db();
|
||||||
let db_path = temp_dir.path().join("test_tags.db");
|
|
||||||
|
|
||||||
// Open the database
|
|
||||||
let conn = db::open(db_path).expect("Failed to open database");
|
|
||||||
|
|
||||||
// First insert an item to have a valid ID
|
// First insert an item to have a valid ID
|
||||||
let item = crate::db::Item {
|
let item_id = create_test_item(&conn);
|
||||||
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");
|
|
||||||
|
|
||||||
// Create a test tag with the valid item ID
|
// Create a test tag with the valid item ID
|
||||||
let tag = Tag {
|
let tag = Tag {
|
||||||
@@ -35,12 +25,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_database_item_tag_operations() {
|
fn test_database_item_tag_operations() {
|
||||||
// Create a temporary directory for the database
|
// Create a temporary database
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
let (_temp_dir, conn, _db_path) = create_temp_db();
|
||||||
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");
|
|
||||||
|
|
||||||
// Try to delete tags for non-existent item
|
// Try to delete tags for non-existent item
|
||||||
let item = crate::db::Item {
|
let item = crate::db::Item {
|
||||||
|
|||||||
@@ -1,27 +1,19 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_dir, create_empty_temp_file};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_delete_mode_setup() {
|
fn test_delete_mode_setup() {
|
||||||
// Create a temporary directory for testing
|
let temp_dir = create_temp_dir();
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
|
||||||
|
|
||||||
// Test that we can work with temporary directories
|
|
||||||
assert!(temp_dir.path().exists());
|
assert!(temp_dir.path().exists());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_delete_mode_file_creation_and_deletion() {
|
fn test_delete_mode_file_creation_and_deletion() {
|
||||||
// Create a temporary directory for testing
|
// 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_delete.txt");
|
let test_file = create_empty_temp_file(&temp_dir, "test_delete.txt");
|
||||||
|
|
||||||
// Create a test file
|
|
||||||
{
|
|
||||||
let _file = File::create(&test_file).expect("Failed to create test file");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify file exists before deletion test
|
// Verify file exists before deletion test
|
||||||
assert!(test_file.exists());
|
assert!(test_file.exists());
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::test_temp_dir_setup;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_diff_mode_basic_setup() {
|
fn test_diff_mode_basic_setup() {
|
||||||
// Create a temporary directory for testing
|
test_temp_dir_setup();
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
|
||||||
|
|
||||||
// Test that we can work with temporary directories
|
|
||||||
assert!(temp_dir.path().exists());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_dir, test_temp_dir_setup};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_mode_basic_setup() {
|
fn test_get_mode_basic_setup() {
|
||||||
// Create a temporary directory for testing
|
test_temp_dir_setup();
|
||||||
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]
|
#[test]
|
||||||
fn test_get_mode_path_operations() {
|
fn test_get_mode_path_operations() {
|
||||||
// Create a temporary directory for testing
|
// 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");
|
let test_file = temp_dir.path().join("test_get.txt");
|
||||||
|
|
||||||
// Test path creation
|
// Test path creation
|
||||||
|
|||||||
@@ -1,26 +1,16 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_dir, test_file_creation};
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_info_mode_file_operations() {
|
fn test_info_mode_file_operations() {
|
||||||
// Create a temporary directory for testing
|
// Create a temporary directory for testing
|
||||||
let dir = TempDir::new().expect("Failed to create temp directory");
|
let dir = create_temp_dir();
|
||||||
let file_path = dir.path().join("info_test.txt");
|
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);
|
||||||
// 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());
|
|
||||||
|
|
||||||
|
// Additional verification specific to info mode
|
||||||
let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata");
|
let metadata = std::fs::metadata(&file_path).expect("Failed to get file metadata");
|
||||||
assert!(metadata.len() > 0);
|
assert!(metadata.len() > 0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::{create_temp_dir, test_temp_dir_setup};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_list_mode_basic_setup() {
|
fn test_list_mode_basic_setup() {
|
||||||
// Create a temporary directory for testing
|
test_temp_dir_setup();
|
||||||
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]
|
#[test]
|
||||||
fn test_list_mode_directory_operations() {
|
fn test_list_mode_directory_operations() {
|
||||||
// Create a temporary directory for testing
|
// 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)
|
// Test reading directory contents (should be empty)
|
||||||
let entries: Vec<_> = std::fs::read_dir(temp_dir.path())
|
let entries: Vec<_> = std::fs::read_dir(temp_dir.path())
|
||||||
|
|||||||
@@ -1,20 +1,12 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::tempdir;
|
use crate::tests::common::test_helpers::{create_temp_dir, create_temp_file_with_content, create_empty_temp_file};
|
||||||
use std::fs::File;
|
|
||||||
use std::io::Write;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_save_mode_basic_functionality() {
|
fn test_save_mode_basic_functionality() {
|
||||||
// Create a temporary directory for testing
|
// Create a temporary directory for testing
|
||||||
let dir = tempdir().expect("Failed to create temporary directory");
|
let dir = create_temp_dir();
|
||||||
let file_path = dir.path().join("test_input.txt");
|
let file_path = create_temp_file_with_content(&dir, "test_input.txt", "test content for save mode\n");
|
||||||
|
|
||||||
// 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify file was created
|
// Verify file was created
|
||||||
assert!(file_path.exists());
|
assert!(file_path.exists());
|
||||||
@@ -26,14 +18,8 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_save_mode_empty_file() {
|
fn test_save_mode_empty_file() {
|
||||||
// Create a temporary directory for testing
|
// Create a temporary directory for testing
|
||||||
let dir = tempdir().expect("Failed to create temporary directory");
|
let dir = create_temp_dir();
|
||||||
let file_path = dir.path().join("empty_test.txt");
|
let file_path = create_empty_temp_file(&dir, "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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify empty file was created
|
// Verify empty file was created
|
||||||
assert!(file_path.exists());
|
assert!(file_path.exists());
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::test_temp_dir_setup;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_status_mode_basic_setup() {
|
fn test_status_mode_basic_setup() {
|
||||||
// Create a temporary directory for testing
|
test_temp_dir_setup();
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
|
||||||
|
|
||||||
// Test that we can work with temporary directories
|
|
||||||
assert!(temp_dir.path().exists());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use tempfile::TempDir;
|
use crate::tests::common::test_helpers::test_temp_dir_setup;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_update_mode_basic_setup() {
|
fn test_update_mode_basic_setup() {
|
||||||
// Create a temporary directory for testing
|
test_temp_dir_setup();
|
||||||
let temp_dir = TempDir::new().expect("Failed to create temp directory");
|
|
||||||
|
|
||||||
// Test that we can work with temporary directories
|
|
||||||
assert!(temp_dir.path().exists());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user