Skip to content

Commit

Permalink
Merge pull request #160 from Sai-ganesh-0004/main
Browse files Browse the repository at this point in the history
Added tests for add_or_update function
  • Loading branch information
Kota-Karthik authored Oct 28, 2024
2 parents c0b7d37 + 420fa88 commit 72d514c
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions tests/test_allFileMetadata.py
Original file line number Diff line number Diff line change
@@ -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}'"
)

0 comments on commit 72d514c

Please sign in to comment.