diff --git a/tests/allFileMetadata_compare_and_replace.py b/tests/allFileMetadata_compare_and_replace.py index dcba128..e5871ae 100644 --- a/tests/allFileMetadata_compare_and_replace.py +++ b/tests/allFileMetadata_compare_and_replace.py @@ -1,7 +1,6 @@ import os import pytest import tempfile -import time from twinTrim.dataStructures.allFileMetadata import AllFileMetadata from unittest.mock import patch @@ -21,28 +20,33 @@ def setup_files(): # Return the paths of the temporary files yield temp_file1.name, temp_file2.name - # Cleanup - os.remove(temp_file1.name) - os.remove(temp_file2.name) + # Cleanup - only remove if the file still exists + if os.path.exists(temp_file1.name): + os.remove(temp_file1.name) + if os.path.exists(temp_file2.name): + os.remove(temp_file2.name) def test_compare_and_replace_removes_correct_file(setup_files): file1, file2 = setup_files - # Modify the modification time of the second file to be newer - time.sleep(1) # Ensure file2 is modified after file1 - os.utime(file2, None) + # Use patch to mock modification times and handle_and_remove function + with patch('os.path.getmtime') as mock_getmtime, \ + patch('twinTrim.dataStructures.allFileMetadata.handle_and_remove') as mock_remove: + + # Set mock modification times: file1 older than file2 + mock_getmtime.side_effect = lambda path: 1000 if path == file1 else 2000 - metadata1 = AllFileMetadata(file1) - metadata2 = AllFileMetadata(file2) + metadata1 = AllFileMetadata(file1) + metadata2 = AllFileMetadata(file2) - with patch('twinTrim.dataStructures.allFileMetadata.handle_and_remove') as mock_remove: + # Execute compare_and_replace metadata1.compare_and_replace(metadata2) - # Check that file1 is removed + # Verify that file1 was "removed" since it is older mock_remove.assert_called_once_with(file1) def test_compare_and_replace_no_file_removed_when_file_missing(setup_files): - file1, file2 = setup_files + file1, _ = setup_files # Create a metadata object for file1 metadata1 = AllFileMetadata(file1) @@ -51,7 +55,7 @@ def test_compare_and_replace_no_file_removed_when_file_missing(setup_files): nonexistent_file = 'nonexistent_file.txt' metadata2 = AllFileMetadata(nonexistent_file) - with patch('twinTrim.dataStructures.allFileMetadata.handle_and_remove') as mock_remove: + with patch('twinTrim.utils.handle_and_remove') as mock_remove: metadata1.compare_and_replace(metadata2) # Check that no file is removed @@ -63,18 +67,18 @@ def test_compare_and_replace_no_file_removed_when_both_files_missing(setup_files # Create a metadata object for file1 metadata1 = AllFileMetadata(file1) - # Create a metadata object for two non-existing files + # Create metadata objects for two non-existing files nonexistent_file1 = 'nonexistent_file1.txt' nonexistent_file2 = 'nonexistent_file2.txt' metadata2 = AllFileMetadata(nonexistent_file1) metadata3 = AllFileMetadata(nonexistent_file2) - with patch('twinTrim.dataStructures.allFileMetadata.handle_and_remove') as mock_remove: + with patch('twinTrim.utils.handle_and_remove') as mock_remove: metadata1.compare_and_replace(metadata2) # No file should be removed since the second file doesn't exist mock_remove.assert_not_called() - with patch('twinTrim.dataStructures.allFileMetadata.handle_and_remove') as mock_remove: + with patch('twinTrim.utils.handle_and_remove') as mock_remove: metadata1.compare_and_replace(metadata3) # No file should be removed since the second file doesn't exist mock_remove.assert_not_called() \ No newline at end of file diff --git a/tests/test_fileMetadata.py b/tests/test_fileMetadata.py index dae0a56..d870148 100644 --- a/tests/test_fileMetadata.py +++ b/tests/test_fileMetadata.py @@ -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 @@ -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" \ No newline at end of file diff --git a/tests/test_flagController.py b/tests/test_flagController.py index 11a844e..2c18ebf 100644 --- a/tests/test_flagController.py +++ b/tests/test_flagController.py @@ -1,3 +1,4 @@ +import os import pytest from unittest.mock import patch, MagicMock from twinTrim.flagController import handleAllFlag @@ -29,7 +30,7 @@ def test_handle_all_flag_filtered_files(mock_progress_bar, mock_add_or_update_fi """Test handleAllFlag with files that pass the filter.""" # Simulate a directory with 3 files mock_os_walk.return_value = [ - ('/path/to/files', [], ['file1.txt', 'file2.txt', 'file3.txt']) + (os.path.normpath('/path/to/files'), [], ['file1.txt', 'file2.txt', 'file3.txt']) ] # Mock file_filter to filter out some files @@ -40,10 +41,13 @@ def test_handle_all_flag_filtered_files(mock_progress_bar, mock_add_or_update_fi mock_progress_bar.return_value.__enter__.return_value = MagicMock() # Call the function - handleAllFlag('/path/to/files', mock_file_filter, 'yellow', 'white') - + handleAllFlag(os.path.normpath('/path/to/files'), mock_file_filter, 'yellow', 'white') + + # Normalizing the expected file path to handle platform-specific slashes (Windows != \\) + expected_path = os.path.normpath('/path/to/files/file2.txt') + # Assertions - mock_add_or_update_file.assert_called_once_with('/path/to/files/file2.txt') # Only file2.txt should be processed + mock_add_or_update_file.assert_called_once_with(expected_path) # Only file2.txt should be processed mock_progress_bar.assert_called_once() # Progress bar should still be created @@ -54,7 +58,7 @@ def test_handle_all_flag_success(mock_progress_bar, mock_add_or_update_file, moc """Test handleAllFlag successful execution.""" # Simulate a directory with 3 files mock_os_walk.return_value = [ - ('/path/to/files', [], ['file1.txt', 'file2.txt', 'file3.txt']) + (os.path.normpath('/path/to/files'), [], ['file1.txt', 'file2.txt', 'file3.txt']) ] # Mock file_filter to allow all files @@ -65,7 +69,7 @@ def test_handle_all_flag_success(mock_progress_bar, mock_add_or_update_file, moc mock_progress_bar.return_value.__enter__.return_value = MagicMock() # Call the function - handleAllFlag('/path/to/files', mock_file_filter, 'yellow', 'white') + handleAllFlag(os.path.normpath('/path/to/files'), mock_file_filter, 'yellow', 'white') # Assertions assert mock_add_or_update_file.call_count == 3 # All 3 files should be processed