test: add CLI integration test for delete command using s101.toml

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-22 12:34:08 +08:00
parent c9542d36a9
commit 8c70e8681b

View File

@@ -6,12 +6,16 @@ from pathlib import Path
import pytest import pytest
from azure.data.tables import TableServiceClient from azure.data.tables import TableServiceClient
from click.testing import CliRunner
from order_bak.backup import backup_orders from order_bak.backup import backup_orders
from order_bak.cli import main
from order_bak.csv_utils import read_csv from order_bak.csv_utils import read_csv
from order_bak.delete import delete_orders from order_bak.delete import delete_orders
from order_bak.scan import scan_orders from order_bak.scan import scan_orders
CONFIG_PATH = Path(__file__).parent.parent / "assets" / "s101.toml"
CONNECTION_STRING = os.environ.get( CONNECTION_STRING = os.environ.get(
"ORDER_BAK_TEST_CONNECTION_STRING", "ORDER_BAK_TEST_CONNECTION_STRING",
( (
@@ -167,3 +171,62 @@ def test_full_pipeline(table_client, tmp_path):
if e.get("OrderTime") and e["OrderTime"].day == 2 and e["state"] == 1 if e.get("OrderTime") and e["OrderTime"].day == 2 and e["state"] == 1
] ]
assert len(remaining_jan2_recived) == 4 assert len(remaining_jan2_recived) == 4
@pytest.mark.integration
def test_delete_command_all_backups(table_client, tmp_path, monkeypatch):
"""CLI delete command reads all backup CSVs and deletes them from Azure."""
# Seed: 3 days × 2 users × 1 Recived order = 6 orders to back up
orders = []
for day in [1, 2, 3]:
for user in ["u1", "u2"]:
orders.append({
"PartitionKey": user,
"RowKey": f"o-d{day}-{user}",
"OrderTime": datetime(2025, 1, day, 10, 0, tzinfo=timezone.utc),
"ReceiveTime": datetime(2025, 1, day, 11, 0, tzinfo=timezone.utc),
"revenue": 10.0,
"platformOrder": "plat",
"prodCount": 1,
"pfId": user,
"prodId": "prod1",
"prodPrice": 10.0,
"state": 1,
"ver": 3,
"customData": None,
})
_seed_orders(table_client, orders)
# Scan then backup (cutoff excludes nothing — all 6 are Recived before Jan 4)
scan_dir = tmp_path / "scan"
scan_path = scan_orders(
connection_string=CONNECTION_STRING,
page_size=10,
delay_ms=0,
scan_dir=scan_dir,
)
backup_dir = tmp_path / "backups"
backed_up = backup_orders(
scan_path=scan_path,
backup_dir=backup_dir,
cutoff=datetime(2025, 1, 4, tzinfo=timezone.utc),
)
assert backed_up == 6
assert len(list(backup_dir.glob("*.csv"))) == 3 # one file per day
# chdir so Path("./backups") in the CLI resolves to tmp_path/backups
monkeypatch.chdir(tmp_path)
result = CliRunner().invoke(
main,
["--config", str(CONFIG_PATH), "delete"],
input="y\n",
)
assert result.exit_code == 0, result.output
assert "6 orders" in result.output
assert "3 file" in result.output
# All 6 Recived orders must be gone from the table
remaining = list(table_client.list_entities())
assert len(remaining) == 0