Files
order-bak/tests/test_delete.py
2026-04-21 16:02:06 +08:00

68 lines
1.7 KiB
Python

from datetime import datetime, timezone
from unittest.mock import MagicMock
from order_bak.csv_utils import write_csv
from order_bak.delete import delete_orders
def _make_entity(pk, rk):
return {
"PartitionKey": pk,
"RowKey": rk,
"OrderTime": datetime(2025, 1, 1, tzinfo=timezone.utc),
"ReceiveTime": None,
"revenue": 10.0,
"platformOrder": "p",
"prodCount": 1,
"pfId": pk,
"prodId": "prod",
"prodPrice": 10.0,
"state": 1,
"ver": 3,
"customData": None,
}
def test_delete_groups_by_partition_and_batches(tmp_path):
entities = []
for i in range(3):
entities.append(_make_entity("user1", f"order{i}"))
for i in range(3, 5):
entities.append(_make_entity("user2", f"order{i}"))
csv_path = tmp_path / "2025-01-01.csv"
write_csv(csv_path, entities)
mock_table = MagicMock()
deleted_count = delete_orders(
table_client=mock_table,
csv_path=csv_path,
batch_size=2,
delay_ms=0,
)
assert deleted_count == 5
# user1: 3 entities, batch_size=2 -> 2 batches (2 + 1)
# user2: 2 entities, batch_size=2 -> 1 batch
assert mock_table.submit_transaction.call_count == 3
def test_delete_respects_batch_size(tmp_path):
entities = [_make_entity("user1", f"order{i}") for i in range(5)]
csv_path = tmp_path / "2025-01-01.csv"
write_csv(csv_path, entities)
mock_table = MagicMock()
deleted_count = delete_orders(
table_client=mock_table,
csv_path=csv_path,
batch_size=2,
delay_ms=0,
)
assert deleted_count == 5
assert mock_table.submit_transaction.call_count == 3 # 2+2+1