- Introduced `analyst_auth_adapter.py` for managing authentication context and access control for the data analysis agent. - Added new API endpoints in `analyst.py` for chat, dashboard, asset management, and metrics, utilizing the new authentication context. - Created Pydantic models in `analyst_schemas.py` for request and response structures, ensuring consistent data handling. - Updated SQL guard logic in `sql_guard.py` to enforce access restrictions based on user roles and contexts. - Implemented migration scripts for new database tables related to the data analysis agent, enhancing data management capabilities. - Removed legacy authentication code from `auth.py`, streamlining the authentication process. This update significantly enhances the data analysis capabilities, providing a robust framework for querying and managing data securely.
64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
"""analytics_repo 数据访问测试(真实 MySQL)。"""
|
|
import unittest
|
|
|
|
from app.service.analytics_repo import AnalyticsRepo, classify_empty
|
|
|
|
|
|
class TestClassifyEmpty(unittest.TestCase):
|
|
def test_zero(self):
|
|
self.assertEqual(classify_empty([], "SELECT COUNT(*) FROM core_customer"), "zero")
|
|
|
|
def test_not_match(self):
|
|
self.assertEqual(
|
|
classify_empty([], "SELECT * FROM core_holding WHERE customer_id='CUST-X'"), "not_match"
|
|
)
|
|
|
|
def test_no_data(self):
|
|
self.assertEqual(classify_empty([], "SELECT * FROM core_product"), "no_data")
|
|
|
|
def test_has_data(self):
|
|
self.assertEqual(classify_empty([["a"]], "SELECT 1"), "has_data")
|
|
|
|
|
|
class TestRepoMySQL(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.repo = AnalyticsRepo()
|
|
|
|
def test_execute_readonly_aggregate(self):
|
|
res = self.repo.execute_readonly(
|
|
"SELECT risk_code, COUNT(*) AS c FROM core_customer_risk GROUP BY risk_code ORDER BY risk_code"
|
|
)
|
|
self.assertIn("risk_code", res["columns"])
|
|
self.assertGreater(len(res["rows"]), 0)
|
|
|
|
def test_resolve_advisor_scope(self):
|
|
res = self.repo.execute_readonly(
|
|
"SELECT staff_id FROM core_staff WHERE staff_type='advisor' AND is_active=1 LIMIT 1"
|
|
)
|
|
self.assertTrue(res["rows"], "种子中应存在 advisor")
|
|
advisor_id = res["rows"][0][0]
|
|
scope = self.repo.resolve_advisor_scope(advisor_id)
|
|
self.assertGreater(len(scope), 0)
|
|
|
|
def test_data_as_of(self):
|
|
self.assertIsInstance(self.repo.get_data_as_of(), str)
|
|
|
|
def test_log_query_roundtrip(self):
|
|
import uuid
|
|
trace = "test-" + uuid.uuid4().hex[:12]
|
|
self.repo.log_query(
|
|
session_id="sess-test", trace_id=trace, staff_id="STAFF-TEST",
|
|
nl_question="测试", generated_sql="SELECT 1", sql_hash="hash",
|
|
row_count=1, exec_status="success", result_summary={"ok": True},
|
|
exec_latency_ms=5, has_disclaimer=True,
|
|
)
|
|
res = self.repo.execute_readonly(
|
|
f"SELECT trace_id FROM jinrong_agent.analytics_query_log WHERE trace_id='{trace}'"
|
|
)
|
|
self.assertEqual(len(res["rows"]), 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|