Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #11 Mock file operations Tests #156

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion tests/test_fileMetadata.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# test_fileMetadata.py
from unittest.mock import mock_open, patch
import pytest
from twinTrim.dataStructures.fileMetadata import FileMetadata
from twinTrim.dataStructures.fileMetadata import normalStore, add_or_update_normal_file, FileMetadata

def test_insert_new_file():
# Test inserting a new file into the metadata
Expand Down Expand Up @@ -35,3 +36,43 @@ def test_insert_multiple_files():
assert len(metadata.filepaths) == 2
assert file_path1 in metadata.filepaths
assert file_path2 in metadata.filepaths

def test_add_or_update_new_file():
file_path = "C:\\Users\\2004s\\Desktop\\dummy\\dummy_5.txt" # Define the mock file path
expected_file_hash = "b1295d8ebb927df19ad74eec6aea72e3" # Use the actual hash computed from the file

# Mock the get_file_hash function to return the expected hash
with patch("twinTrim.utils.get_file_hash", return_value=expected_file_hash), \
patch("builtins.open", mock_open(read_data=b"some binary content")):

# Clear normalStore before the test to avoid conflicts
normalStore.clear()

# Call the function to add a new file
add_or_update_normal_file(file_path)

# Check that the expected file hash is in normalStore
assert expected_file_hash in normalStore.keys(), f"Expected hash '{expected_file_hash}' not found in normalStore"

# Check that the file path was added correctly
assert normalStore[expected_file_hash].filepaths == [file_path], "File path not added correctly"

def test_add_or_update_existing_file():
file_path1 = "C:\\Users\\2004s\\Desktop\\dummy\\dummy_5.txt"
file_path2 = "C:\\Users\\2004s\\Desktop\\dummy\\dummy_5_v2.txt"
expected_file_hash = "b1295d8ebb927df19ad74eec6aea72e3" # Use the actual hash computed from the file

# First add a file
with patch("twinTrim.utils.get_file_hash", return_value=expected_file_hash), \
patch("builtins.open", mock_open(read_data=b"some binary content")):
normalStore.clear()
add_or_update_normal_file(file_path1)

# Then update it
with patch("twinTrim.utils.get_file_hash", return_value=expected_file_hash), \
patch("builtins.open", mock_open(read_data=b"some binary content")):
add_or_update_normal_file(file_path2)

# Check that the file paths were updated correctly
assert expected_file_hash in normalStore.keys(), f"Expected hash '{expected_file_hash}' not found in normalStore"
assert normalStore[expected_file_hash].filepaths == [file_path1, file_path2], "File paths not updated correctly"
Loading