-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deleted FileFilter folder and added unit tests for setMinFileSize method of fileFilter class in tests folder.
- Loading branch information
Showing
8 changed files
with
145 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import pytest | ||
from twinTrim.dataStructures.fileFilter import FileFilter | ||
|
||
|
||
def test_set_min_file_size_valid(): | ||
"""Test setting valid minimum file size values.""" | ||
file_filter = FileFilter() | ||
|
||
# Test with a larger valid size | ||
file_filter.setMinFileSize("20kb") | ||
assert file_filter.minFileSize == "20kb", "Failed to set min file size to 20kb" | ||
|
||
# Test with a smaller valid size | ||
file_filter.setMinFileSize("5kb") | ||
assert file_filter.minFileSize == "5kb", "Failed to set min file size to 5kb" | ||
|
||
# Test with an edge case (1kb) | ||
file_filter.setMinFileSize("1kb") | ||
assert file_filter.minFileSize == "1kb", "Failed to set min file size to 1kb" | ||
|
||
def test_set_min_file_size_empty_string(): | ||
"""Test setting an empty string for minimum file size.""" | ||
file_filter = FileFilter() | ||
|
||
# Empty string should be allowed since no validation exists | ||
file_filter.setMinFileSize("") | ||
assert file_filter.minFileSize == "", "Failed to set min file size to an empty string" | ||
|
||
def test_set_min_file_size_special_characters(): | ||
"""Test setting special characters or random string as min file size.""" | ||
file_filter = FileFilter() | ||
|
||
# Special characters should be accepted since no validation exists | ||
file_filter.setMinFileSize("!!invalid!!") | ||
assert file_filter.minFileSize == "!!invalid!!", "Failed to set min file size to special characters" | ||
|
||
def test_set_min_file_size_numeric_string(): | ||
"""Test setting numeric string as minimum file size.""" | ||
file_filter = FileFilter() | ||
|
||
# Numeric string should be accepted, even without units | ||
file_filter.setMinFileSize("123") | ||
assert file_filter.minFileSize == "123", "Failed to set min file size to a numeric string" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import threading | ||
import pytest | ||
from twinTrim.dataStructures.fileMetadata import add_or_update_normal_file, normalStore, normalStore_lock | ||
|
||
# Mocking the get_file_hash function so that we don't have to worry about the actual file | ||
def mock_get_file_hash(file_path): | ||
return f"hash_{file_path}" | ||
|
||
# This will automatically replaces the get_file_hash function with the mock_get_file_hash function | ||
@pytest.fixture(autouse=True) | ||
def mock_get_file_hash_func(monkeypatch): | ||
monkeypatch.setattr("twinTrim.dataStructures.fileMetadata.get_file_hash", mock_get_file_hash) | ||
|
||
# Automatically reset the normalStore before each test | ||
@pytest.fixture(autouse=True) | ||
def reset_normal_store(): | ||
normalStore.clear() | ||
|
||
def test_add_or_update_normal_file_concurrently(): | ||
"Test that normalStore is consistant when add_or_update_normal_file is called concurrently with different file paths by multiple threads" | ||
|
||
# List of file paths to be added concurrently | ||
file_paths = [f"file_{i}" for i in range(10)] | ||
|
||
# this function will be called by each thread to add a file path | ||
def worker(file_path): | ||
add_or_update_normal_file(file_path) | ||
|
||
# Threads for concurrent execution | ||
threads = [threading.Thread(target=worker, args=(file_path,)) for file_path in file_paths] | ||
|
||
# starting all the threads for concurrent execution | ||
for thread in threads: | ||
thread.start() | ||
|
||
# waiting for all the threads to finish | ||
for thread in threads: | ||
thread.join() | ||
|
||
# Checking that normalStore has exactly 10 unique file hashes (since all file paths are unique) | ||
assert len(normalStore) == 10 | ||
|
||
# Checking that each file hash has exactly 1 file path | ||
for file_path in file_paths: | ||
file_hash = mock_get_file_hash(file_path) | ||
assert file_hash in normalStore | ||
assert normalStore[file_hash].filepaths == [file_path] | ||
|
||
def test_add_or_update_normal_file_with_duplicates_concurrently(): | ||
"Test that adding duplicate file paths to normalStore does not create duplicate entries" | ||
|
||
file_paths = "duplicate_file.txt" | ||
num_threads = 5 | ||
|
||
# this function will be called by each thread to add a file path | ||
def worker(): | ||
add_or_update_normal_file(file_paths) | ||
|
||
# Create a list of threads to add the same file path concurrently | ||
threads = [threading.Thread(target=worker) for _ in range(num_threads)] | ||
|
||
# starting all the threads for concurrent execution | ||
for thread in threads: | ||
thread.start() | ||
|
||
# waiting for all the threads to finish | ||
for thread in threads: | ||
thread.join() | ||
|
||
# Checking that normalStore has exactly 1 unique file hash (since all file paths are the same) | ||
file_hash = mock_get_file_hash(file_paths) | ||
assert len(normalStore) == 1 | ||
assert file_hash in normalStore | ||
|
||
# Checking that the file path is added only once despite being added by multiple threads | ||
assert normalStore[file_hash].filepaths == [file_paths] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters