Skip to content

Update test_allFileMetadata.py #167

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

Closed
wants to merge 1 commit into from
Closed
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
75 changes: 33 additions & 42 deletions tests/test_allFileMetadata.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
import os
import time
import pytest
from twinTrim.dataStructures.allFileMetadata import add_or_update_file, store
from twinTrim.utils import get_file_hash
from twinTrim.dataStructures.allFileMetadata import AllFileMetadata
from pathlib import Path

@pytest.fixture
def temp_file(tmp_path):
"""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
"""Creates a temporary file for testing and returns its Path object."""
file = tmp_path / "test_file.txt"
file.write_text("Sample content")
return file # Return Path object instead of str

def test_get_modification_time_existing_file(temp_file):
"""
Test that the modification time is correctly retrieved for an existing file.
"""
# Arrange
metadata = AllFileMetadata(temp_file)

# Act
modification_time = metadata.get_modification_time()

# Assert
assert modification_time > 0, (
"Expected a positive modification time for an existing file, but got None or 0"
)

# 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")

# Wait to ensure modification time is different
time.sleep(1)
os.utime(new_file, (new_file.stat().st_atime, time.time()))
def test_get_modification_time_non_existing_file():
"""
Test that the modification time retrieval returns -1 or appropriate error for non-existing file.
"""
# Arrange
non_existing_filepath = Path("non/existing/path/file.txt")
metadata = AllFileMetadata(non_existing_filepath)

# Act: Update the store with the new file having the same hash
add_or_update_file(str(new_file))
# Act
modification_time = metadata.get_modification_time()

# 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}'"
# Assert
assert modification_time == -1, (
"Expected -1 for modification time of non-existing file, but got a different value"
)
Loading