From 420fa8864dd50295b28d1b996cf70fa2147abc94 Mon Sep 17 00:00:00 2001 From: Sai Ganesh <322103310216@gvpce.ac.in> Date: Mon, 28 Oct 2024 20:39:07 +0530 Subject: [PATCH] Added tests for add_or_update function --- tests/test_allFileMetadata.py | 68 +++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/tests/test_allFileMetadata.py b/tests/test_allFileMetadata.py index 9c0c6f8..3522b7f 100644 --- a/tests/test_allFileMetadata.py +++ b/tests/test_allFileMetadata.py @@ -1,29 +1,51 @@ -import pytest import os -from twinTrim.dataStructures.allFileMetadata import AllFileMetadata +import time import pytest - +from twinTrim.dataStructures.allFileMetadata import add_or_update_file, store +from twinTrim.utils import get_file_hash @pytest.fixture def temp_file(tmp_path): - # Create a temporary file for testing - file = tmp_path / "test_file.txt" - file.write_text("Sample content") - return str(file) - -def test_get_modification_time_existing_file(temp_file): - # Arrange - metadata = AllFileMetadata(temp_file) - # Act - modification_time = metadata.get_modification_time() - # Assert - assert modification_time is not None # Replace with appropriate assertion + """Fixture to create a temporary file for testing.""" + temp_file_path = tmp_path / "test_file.txt" + with open(temp_file_path, "w") as f: + f.write("Sample content") + return str(temp_file_path) + +def test_add_new_file_metadata(temp_file): + # Arrange: Clear the store and add a new file + store.clear() + add_or_update_file(temp_file) + file_hash = get_file_hash(temp_file) + + # Assert: Check if the file was added to the store + assert file_hash in store, "File hash should be in the store after adding" + assert store[file_hash].filepath == temp_file, "Stored file path should match the added file" + +def test_update_existing_file_metadata(temp_file, tmp_path): + # Arrange: Clear the store, add the initial file + store.clear() + add_or_update_file(temp_file) + file_hash = get_file_hash(temp_file) + + # Verify the initial file is added + assert file_hash in store + original_file_path = store[file_hash].filepath + + # Create a new file with the same content but a different modification time + new_file = tmp_path / "new_test_file.txt" + with open(new_file, "w") as f: + f.write("Sample content") -def test_get_modification_time_non_existing_file(): - # Arrange - non_existing_filepath = "path/to/non/existing/file.txt" - metadata = AllFileMetadata(non_existing_filepath) - # Act - modification_time = metadata.get_modification_time() - # Assert - assert modification_time == -1 # Update the assertion + # Wait to ensure modification time is different + time.sleep(1) + os.utime(new_file, (new_file.stat().st_atime, time.time())) + + # Act: Update the store with the new file having the same hash + add_or_update_file(str(new_file)) + + # Assert: Verify the store is updated with the latest file path + assert file_hash in store, "File hash should still be in the store after updating" + assert store[file_hash].filepath == str(new_file), ( + f"Expected latest file path '{new_file}', but got '{store[file_hash].filepath}'" + )