Skip to content

Commit

Permalink
Merge pull request #156 from ShirshenduR/mock
Browse files Browse the repository at this point in the history
Fixes #11 Mock file operations Tests
  • Loading branch information
Kota-Karthik authored Oct 28, 2024
2 parents 3d7a42e + bbebd4a commit 177cb0d
Showing 1 changed file with 42 additions and 1 deletion.
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"

0 comments on commit 177cb0d

Please sign in to comment.