補業績分析引導頁快取測試

This commit is contained in:
OoO
2026-05-13 12:50:34 +08:00
parent dd8ccdf4ad
commit e8497d09ea

View File

@@ -1,4 +1,5 @@
from pathlib import Path
from flask import Flask, session
ROOT = Path(__file__).resolve().parents[1]
@@ -144,6 +145,57 @@ def test_clear_daily_sales_cache_removes_shared_view_cache_files(tmp_path, monke
assert not cache_file.exists()
def test_sales_analysis_preview_context_cache_avoids_reloading_options(tmp_path, monkeypatch):
from routes import sales_routes
app = Flask(__name__)
app.secret_key = "test"
class FakeDatabaseManager:
engine = object()
class FakeInspector:
def has_table(self, table_name):
return table_name == "realtime_sales_monthly"
preview_calls = {"count": 0}
def fake_preview_options(_engine, _table_name):
preview_calls["count"] += 1
return {
"categories": ["美妝"],
"brands": ["品牌"],
"vendors": ["廠商"],
"activities": ["活動"],
"payments": ["付款"],
"months": ["2026-05"],
}
rendered = []
def fake_render_template(template_name, **context):
rendered.append((template_name, context))
return context
monkeypatch.setattr(sales_routes, "_SALES_ANALYSIS_PAGE_CACHE_DIR", tmp_path)
monkeypatch.setattr(sales_routes, "DatabaseManager", FakeDatabaseManager)
monkeypatch.setattr(sales_routes, "inspect", lambda _engine: FakeInspector())
monkeypatch.setattr(sales_routes, "_fetch_sales_data_range", lambda *_args: "2026/05/01 - 2026/05/13")
monkeypatch.setattr(sales_routes, "_preview_sales_filter_options", fake_preview_options)
monkeypatch.setattr(sales_routes, "render_template", fake_render_template)
sales_routes._SALES_ANALYSIS_RESULT_CACHE.clear()
for _ in range(2):
with app.test_request_context("/sales_analysis"):
session["logged_in"] = True
response = sales_routes.sales_analysis()
assert response["no_filter"] is True
assert response["all_categories"] == ["美妝"]
assert preview_calls["count"] == 1
assert len(rendered) == 2
def test_cache_dicts_are_only_defined_in_cache_manager():
assignments = []
for path in [ROOT / "app.py", *ROOT.glob("routes/*.py"), *ROOT.glob("services/*.py")]: