mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-19 22:21:04 +08:00
Refactor: reformat all code for lefthook using ruff and gofmt (#16585)
This commit is contained in:
@@ -53,7 +53,7 @@ def test_validate_immutable_fields_no_changes():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -66,7 +66,7 @@ def test_validate_immutable_fields_chunk_count_matches():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -79,7 +79,7 @@ def test_validate_immutable_fields_token_count_matches():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -92,7 +92,7 @@ def test_validate_immutable_fields_progress_matches():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -105,7 +105,7 @@ def test_validate_immutable_fields_chunk_count_mismatch():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg == "Can't change `chunk_count`."
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -118,7 +118,7 @@ def test_validate_immutable_fields_token_count_mismatch():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg == "Can't change `token_count`."
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -131,7 +131,7 @@ def test_validate_immutable_fields_progress_mismatch():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg == "Can't change `progress`."
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -145,18 +145,18 @@ def test_validate_immutable_fields_progress_boundary_values():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.0
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
|
||||
|
||||
# Test with 1.0
|
||||
update_doc_req = UpdateDocumentReq(progress=1.0)
|
||||
doc = Mock()
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 1.0
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -169,7 +169,7 @@ def test_validate_immutable_fields_none_values():
|
||||
doc.chunk_num = 10
|
||||
doc.token_num = 100
|
||||
doc.progress = 0.5
|
||||
|
||||
|
||||
error_msg, error_code = validate_immutable_fields(update_doc_req, doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -240,6 +240,7 @@ def test_validate_document_name_valid():
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
|
||||
|
||||
def test_validate_document_name_attr_error():
|
||||
"""Test valid document name update."""
|
||||
req_doc_name = 0
|
||||
@@ -312,7 +313,7 @@ def test_validate_chunk_method_valid():
|
||||
doc = Mock()
|
||||
doc.type = FileType.PDF
|
||||
doc.name = "document.pdf"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
@@ -323,7 +324,7 @@ def test_validate_chunk_method_visual_not_supported():
|
||||
doc = Mock()
|
||||
doc.type = FileType.VISUAL
|
||||
doc.name = "image.jpg"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert "Not supported yet!" in error_msg
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -334,7 +335,7 @@ def test_validate_chunk_method_ppt_not_supported():
|
||||
doc = Mock()
|
||||
doc.type = FileType.PDF
|
||||
doc.name = "presentation.ppt"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert "Not supported yet!" in error_msg
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -345,7 +346,7 @@ def test_validate_chunk_method_pptx_not_supported():
|
||||
doc = Mock()
|
||||
doc.type = FileType.PDF
|
||||
doc.name = "presentation.pptx"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert "Not supported yet!" in error_msg
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -356,7 +357,7 @@ def test_validate_chunk_method_pages_not_supported():
|
||||
doc = Mock()
|
||||
doc.type = FileType.PDF
|
||||
doc.name = "document.pages"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert "Not supported yet!" in error_msg
|
||||
assert error_code == RetCode.DATA_ERROR
|
||||
@@ -367,7 +368,7 @@ def test_validate_chunk_method_other_extensions_still_valid():
|
||||
doc = Mock()
|
||||
doc.type = FileType.PDF
|
||||
doc.name = "document.docx"
|
||||
|
||||
|
||||
error_msg, error_code = validate_chunk_method(doc)
|
||||
assert error_msg is None
|
||||
assert error_code is None
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
Unit tests for MinIO health check (check_minio_alive) and scheme/verify helpers.
|
||||
Covers SSL/HTTPS and certificate verification (issues #13158, #13159).
|
||||
"""
|
||||
|
||||
from unittest.mock import patch, Mock
|
||||
|
||||
|
||||
@@ -27,6 +28,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_scheme_http_when_secure_false(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000", "secure": False}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
scheme, verify = _minio_scheme_and_verify()
|
||||
assert scheme == "http"
|
||||
assert verify is True
|
||||
@@ -35,6 +37,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_scheme_https_when_secure_true(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000", "secure": True}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
scheme, verify = _minio_scheme_and_verify()
|
||||
assert scheme == "https"
|
||||
assert verify is True
|
||||
@@ -43,6 +46,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_scheme_https_when_secure_string_true(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000", "secure": "true"}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
scheme, verify = _minio_scheme_and_verify()
|
||||
assert scheme == "https"
|
||||
|
||||
@@ -50,6 +54,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_verify_false_for_self_signed(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000", "secure": True, "verify": False}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
scheme, verify = _minio_scheme_and_verify()
|
||||
assert scheme == "https"
|
||||
assert verify is False
|
||||
@@ -58,6 +63,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_verify_string_false(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000", "verify": "false"}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
_, verify = _minio_scheme_and_verify()
|
||||
assert verify is False
|
||||
|
||||
@@ -65,6 +71,7 @@ class TestMinioSchemeAndVerify:
|
||||
def test_default_verify_true_when_key_missing(self, mock_settings):
|
||||
mock_settings.MINIO = {"host": "minio:9000"}
|
||||
from api.utils.health_utils import _minio_scheme_and_verify
|
||||
|
||||
_, verify = _minio_scheme_and_verify()
|
||||
assert verify is True
|
||||
|
||||
@@ -80,6 +87,7 @@ class TestCheckMinioAlive:
|
||||
mock_response.status_code = 200
|
||||
mock_get.return_value = mock_response
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
result = check_minio_alive()
|
||||
assert result["status"] == "alive"
|
||||
assert "elapsed" in result["message"]
|
||||
@@ -96,6 +104,7 @@ class TestCheckMinioAlive:
|
||||
mock_response.status_code = 200
|
||||
mock_get.return_value = mock_response
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
check_minio_alive()
|
||||
call_args = mock_get.call_args
|
||||
assert call_args[0][0] == "https://minio:9000/minio/health/live"
|
||||
@@ -108,6 +117,7 @@ class TestCheckMinioAlive:
|
||||
mock_response.status_code = 200
|
||||
mock_get.return_value = mock_response
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
check_minio_alive()
|
||||
call_args = mock_get.call_args
|
||||
assert call_args[1]["verify"] is False
|
||||
@@ -120,6 +130,7 @@ class TestCheckMinioAlive:
|
||||
mock_response.status_code = 503
|
||||
mock_get.return_value = mock_response
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
result = check_minio_alive()
|
||||
assert result["status"] == "timeout"
|
||||
|
||||
@@ -129,6 +140,7 @@ class TestCheckMinioAlive:
|
||||
mock_settings.MINIO = {"host": "minio:9000"}
|
||||
mock_get.side_effect = ConnectionError("Connection refused")
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
result = check_minio_alive()
|
||||
assert result["status"] == "timeout"
|
||||
assert "error" in result["message"]
|
||||
@@ -141,6 +153,7 @@ class TestCheckMinioAlive:
|
||||
mock_response.status_code = 200
|
||||
mock_get.return_value = mock_response
|
||||
from api.utils.health_utils import check_minio_alive
|
||||
|
||||
check_minio_alive()
|
||||
call_args = mock_get.call_args
|
||||
assert call_args[1]["timeout"] == 10
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
"""
|
||||
Unit tests for OceanBase health check and performance monitoring functionality.
|
||||
"""
|
||||
|
||||
import inspect
|
||||
import os
|
||||
import types
|
||||
@@ -27,20 +28,15 @@ from api.utils.health_utils import get_oceanbase_status, check_oceanbase_health
|
||||
|
||||
class TestOceanBaseHealthCheck:
|
||||
"""Test cases for OceanBase health check functionality."""
|
||||
|
||||
@patch('api.utils.health_utils.OBConnection')
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'oceanbase'})
|
||||
|
||||
@patch("api.utils.health_utils.OBConnection")
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "oceanbase"})
|
||||
def test_get_oceanbase_status_success(self, mock_ob_class):
|
||||
"""Test successful OceanBase status retrieval."""
|
||||
# Setup mock
|
||||
mock_ob_connection = Mock()
|
||||
mock_ob_connection.uri = "localhost:2881"
|
||||
mock_ob_connection.health.return_value = {
|
||||
"uri": "localhost:2881",
|
||||
"version_comment": "OceanBase 4.3.5.1",
|
||||
"status": "healthy",
|
||||
"connection": "connected"
|
||||
}
|
||||
mock_ob_connection.health.return_value = {"uri": "localhost:2881", "version_comment": "OceanBase 4.3.5.1", "status": "healthy", "connection": "connected"}
|
||||
mock_ob_connection.get_performance_metrics.return_value = {
|
||||
"connection": "connected",
|
||||
"latency_ms": 5.2,
|
||||
@@ -49,13 +45,13 @@ class TestOceanBaseHealthCheck:
|
||||
"query_per_second": 150,
|
||||
"slow_queries": 2,
|
||||
"active_connections": 10,
|
||||
"max_connections": 300
|
||||
"max_connections": 300,
|
||||
}
|
||||
mock_ob_class.return_value = mock_ob_connection
|
||||
|
||||
|
||||
# Execute
|
||||
result = get_oceanbase_status()
|
||||
|
||||
|
||||
# Assert
|
||||
assert result["status"] == "alive"
|
||||
assert "message" in result
|
||||
@@ -63,36 +59,31 @@ class TestOceanBaseHealthCheck:
|
||||
assert "performance" in result["message"]
|
||||
assert result["message"]["health"]["status"] == "healthy"
|
||||
assert result["message"]["performance"]["latency_ms"] == 5.2
|
||||
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'elasticsearch'})
|
||||
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "elasticsearch"})
|
||||
def test_get_oceanbase_status_not_configured(self):
|
||||
"""Test OceanBase status when not configured."""
|
||||
with pytest.raises(Exception) as exc_info:
|
||||
get_oceanbase_status()
|
||||
assert "OceanBase is not in use" in str(exc_info.value)
|
||||
|
||||
@patch('api.utils.health_utils.OBConnection')
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'oceanbase'})
|
||||
|
||||
@patch("api.utils.health_utils.OBConnection")
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "oceanbase"})
|
||||
def test_get_oceanbase_status_connection_error(self, mock_ob_class):
|
||||
"""Test OceanBase status when connection fails."""
|
||||
mock_ob_class.side_effect = Exception("Connection failed")
|
||||
|
||||
|
||||
result = get_oceanbase_status()
|
||||
|
||||
|
||||
assert result["status"] == "timeout"
|
||||
assert "error" in result["message"]
|
||||
|
||||
@patch('api.utils.health_utils.OBConnection')
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'oceanbase'})
|
||||
|
||||
@patch("api.utils.health_utils.OBConnection")
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "oceanbase"})
|
||||
def test_check_oceanbase_health_healthy(self, mock_ob_class):
|
||||
"""Test OceanBase health check returns healthy status."""
|
||||
mock_ob_connection = Mock()
|
||||
mock_ob_connection.health.return_value = {
|
||||
"uri": "localhost:2881",
|
||||
"version_comment": "OceanBase 4.3.5.1",
|
||||
"status": "healthy",
|
||||
"connection": "connected"
|
||||
}
|
||||
mock_ob_connection.health.return_value = {"uri": "localhost:2881", "version_comment": "OceanBase 4.3.5.1", "status": "healthy", "connection": "connected"}
|
||||
mock_ob_connection.get_performance_metrics.return_value = {
|
||||
"connection": "connected",
|
||||
"latency_ms": 5.2,
|
||||
@@ -101,28 +92,23 @@ class TestOceanBaseHealthCheck:
|
||||
"query_per_second": 150,
|
||||
"slow_queries": 0,
|
||||
"active_connections": 10,
|
||||
"max_connections": 300
|
||||
"max_connections": 300,
|
||||
}
|
||||
mock_ob_class.return_value = mock_ob_connection
|
||||
|
||||
|
||||
result = check_oceanbase_health()
|
||||
|
||||
|
||||
assert result["status"] == "healthy"
|
||||
assert result["details"]["connection"] == "connected"
|
||||
assert result["details"]["latency_ms"] == 5.2
|
||||
assert result["details"]["query_per_second"] == 150
|
||||
|
||||
@patch('api.utils.health_utils.OBConnection')
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'oceanbase'})
|
||||
|
||||
@patch("api.utils.health_utils.OBConnection")
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "oceanbase"})
|
||||
def test_check_oceanbase_health_degraded(self, mock_ob_class):
|
||||
"""Test OceanBase health check returns degraded status for high latency."""
|
||||
mock_ob_connection = Mock()
|
||||
mock_ob_connection.health.return_value = {
|
||||
"uri": "localhost:2881",
|
||||
"version_comment": "OceanBase 4.3.5.1",
|
||||
"status": "healthy",
|
||||
"connection": "connected"
|
||||
}
|
||||
mock_ob_connection.health.return_value = {"uri": "localhost:2881", "version_comment": "OceanBase 4.3.5.1", "status": "healthy", "connection": "connected"}
|
||||
mock_ob_connection.get_performance_metrics.return_value = {
|
||||
"connection": "connected",
|
||||
"latency_ms": 1500.0, # High latency > 1000ms
|
||||
@@ -131,43 +117,35 @@ class TestOceanBaseHealthCheck:
|
||||
"query_per_second": 50,
|
||||
"slow_queries": 5,
|
||||
"active_connections": 10,
|
||||
"max_connections": 300
|
||||
"max_connections": 300,
|
||||
}
|
||||
mock_ob_class.return_value = mock_ob_connection
|
||||
|
||||
|
||||
result = check_oceanbase_health()
|
||||
|
||||
|
||||
assert result["status"] == "degraded"
|
||||
assert result["details"]["latency_ms"] == 1500.0
|
||||
|
||||
@patch('api.utils.health_utils.OBConnection')
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'oceanbase'})
|
||||
|
||||
@patch("api.utils.health_utils.OBConnection")
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "oceanbase"})
|
||||
def test_check_oceanbase_health_unhealthy(self, mock_ob_class):
|
||||
"""Test OceanBase health check returns unhealthy status."""
|
||||
mock_ob_connection = Mock()
|
||||
mock_ob_connection.health.return_value = {
|
||||
"uri": "localhost:2881",
|
||||
"status": "unhealthy",
|
||||
"connection": "disconnected",
|
||||
"error": "Connection timeout"
|
||||
}
|
||||
mock_ob_connection.get_performance_metrics.return_value = {
|
||||
"connection": "disconnected",
|
||||
"error": "Connection timeout"
|
||||
}
|
||||
mock_ob_connection.health.return_value = {"uri": "localhost:2881", "status": "unhealthy", "connection": "disconnected", "error": "Connection timeout"}
|
||||
mock_ob_connection.get_performance_metrics.return_value = {"connection": "disconnected", "error": "Connection timeout"}
|
||||
mock_ob_class.return_value = mock_ob_connection
|
||||
|
||||
|
||||
result = check_oceanbase_health()
|
||||
|
||||
|
||||
assert result["status"] == "unhealthy"
|
||||
assert result["details"]["connection"] == "disconnected"
|
||||
assert "error" in result["details"]
|
||||
|
||||
@patch.dict(os.environ, {'DOC_ENGINE': 'elasticsearch'})
|
||||
|
||||
@patch.dict(os.environ, {"DOC_ENGINE": "elasticsearch"})
|
||||
def test_check_oceanbase_health_not_configured(self):
|
||||
"""Test OceanBase health check when not configured."""
|
||||
result = check_oceanbase_health()
|
||||
|
||||
|
||||
assert result["status"] == "not_configured"
|
||||
assert result["details"]["connection"] == "not_configured"
|
||||
assert "not configured" in result["details"]["message"].lower()
|
||||
@@ -175,29 +153,32 @@ class TestOceanBaseHealthCheck:
|
||||
|
||||
class TestOBConnectionPerformanceMetrics:
|
||||
"""Test cases for OBConnection performance metrics methods."""
|
||||
|
||||
|
||||
def _create_mock_connection(self):
|
||||
"""Create a mock OBConnection with actual methods."""
|
||||
|
||||
# Create a simple object and bind the real methods to it
|
||||
class MockConn:
|
||||
pass
|
||||
|
||||
conn = MockConn()
|
||||
# Get the actual class from the singleton wrapper's closure
|
||||
from rag.utils import ob_conn
|
||||
|
||||
# OBConnection is wrapped by @singleton decorator, so it's a function
|
||||
# The original class is stored in the closure of the singleton function
|
||||
# Find the class by checking all closure cells
|
||||
ob_connection_class = None
|
||||
if hasattr(ob_conn.OBConnection, '__closure__') and ob_conn.OBConnection.__closure__:
|
||||
if hasattr(ob_conn.OBConnection, "__closure__") and ob_conn.OBConnection.__closure__:
|
||||
for cell in ob_conn.OBConnection.__closure__:
|
||||
cell_value = cell.cell_contents
|
||||
if inspect.isclass(cell_value):
|
||||
ob_connection_class = cell_value
|
||||
break
|
||||
|
||||
|
||||
if ob_connection_class is None:
|
||||
raise ValueError("Could not find OBConnection class in closure")
|
||||
|
||||
|
||||
# Bind the actual methods to our mock object
|
||||
conn.get_performance_metrics = types.MethodType(ob_connection_class.get_performance_metrics, conn)
|
||||
conn._get_storage_info = types.MethodType(ob_connection_class._get_storage_info, conn)
|
||||
@@ -205,7 +186,7 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn._get_slow_query_count = types.MethodType(ob_connection_class._get_slow_query_count, conn)
|
||||
conn._estimate_qps = types.MethodType(ob_connection_class._estimate_qps, conn)
|
||||
return conn
|
||||
|
||||
|
||||
def test_get_performance_metrics_success(self):
|
||||
"""Test successful retrieval of performance metrics."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -214,29 +195,27 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn.client = mock_client
|
||||
conn.uri = "localhost:2881"
|
||||
conn.db_name = "test"
|
||||
|
||||
|
||||
# Mock client methods - create separate mock results for each call
|
||||
mock_result1 = Mock()
|
||||
mock_result1.fetchone.return_value = (1,)
|
||||
|
||||
|
||||
mock_result2 = Mock()
|
||||
mock_result2.fetchone.return_value = (100.5,)
|
||||
|
||||
|
||||
mock_result3 = Mock()
|
||||
mock_result3.fetchone.return_value = (100.0,)
|
||||
|
||||
|
||||
mock_result4 = Mock()
|
||||
mock_result4.fetchall.return_value = [
|
||||
(1, 'user', 'host', 'db', 'Query', 0, 'executing', 'SELECT 1')
|
||||
]
|
||||
mock_result4.fetchone.return_value = ('max_connections', '300')
|
||||
|
||||
mock_result4.fetchall.return_value = [(1, "user", "host", "db", "Query", 0, "executing", "SELECT 1")]
|
||||
mock_result4.fetchone.return_value = ("max_connections", "300")
|
||||
|
||||
mock_result5 = Mock()
|
||||
mock_result5.fetchone.return_value = (0,)
|
||||
|
||||
|
||||
mock_result6 = Mock()
|
||||
mock_result6.fetchone.return_value = (5,)
|
||||
|
||||
|
||||
# Setup side_effect to return different mocks for different queries
|
||||
def sql_side_effect(query):
|
||||
if "SELECT 1" in query:
|
||||
@@ -254,21 +233,22 @@ class TestOBConnectionPerformanceMetrics:
|
||||
elif "information_schema.processlist" in query and "COUNT" in query:
|
||||
return mock_result6
|
||||
return Mock()
|
||||
|
||||
|
||||
mock_client.perform_raw_text_sql.side_effect = sql_side_effect
|
||||
mock_client.pool_size = 300
|
||||
|
||||
|
||||
# Mock logger
|
||||
import logging
|
||||
conn.logger = logging.getLogger('test')
|
||||
|
||||
|
||||
conn.logger = logging.getLogger("test")
|
||||
|
||||
result = conn.get_performance_metrics()
|
||||
|
||||
|
||||
assert result["connection"] == "connected"
|
||||
assert result["latency_ms"] >= 0
|
||||
assert "storage_used" in result
|
||||
assert "storage_total" in result
|
||||
|
||||
|
||||
def test_get_performance_metrics_connection_error(self):
|
||||
"""Test performance metrics when connection fails."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -277,14 +257,14 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn.client = mock_client
|
||||
conn.uri = "localhost:2881"
|
||||
conn.logger = Mock()
|
||||
|
||||
|
||||
mock_client.perform_raw_text_sql.side_effect = Exception("Connection failed")
|
||||
|
||||
|
||||
result = conn.get_performance_metrics()
|
||||
|
||||
|
||||
assert result["connection"] == "disconnected"
|
||||
assert "error" in result
|
||||
|
||||
|
||||
def test_get_storage_info_success(self):
|
||||
"""Test successful retrieval of storage information."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -293,27 +273,27 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn.client = mock_client
|
||||
conn.db_name = "test"
|
||||
conn.logger = Mock()
|
||||
|
||||
|
||||
mock_result1 = Mock()
|
||||
mock_result1.fetchone.return_value = (100.5,)
|
||||
mock_result2 = Mock()
|
||||
mock_result2.fetchone.return_value = (100.0,)
|
||||
|
||||
|
||||
def sql_side_effect(query):
|
||||
if "information_schema.tables" in query:
|
||||
return mock_result1
|
||||
elif "__all_disk_stat" in query:
|
||||
return mock_result2
|
||||
return Mock()
|
||||
|
||||
|
||||
mock_client.perform_raw_text_sql.side_effect = sql_side_effect
|
||||
|
||||
|
||||
result = conn._get_storage_info()
|
||||
|
||||
|
||||
assert "storage_used" in result
|
||||
assert "storage_total" in result
|
||||
assert "MB" in result["storage_used"]
|
||||
|
||||
|
||||
def test_get_storage_info_fallback(self):
|
||||
"""Test storage info with fallback when total space unavailable."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -322,7 +302,7 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn.client = mock_client
|
||||
conn.db_name = "test"
|
||||
conn.logger = Mock()
|
||||
|
||||
|
||||
# First query succeeds, second fails
|
||||
def side_effect(query):
|
||||
if "information_schema.tables" in query:
|
||||
@@ -331,14 +311,14 @@ class TestOBConnectionPerformanceMetrics:
|
||||
return mock_result
|
||||
else:
|
||||
raise Exception("Table not found")
|
||||
|
||||
|
||||
mock_client.perform_raw_text_sql.side_effect = side_effect
|
||||
|
||||
|
||||
result = conn._get_storage_info()
|
||||
|
||||
|
||||
assert "storage_used" in result
|
||||
assert "storage_total" in result
|
||||
|
||||
|
||||
def test_get_connection_pool_stats(self):
|
||||
"""Test retrieval of connection pool statistics."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -347,31 +327,28 @@ class TestOBConnectionPerformanceMetrics:
|
||||
conn.client = mock_client
|
||||
conn.logger = Mock()
|
||||
mock_client.pool_size = 300
|
||||
|
||||
|
||||
mock_result1 = Mock()
|
||||
mock_result1.fetchall.return_value = [
|
||||
(1, 'user', 'host', 'db', 'Query', 0, 'executing', 'SELECT 1'),
|
||||
(2, 'user', 'host', 'db', 'Sleep', 10, None, None)
|
||||
]
|
||||
|
||||
mock_result1.fetchall.return_value = [(1, "user", "host", "db", "Query", 0, "executing", "SELECT 1"), (2, "user", "host", "db", "Sleep", 10, None, None)]
|
||||
|
||||
mock_result2 = Mock()
|
||||
mock_result2.fetchone.return_value = ('max_connections', '300')
|
||||
|
||||
mock_result2.fetchone.return_value = ("max_connections", "300")
|
||||
|
||||
def sql_side_effect(query):
|
||||
if "SHOW PROCESSLIST" in query:
|
||||
return mock_result1
|
||||
elif "SHOW VARIABLES LIKE 'max_connections'" in query:
|
||||
return mock_result2
|
||||
return Mock()
|
||||
|
||||
|
||||
mock_client.perform_raw_text_sql.side_effect = sql_side_effect
|
||||
|
||||
|
||||
result = conn._get_connection_pool_stats()
|
||||
|
||||
|
||||
assert "active_connections" in result
|
||||
assert "max_connections" in result
|
||||
assert result["active_connections"] >= 0
|
||||
|
||||
|
||||
def test_get_slow_query_count(self):
|
||||
"""Test retrieval of slow query count."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -379,16 +356,16 @@ class TestOBConnectionPerformanceMetrics:
|
||||
mock_client = Mock()
|
||||
conn.client = mock_client
|
||||
conn.logger = Mock()
|
||||
|
||||
|
||||
mock_result = Mock()
|
||||
mock_result.fetchone.return_value = (5,)
|
||||
mock_client.perform_raw_text_sql.return_value = mock_result
|
||||
|
||||
|
||||
result = conn._get_slow_query_count(threshold_seconds=1)
|
||||
|
||||
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
|
||||
def test_estimate_qps(self):
|
||||
"""Test QPS estimation."""
|
||||
# Create mock connection with actual methods
|
||||
@@ -396,17 +373,16 @@ class TestOBConnectionPerformanceMetrics:
|
||||
mock_client = Mock()
|
||||
conn.client = mock_client
|
||||
conn.logger = Mock()
|
||||
|
||||
|
||||
mock_result = Mock()
|
||||
mock_result.fetchone.return_value = (10,)
|
||||
mock_client.perform_raw_text_sql.return_value = mock_result
|
||||
|
||||
|
||||
result = conn._estimate_qps()
|
||||
|
||||
|
||||
assert isinstance(result, int)
|
||||
assert result >= 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v"])
|
||||
|
||||
|
||||
Reference in New Issue
Block a user