Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
refactor: move functions and constants to test mod top
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <[email protected]>
  • Loading branch information
tareknaser committed Feb 10, 2024
1 parent 51a6e10 commit b3205f2
Showing 1 changed file with 99 additions and 99 deletions.
198 changes: 99 additions & 99 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,105 @@ mod tests {
assert!(result.is_ok());
}

const ACTIONS_COUNT: i32 = 4;

enum FileAction {
CREATE = 0, // create new file by new path
UPDATE, // new content by existing path
DELETE, // remove the file by existing path
MOVE, // same content, new path
}

impl From<i32> for FileAction {
fn from(value: i32) -> Self {
match value {
0 => FileAction::CREATE,
1 => FileAction::UPDATE,
2 => FileAction::DELETE,
3 => FileAction::MOVE,
_ => unreachable!("Invalid ActionType value"),
}
}
}

const FILE_NAME: &str = "test_";
const FILE_COUNT: i32 = 3;
const FILE_RAND_NAME_SIZE: usize = 7;

// create randomize file name
fn new_rand_filename() -> String {
let rng = rand::thread_rng();
rng.sample_iter(&Alphanumeric)
.take(FILE_RAND_NAME_SIZE)
.map(char::from)
.collect()
}

fn generate_random_update(
root_path: PathBuf,
cur_name: Option<&str>,
new_name: Option<&str>,
) -> i32 {
let mut rng = rand::thread_rng();
let rand_num: i32 = rng.gen_range(0..ACTIONS_COUNT);

let mut cur_file_path = root_path.clone();
if let Some(file_name) = cur_name {
cur_file_path.push(file_name);
}

let mut new_file_name = "";
let mut new_file_path = root_path.clone();
if let Some(file_name) = new_name {
new_file_name = file_name;
new_file_path.push(file_name);
}

match rand_num.try_into() {
Ok(FileAction::CREATE) => {
create_file_at(
root_path.clone(),
Some(DATA_SIZE_2),
Some(&new_file_name),
);
}
Ok(FileAction::UPDATE) => {
let mut file = File::create(cur_file_path.as_path())
.expect("Unable to create file");

let ten_millis = time::Duration::from_millis(10);
thread::sleep(ten_millis);

modify_file(&mut file);
}
Ok(FileAction::DELETE) => {
std::fs::remove_file(cur_file_path.clone())
.expect("Should remove file successfully");
}
Ok(FileAction::MOVE) => {
std::fs::rename(cur_file_path, new_file_path)
.expect("Should rename file successfully");
}
Err(_) => println!("rnd_num error"),
}

return rand_num;
}

fn create_temp_files_in_directory(root_path: PathBuf) {
for i in 0..FILE_COUNT {
let data_size: u64 = (i + 10).try_into().unwrap();
let mut create_file_name = String::from(FILE_NAME);
create_file_name.push_str(&i.to_string());

create_file_at(
root_path.clone(),
Some(data_size),
Some(&create_file_name),
);
}
}

// resource index build

#[test]
Expand Down Expand Up @@ -1102,105 +1201,6 @@ mod tests {
assert!(new2 > new1);
}

const ACTIONS_COUNT: i32 = 4;

enum FileAction {
CREATE = 0, // create new file by new path
UPDATE, // new content by existing path
DELETE, // remove the file by existing path
MOVE, // same content, new path
}

impl From<i32> for FileAction {
fn from(value: i32) -> Self {
match value {
0 => FileAction::CREATE,
1 => FileAction::UPDATE,
2 => FileAction::DELETE,
3 => FileAction::MOVE,
_ => unreachable!("Invalid ActionType value"),
}
}
}

const FILE_NAME: &str = "test_";
const FILE_COUNT: i32 = 3;
const FILE_RAND_NAME_SIZE: usize = 7;

// create randomize file name
fn new_rand_filename() -> String {
let rng = rand::thread_rng();
rng.sample_iter(&Alphanumeric)
.take(FILE_RAND_NAME_SIZE)
.map(char::from)
.collect()
}

fn generate_random_update(
root_path: PathBuf,
cur_name: Option<&str>,
new_name: Option<&str>,
) -> i32 {
let mut rng = rand::thread_rng();
let rand_num: i32 = rng.gen_range(0..ACTIONS_COUNT);

let mut cur_file_path = root_path.clone();
if let Some(file_name) = cur_name {
cur_file_path.push(file_name);
}

let mut new_file_name = "";
let mut new_file_path = root_path.clone();
if let Some(file_name) = new_name {
new_file_name = file_name;
new_file_path.push(file_name);
}

match rand_num.try_into() {
Ok(FileAction::CREATE) => {
create_file_at(
root_path.clone(),
Some(DATA_SIZE_2),
Some(&new_file_name),
);
}
Ok(FileAction::UPDATE) => {
let mut file = File::create(cur_file_path.as_path())
.expect("Unable to create file");

let ten_millis = time::Duration::from_millis(10);
thread::sleep(ten_millis);

modify_file(&mut file);
}
Ok(FileAction::DELETE) => {
std::fs::remove_file(cur_file_path.clone())
.expect("Should remove file successfully");
}
Ok(FileAction::MOVE) => {
std::fs::rename(cur_file_path, new_file_path)
.expect("Should rename file successfully");
}
Err(_) => println!("rnd_num error"),
}

return rand_num;
}

fn create_temp_files_in_directory(root_path: PathBuf) {
for i in 0..FILE_COUNT {
let data_size: u64 = (i + 10).try_into().unwrap();
let mut create_file_name = String::from(FILE_NAME);
create_file_name.push_str(&i.to_string());

create_file_at(
root_path.clone(),
Some(data_size),
Some(&create_file_name),
);
}
}

#[test]
fn update_all_compare_track_one_methods() {
run_test_and_clean_up(|path| {
Expand Down

0 comments on commit b3205f2

Please sign in to comment.