refactor: change backup_orders to use cutoff param instead of start/end

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-04-21 23:02:18 +08:00
parent fac909ca8b
commit e0eff89e8c
2 changed files with 30 additions and 5 deletions

View File

@@ -9,8 +9,7 @@ from order_bak.csv_utils import CSV_COLUMNS, entity_to_row, parse_row
def backup_orders( def backup_orders(
scan_path: Path, scan_path: Path,
backup_dir: Path, backup_dir: Path,
start: datetime, cutoff: datetime,
end: datetime,
) -> int: ) -> int:
backup_dir.mkdir(parents=True, exist_ok=True) backup_dir.mkdir(parents=True, exist_ok=True)
@@ -25,7 +24,7 @@ def backup_orders(
order_time = entity["OrderTime"] order_time = entity["OrderTime"]
if order_time is None: if order_time is None:
continue continue
if order_time < start or order_time >= end: if order_time >= cutoff:
continue continue
date_key = order_time.strftime("%Y-%m-%d") date_key = order_time.strftime("%Y-%m-%d")
by_date[date_key].append(entity) by_date[date_key].append(entity)

View File

@@ -41,8 +41,7 @@ def test_backup_filters_by_state_and_date(tmp_path):
result = backup_orders( result = backup_orders(
scan_path=scan_csv, scan_path=scan_csv,
backup_dir=backup_dir, backup_dir=backup_dir,
start=datetime(2025, 1, 1, tzinfo=timezone.utc), cutoff=datetime(2025, 1, 3, tzinfo=timezone.utc),
end=datetime(2025, 1, 3, tzinfo=timezone.utc),
) )
assert result == 2 assert result == 2
@@ -56,3 +55,30 @@ def test_backup_filters_by_state_and_date(tmp_path):
jan1_rows = list(read_csv(jan1)) jan1_rows = list(read_csv(jan1))
assert len(jan1_rows) == 1 assert len(jan1_rows) == 1
assert jan1_rows[0]["RowKey"] == "o1" assert jan1_rows[0]["RowKey"] == "o1"
def test_backup_filters_by_cutoff(tmp_path):
scan_dir = tmp_path / "scan"
backup_dir = tmp_path / "backups"
scan_dir.mkdir()
backup_dir.mkdir()
entities = [
_make_entity("u1", "o1", datetime(2025, 1, 1, 10, tzinfo=timezone.utc), 1),
_make_entity("u2", "o2", datetime(2025, 1, 2, 8, tzinfo=timezone.utc), 1),
_make_entity("u3", "o3", datetime(2025, 1, 3, 8, tzinfo=timezone.utc), 1), # at/after cutoff
]
scan_csv = scan_dir / "scan-2025-01-03.csv"
write_csv(scan_csv, entities)
cutoff = datetime(2025, 1, 3, tzinfo=timezone.utc)
result = backup_orders(
scan_path=scan_csv,
backup_dir=backup_dir,
cutoff=cutoff,
)
assert result == 2
assert (backup_dir / "2025-01-01.csv").exists()
assert (backup_dir / "2025-01-02.csv").exists()
assert not (backup_dir / "2025-01-03.csv").exists()