feat(pchome): automate authorized sales acquisition
Some checks failed
CD Pipeline / deploy (push) Has been cancelled

This commit is contained in:
ogt
2026-07-11 01:30:42 +08:00
parent f68223f0ed
commit 6e65ef00bb
21 changed files with 2428 additions and 617 deletions

View File

@@ -94,12 +94,14 @@ def test_daily_sales_import_fails_when_monthly_sync_fails(monkeypatch, tmp_path)
job = session.query(import_service.ImportJob).filter_by(id=job_id).one()
assert job.status == "failed"
assert job.progress_percent == 95
assert job.current_step == "業績分析儀表板同步失敗"
assert job.current_step == "交易驗證失敗,已回滾"
assert "monthly sync boom" in job.error_message
summary = json.loads(job.import_summary)
assert summary["sync_success"] is False
assert summary["synced_to"] is None
assert "monthly sync boom" in summary["sync_error"]
assert summary["atomic"] is True
assert summary["rolled_back"] is True
finally:
session.close()
@@ -107,11 +109,45 @@ def test_daily_sales_import_fails_when_monthly_sync_fails(monkeypatch, tmp_path)
snapshot_rows = conn.execute(text("SELECT COUNT(*) FROM daily_sales_snapshot")).scalar()
monthly_rows = conn.execute(text("SELECT COUNT(*) FROM realtime_sales_monthly")).scalar()
assert snapshot_rows == 1
assert snapshot_rows == 0
assert monthly_rows == 0
assert FakeNotificationManager.sent_messages
def test_daily_sales_import_preserves_today_fallback_when_date_column_is_missing(monkeypatch, tmp_path):
import_service = _load_import_service(monkeypatch, f"sqlite:///{tmp_path / 'momo.db'}")
_prepare_daily_sales_tables(import_service)
source_df = pd.DataFrame([{
"商品ID": "A001",
"商品名稱": "測試商品",
"銷售金額": 1200,
}])
monkeypatch.setattr(
import_service,
"_read_daily_sales_excel",
lambda _path: (
source_df.copy(),
{"date_col": None, "sheet_name": "即時業績明細", "header_row": 1},
),
)
service = import_service.ImportService()
job_id = service.create_import_job("daily_sales", "drive-file-1", "daily.xlsx", 1024)
assert service.process_daily_sales_import(job_id, str(tmp_path / "daily.xlsx")) is True
expected_date = import_service.datetime.now(import_service.TAIPEI_TZ).date().isoformat()
with import_service.engine.connect() as conn:
snapshot = conn.execute(text(
'SELECT "日期", snapshot_date FROM daily_sales_snapshot'
)).one()
monthly = conn.execute(text(
'SELECT "日期" FROM realtime_sales_monthly'
)).one()
assert snapshot[0] == expected_date
assert str(snapshot[1])[:10] == expected_date
assert monthly[0] == expected_date
def test_auto_import_does_not_move_drive_file_when_import_fails(monkeypatch, tmp_path):
import_service = _load_import_service(monkeypatch, f"sqlite:///{tmp_path / 'momo.db'}")
import_service.Base.metadata.create_all(import_service.engine)
@@ -145,6 +181,47 @@ def test_auto_import_does_not_move_drive_file_when_import_fails(monkeypatch, tmp
assert fake_drive.moved_files == []
def test_auto_import_reports_partial_when_drive_archive_fails(monkeypatch, tmp_path):
import_service = _load_import_service(monkeypatch, f"sqlite:///{tmp_path / 'momo.db'}")
import_service.Base.metadata.create_all(import_service.engine)
monkeypatch.chdir(tmp_path)
class FakeDriveService:
last_error_kind = None
last_error = None
def list_files_in_folder(self, folder_path, file_pattern):
return [{"id": "drive-file-1", "name": "daily.xlsx", "size": 1024}]
def download_file(self, file_id, local_path):
os.makedirs(os.path.dirname(local_path), exist_ok=True)
with open(local_path, "wb") as handle:
handle.write(b"test")
return True
def move_file(self, file_id, folder, create_missing=False):
return False
monkeypatch.setattr(import_service, "drive_service", FakeDriveService())
service = import_service.ImportService()
monkeypatch.setattr(service, "process_daily_sales_import", lambda job_id, path: True)
result = service.auto_import_from_drive()
assert result["success"] is False
assert result["status"] == "partial_source_finalize_failed"
assert result["imported_count"] == 1
assert result["source_finalize_ok"] is False
assert result["archive_failed_count"] == 1
session = import_service.Session()
try:
job = session.query(import_service.ImportJob).one()
assert job.status == "completed"
assert job.current_step == "匯入完成,來源封存待自動重試"
finally:
session.close()
def test_auto_import_fails_closed_when_drive_auth_fails(monkeypatch, tmp_path):
import_service = _load_import_service(monkeypatch, f"sqlite:///{tmp_path / 'momo.db'}")
import_service.Base.metadata.create_all(import_service.engine)