test(quality): fix fake tests and add missing coverage

P0 fixes:
- telemetry: rewrite fake tests with real behavior verification (25 tests)
- config: delete copied-source tests, use proper imports (2 deleted)
- persistence: fix try-except pass to verify specific errors

P1 fixes:
- pipeline: add real FixedWindowAlgo tests instead of mocks (12 tests)
- provider: add SessionManager and ToolManager tests (25 tests)
- storage: add S3StorageProvider tests with moto mock (16 tests)
- plugin: add handler action tests for setting inheritance (15 tests)
- rag: add file storage and ZIP processing tests (21 tests)
- vector: add VDB filter conversion tests (30 tests)

P2 fixes:
- pipeline/msgtrun: strengthen assertions for exact message count
- api: add response structure validation in integration tests

New test files:
- provider/test_session_manager.py
- provider/test_tool_manager.py
- storage/test_s3storage.py
- plugin/test_handler_actions.py
- rag/test_file_storage.py
- vector/test_vdb_filter_conversion.py

Source code bugs documented:
- provider: TokenManager.next_token() ZeroDivisionError
- telemetry: send_tasks class variable shared state
- command: empty command IndexError, unused parameters
- utils: funcschema KeyError
- entity: vector.py independent declarative_base

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
huanghuoguoguo
2026-05-11 10:20:34 +08:00
parent adb4b29c94
commit 1a3c73bc05
17 changed files with 3123 additions and 510 deletions
+24 -4
View File
@@ -77,7 +77,7 @@ def fake_provider_app():
app.provider_service.get_provider = AsyncMock(return_value={
'uuid': 'test-provider-uuid', 'name': 'OpenAI', 'requester': 'chatcmpl'
})
app.provider_service.create_provider = AsyncMock(return_value={'uuid': 'new-provider-uuid'})
app.provider_service.create_provider = AsyncMock(return_value='new-provider-uuid')
app.provider_service.update_provider = AsyncMock(return_value={})
app.provider_service.delete_provider = AsyncMock()
app.provider_service.get_provider_model_counts = AsyncMock(return_value={
@@ -132,7 +132,7 @@ class TestProviderEndpoints:
@pytest.mark.asyncio
async def test_get_providers_success(self, quart_test_client):
"""GET /api/v1/provider/providers returns provider list."""
"""GET /api/v1/provider/providers returns provider list with complete structure."""
response = await quart_test_client.get(
'/api/v1/provider/providers',
headers={'Authorization': 'Bearer test_token'}
@@ -142,10 +142,21 @@ class TestProviderEndpoints:
data = await response.get_json()
assert data['code'] == 0
assert 'data' in data
# Verify response structure completeness
providers = data['data']['providers']
assert isinstance(providers, list)
assert len(providers) == 1
# Verify required fields in provider object
provider = providers[0]
assert 'uuid' in provider
assert 'name' in provider
assert 'requester' in provider
assert provider['uuid'] == 'test-provider-uuid'
assert provider['name'] == 'OpenAI'
@pytest.mark.asyncio
async def test_get_single_provider_success(self, quart_test_client):
"""GET /api/v1/provider/providers/{uuid} returns provider."""
"""GET /api/v1/provider/providers/{uuid} returns complete provider structure."""
response = await quart_test_client.get(
'/api/v1/provider/providers/test-provider-uuid',
headers={'Authorization': 'Bearer test_token'}
@@ -154,10 +165,16 @@ class TestProviderEndpoints:
assert response.status_code == 200
data = await response.get_json()
assert data['code'] == 0
# Verify response structure
provider = data['data']['provider']
assert 'uuid' in provider
assert 'name' in provider
assert 'requester' in provider
assert provider['uuid'] == 'test-provider-uuid'
@pytest.mark.asyncio
async def test_create_provider_success(self, quart_test_client):
"""POST /api/v1/provider/providers creates new provider."""
"""POST /api/v1/provider/providers creates new provider with uuid returned."""
response = await quart_test_client.post(
'/api/v1/provider/providers',
headers={'Authorization': 'Bearer test_token'},
@@ -167,7 +184,10 @@ class TestProviderEndpoints:
assert response.status_code == 200
data = await response.get_json()
assert data['code'] == 0
# Verify uuid is present and matches expected
assert 'data' in data
assert 'uuid' in data['data']
assert data['data']['uuid'] == 'new-provider-uuid'
@pytest.mark.asyncio
async def test_update_provider_success(self, quart_test_client):
@@ -167,31 +167,59 @@ class TestSQLiteMigrationFreshDatabase:
await fresh_engine.dispose()
@pytest.mark.asyncio
async def test_fresh_db_without_create_all_fails_gracefully(self, tmp_path):
async def test_fresh_db_without_create_all_behavior(self, tmp_path):
"""
Fresh database without create_all may fail or have empty tables.
Fresh database without create_all - test actual behavior.
This tests the edge case where migrations run on truly empty DB.
The behavior depends on migration script implementation.
This tests what happens when migrations run on truly empty DB.
The behavior is determined by Alembic and migration scripts.
EXPECTED: Either:
1. Migration succeeds (if scripts handle empty DB)
2. Migration fails with specific error (if scripts require tables)
IMPORTANT: This test verifies the ACTUAL behavior, not accepting
any arbitrary failure with try-except pass.
"""
fresh_db_file = tmp_path / "test_empty_migrations.db"
fresh_url = f"sqlite+aiosqlite:///{fresh_db_file}"
fresh_engine = create_async_engine(fresh_url)
# Don't create tables - try upgrade directly
# This may fail if migrations expect tables to exist
# Capture the actual behavior
actual_result = None
actual_error = None
try:
await run_alembic_upgrade(fresh_engine, 'head')
rev = await get_alembic_current(fresh_engine)
# If it succeeds, verify revision
assert rev is not None
except Exception:
# If it fails, that's acceptable behavior
# Migrations may require create_all first
pass
actual_result = rev
except Exception as e:
actual_error = e
await fresh_engine.dispose()
# Verify specific behavior - one of two outcomes is expected
if actual_result is not None:
# Migration succeeded - verify revision exists
assert actual_result is not None, "Revision should exist after successful migration"
else:
# Migration failed - verify the error type is known
# Alembic typically raises specific errors for missing tables
assert actual_error is not None, "Error should be captured if migration failed"
# Log the error type for documentation (don't silently pass)
error_type = type(actual_error).__name__
# Acceptable error types for empty DB scenarios
acceptable_errors = [
'OperationalError', # SQLite table not found
'ProgrammingError', # SQLAlchemy errors
'CommandError', # Alembic command errors
]
assert error_type in acceptable_errors, (
f"Unexpected error type: {error_type}. "
f"This may indicate a regression in migration behavior. "
f"Error: {actual_error}"
)
class TestSQLiteMigrationGetCurrent:
"""Tests for get_alembic_current behavior."""