use shardex::concurrent::ConcurrentShardex;
use shardex::cow_index::CowShardexIndex;
use shardex::shardex_index::ShardexIndex;
use shardex::{ShardexConfig, ShardexError};
use tempfile::TempDir;
pub mod test_constants {
pub const DEFAULT_SHARD_SIZE: usize = 100;
#[allow(dead_code)]
pub const DEFAULT_VECTOR_SIZE: usize = 128;
}
pub mod test_error_messages {
pub const FAILED_TO_CREATE_TEMP_DIR: &str = "Failed to create test temporary directory";
#[allow(dead_code)]
pub const FAILED_TO_CREATE_INDEX: &str = "Failed to create test index";
}
pub fn create_temp_dir_for_test() -> TempDir {
TempDir::new().expect(test_error_messages::FAILED_TO_CREATE_TEMP_DIR)
}
#[allow(dead_code)]
pub struct TestSetupBuilder {
test_name: String,
vector_size: usize,
shard_size: usize,
}
#[allow(dead_code)]
impl TestSetupBuilder {
pub fn new(test_name: &str) -> Self {
Self {
test_name: test_name.to_string(),
vector_size: test_constants::DEFAULT_VECTOR_SIZE,
shard_size: test_constants::DEFAULT_SHARD_SIZE,
}
}
pub fn with_vector_size(mut self, size: usize) -> Self {
self.vector_size = size;
self
}
pub fn with_shard_size(mut self, size: usize) -> Self {
self.shard_size = size;
self
}
pub fn build(self) -> (TempDir, ShardexConfig) {
let temp_dir = create_temp_dir_for_test();
let config = ShardexConfig::new()
.directory_path(temp_dir.path().to_path_buf())
.vector_size(self.vector_size)
.shard_size(self.shard_size);
(temp_dir, config)
}
pub fn build_with_index(self) -> Result<(TempDir, ShardexConfig, ShardexIndex), ShardexError> {
let (temp_dir, config) = self.build();
let index = ShardexIndex::create(config.clone()).map_err(|e| ShardexError::InvalidInput {
field: "index_creation".to_string(),
reason: format!("{}: {}", test_error_messages::FAILED_TO_CREATE_INDEX, e),
suggestion: "Check directory permissions and disk space".to_string(),
})?;
Ok((temp_dir, config, index))
}
}
#[allow(dead_code)]
pub struct TestEnvironment {
pub temp_dir: TempDir,
#[allow(dead_code)]
pub test_name: String,
}
impl TestEnvironment {
#[allow(dead_code)]
pub fn new(test_name: &str) -> Self {
let temp_dir =
TempDir::new().unwrap_or_else(|e| panic!("Failed to create temp dir for test {}: {}", test_name, e));
Self {
temp_dir,
test_name: test_name.to_string(),
}
}
#[allow(dead_code)]
pub fn path(&self) -> &std::path::Path {
self.temp_dir.path()
}
}
#[allow(dead_code)]
pub fn create_test_concurrent_shardex(test_env: &TestEnvironment) -> ConcurrentShardex {
let config = ShardexConfig::new()
.directory_path(test_env.path())
.vector_size(64)
.shard_size(100);
let index = ShardexIndex::create(config).expect("Failed to create index");
let cow_index = CowShardexIndex::new(index);
ConcurrentShardex::new(cow_index)
}