From e0eff89e8c082e72dcddbb54f8a7efd65a5fa665 Mon Sep 17 00:00:00 2001 From: tech Date: Tue, 21 Apr 2026 23:02:18 +0800 Subject: [PATCH] refactor: change backup_orders to use cutoff param instead of start/end Co-Authored-By: Claude Opus 4.7 --- src/order_bak/backup.py | 5 ++--- tests/test_backup.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/order_bak/backup.py b/src/order_bak/backup.py index 63ac722..c2e46cf 100644 --- a/src/order_bak/backup.py +++ b/src/order_bak/backup.py @@ -9,8 +9,7 @@ from order_bak.csv_utils import CSV_COLUMNS, entity_to_row, parse_row def backup_orders( scan_path: Path, backup_dir: Path, - start: datetime, - end: datetime, + cutoff: datetime, ) -> int: backup_dir.mkdir(parents=True, exist_ok=True) @@ -25,7 +24,7 @@ def backup_orders( order_time = entity["OrderTime"] if order_time is None: continue - if order_time < start or order_time >= end: + if order_time >= cutoff: continue date_key = order_time.strftime("%Y-%m-%d") by_date[date_key].append(entity) diff --git a/tests/test_backup.py b/tests/test_backup.py index 0ddcdf9..dadedd4 100644 --- a/tests/test_backup.py +++ b/tests/test_backup.py @@ -41,8 +41,7 @@ def test_backup_filters_by_state_and_date(tmp_path): result = backup_orders( scan_path=scan_csv, backup_dir=backup_dir, - start=datetime(2025, 1, 1, tzinfo=timezone.utc), - end=datetime(2025, 1, 3, tzinfo=timezone.utc), + cutoff=datetime(2025, 1, 3, tzinfo=timezone.utc), ) assert result == 2 @@ -56,3 +55,30 @@ def test_backup_filters_by_state_and_date(tmp_path): jan1_rows = list(read_csv(jan1)) assert len(jan1_rows) == 1 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()