96 lines
2.2 KiB
Python
96 lines
2.2 KiB
Python
import os
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from main import find_latest_file_in_folder, should_copy_file, compute_file_checksum, verify_copy
|
|
|
|
|
|
|
|
def _touch(path: Path, mtime: float) -> None:
|
|
path.write_text('data', encoding='utf-8')
|
|
os.utime(path, (mtime, mtime))
|
|
|
|
|
|
|
|
def test_find_latest_file_returns_none_for_missing_folder(tmp_path):
|
|
missing = tmp_path / 'missing'
|
|
assert find_latest_file_in_folder(str(missing)) is None
|
|
|
|
|
|
|
|
def test_find_latest_file_picks_newest(tmp_path):
|
|
folder = tmp_path / 'src'
|
|
folder.mkdir()
|
|
|
|
now = time.time()
|
|
older = folder / 'older.bak'
|
|
newer = folder / 'newer.bak'
|
|
other = folder / 'other.sql'
|
|
|
|
_touch(older, now - 10)
|
|
_touch(newer, now - 5)
|
|
_touch(other, now - 1)
|
|
|
|
latest = find_latest_file_in_folder(str(folder))
|
|
assert latest is not None
|
|
assert latest.name == 'other.sql'
|
|
|
|
|
|
|
|
def test_should_copy_file_when_target_missing(tmp_path):
|
|
src = tmp_path / 'src.bak'
|
|
src.write_text('x', encoding='utf-8')
|
|
dst = tmp_path / 'dst.bak'
|
|
|
|
assert should_copy_file(src, dst) is True
|
|
|
|
|
|
|
|
def test_should_copy_file_when_source_newer(tmp_path):
|
|
now = time.time()
|
|
src = tmp_path / 'src.bak'
|
|
dst = tmp_path / 'dst.bak'
|
|
|
|
_touch(dst, now - 10)
|
|
_touch(src, now)
|
|
|
|
assert should_copy_file(src, dst) is True
|
|
|
|
|
|
|
|
def test_should_copy_file_when_target_newer_or_equal(tmp_path):
|
|
now = time.time()
|
|
src = tmp_path / 'src.bak'
|
|
dst = tmp_path / 'dst.bak'
|
|
|
|
_touch(src, now)
|
|
_touch(dst, now)
|
|
|
|
assert should_copy_file(src, dst) is False
|
|
|
|
|
|
def test_compute_file_checksum_stable(tmp_path):
|
|
src = tmp_path / 'a.bak'
|
|
src.write_text('hello', encoding='utf-8')
|
|
c1 = compute_file_checksum(src)
|
|
c2 = compute_file_checksum(src)
|
|
assert c1 == c2
|
|
|
|
|
|
def test_verify_copy_true_for_equal_files(tmp_path):
|
|
src = tmp_path / 'src.bak'
|
|
dst = tmp_path / 'dst.bak'
|
|
src.write_text('data', encoding='utf-8')
|
|
dst.write_text('data', encoding='utf-8')
|
|
assert verify_copy(src, dst) is True
|
|
|
|
|
|
def test_verify_copy_false_for_different_files(tmp_path):
|
|
src = tmp_path / 'src.bak'
|
|
dst = tmp_path / 'dst.bak'
|
|
src.write_text('data1', encoding='utf-8')
|
|
dst.write_text('data2', encoding='utf-8')
|
|
assert verify_copy(src, dst) is False
|