152 lines
5.0 KiB
Python
152 lines
5.0 KiB
Python
"""Tests for FileEntry dataclass."""
|
|
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
from src.core.file_entry import FileEntry
|
|
|
|
|
|
class TestFileEntry:
|
|
"""Tests for FileEntry dataclass."""
|
|
|
|
def test_create_file_entry(self) -> None:
|
|
"""Test creating a FileEntry instance."""
|
|
now = datetime.now()
|
|
entry = FileEntry(
|
|
path="documents/test.txt",
|
|
hash="sha256:abc123",
|
|
size=1024,
|
|
created=now,
|
|
modified=now,
|
|
)
|
|
|
|
assert entry.path == "documents/test.txt"
|
|
assert entry.hash == "sha256:abc123"
|
|
assert entry.size == 1024
|
|
assert entry.created == now
|
|
assert entry.modified == now
|
|
|
|
def test_file_entry_is_immutable(self) -> None:
|
|
"""Test that FileEntry is frozen (immutable)."""
|
|
now = datetime.now()
|
|
entry = FileEntry(
|
|
path="test.txt",
|
|
hash="sha256:abc",
|
|
size=100,
|
|
created=now,
|
|
modified=now,
|
|
)
|
|
|
|
with pytest.raises(AttributeError):
|
|
entry.path = "other.txt" # type: ignore
|
|
|
|
def test_to_dict(self) -> None:
|
|
"""Test serialization to dictionary."""
|
|
created = datetime(2026, 1, 28, 10, 30, 0)
|
|
modified = datetime(2026, 1, 28, 14, 20, 0)
|
|
entry = FileEntry(
|
|
path="documents/file.txt",
|
|
hash="sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
size=1234,
|
|
created=created,
|
|
modified=modified,
|
|
)
|
|
|
|
result = entry.to_dict()
|
|
|
|
assert result == {
|
|
"path": "documents/file.txt",
|
|
"hash": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
"size": 1234,
|
|
"created": "2026-01-28T10:30:00",
|
|
"modified": "2026-01-28T14:20:00",
|
|
}
|
|
|
|
def test_from_dict(self) -> None:
|
|
"""Test deserialization from dictionary."""
|
|
data = {
|
|
"path": "documents/file.txt",
|
|
"hash": "sha256:abc123",
|
|
"size": 1234,
|
|
"created": "2026-01-28T10:30:00",
|
|
"modified": "2026-01-28T14:20:00",
|
|
}
|
|
|
|
entry = FileEntry.from_dict(data)
|
|
|
|
assert entry.path == "documents/file.txt"
|
|
assert entry.hash == "sha256:abc123"
|
|
assert entry.size == 1234
|
|
assert entry.created == datetime(2026, 1, 28, 10, 30, 0)
|
|
assert entry.modified == datetime(2026, 1, 28, 14, 20, 0)
|
|
|
|
def test_from_path(self) -> None:
|
|
"""Test creating FileEntry from actual file."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
base_path = Path(tmpdir)
|
|
file_path = base_path / "test.txt"
|
|
file_path.write_text("Hello, World!")
|
|
|
|
entry = FileEntry.from_path(base_path, file_path)
|
|
|
|
assert entry.path == "test.txt"
|
|
assert entry.hash.startswith("sha256:")
|
|
assert entry.size == 13 # len("Hello, World!")
|
|
assert entry.created is not None
|
|
assert entry.modified is not None
|
|
|
|
def test_from_path_nested_directory(self) -> None:
|
|
"""Test creating FileEntry from file in nested directory."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
base_path = Path(tmpdir)
|
|
nested_dir = base_path / "documents" / "work"
|
|
nested_dir.mkdir(parents=True)
|
|
file_path = nested_dir / "report.txt"
|
|
file_path.write_text("Test content")
|
|
|
|
entry = FileEntry.from_path(base_path, file_path)
|
|
|
|
assert entry.path == "documents/work/report.txt"
|
|
|
|
def test_has_changed_same_hash(self) -> None:
|
|
"""Test has_changed returns False for same hash."""
|
|
now = datetime.now()
|
|
entry1 = FileEntry(
|
|
path="test.txt", hash="sha256:abc", size=100, created=now, modified=now
|
|
)
|
|
entry2 = FileEntry(
|
|
path="test.txt", hash="sha256:abc", size=100, created=now, modified=now
|
|
)
|
|
|
|
assert not entry1.has_changed(entry2)
|
|
|
|
def test_has_changed_different_hash(self) -> None:
|
|
"""Test has_changed returns True for different hash."""
|
|
now = datetime.now()
|
|
entry1 = FileEntry(
|
|
path="test.txt", hash="sha256:abc", size=100, created=now, modified=now
|
|
)
|
|
entry2 = FileEntry(
|
|
path="test.txt", hash="sha256:xyz", size=100, created=now, modified=now
|
|
)
|
|
|
|
assert entry1.has_changed(entry2)
|
|
|
|
def test_is_newer_than(self) -> None:
|
|
"""Test is_newer_than comparison."""
|
|
old_time = datetime(2026, 1, 1, 10, 0, 0)
|
|
new_time = datetime(2026, 1, 2, 10, 0, 0)
|
|
|
|
old_entry = FileEntry(
|
|
path="test.txt", hash="sha256:abc", size=100, created=old_time, modified=old_time
|
|
)
|
|
new_entry = FileEntry(
|
|
path="test.txt", hash="sha256:xyz", size=100, created=new_time, modified=new_time
|
|
)
|
|
|
|
assert new_entry.is_newer_than(old_entry)
|
|
assert not old_entry.is_newer_than(new_entry)
|