chore: fix unit test (#2421)

This commit is contained in:
Aries-ckt
2025-03-09 20:35:38 +08:00
committed by GitHub
parent 81f4c6a558
commit fdadfdd393
37 changed files with 221 additions and 296 deletions

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -1,4 +1,5 @@
from typing import Dict
from unittest.mock import MagicMock
import pytest
import pytest_asyncio
@@ -8,6 +9,7 @@ from httpx import ASGITransport, AsyncClient
from dbgpt.component import SystemApp
from dbgpt.util import AppConfig
from dbgpt.util.fastapi import create_app
from dbgpt_serve.core import BaseServeConfig
def create_system_app(param: Dict) -> SystemApp:
@@ -41,8 +43,18 @@ def system_app(request):
return create_system_app(param)
@pytest.fixture
def config():
mock_config = MagicMock(spec=BaseServeConfig)
mock_config.api_keys = "mock_api_key_123"
mock_config.load_dbgpts_interval = 0
mock_config.default_user = "dbgpt"
mock_config.default_sys_code = "dbgpt"
return mock_config
@pytest_asyncio.fixture
async def client(request, asystem_app: SystemApp):
async def client(request, asystem_app: SystemApp, config: BaseServeConfig):
param = getattr(request, "param", {})
headers = param.get("headers", {})
base_url = param.get("base_url", "http://test")
@@ -62,5 +74,5 @@ async def client(request, asystem_app: SystemApp):
for router in routers:
test_app.include_router(router)
if app_caller:
app_caller(test_app, asystem_app)
app_caller(test_app, asystem_app, config)
yield client

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance
@@ -28,7 +30,14 @@ def service(system_app: SystemApp):
@pytest.fixture
def default_entity_dict():
# TODO: build your default entity dict
return {}
return {
"host": "",
"port": 0,
"model": "",
"provider": "",
"worker_type": "",
"params": "{}",
}
@pytest.mark.parametrize(

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -7,14 +7,15 @@ from dbgpt_serve.core.tests.conftest import ( # noqa: F401
system_app,
)
from ..api.schemas import ServeRequest, ServerResponse
from ..config import ServeConfig
from ..models.models import ServeDao, ServeEntity
from ..models.models import ServeDao, ServeEntity, ServeRequest, ServerResponse
@pytest.fixture(autouse=True)
def setup_and_teardown():
db.init_db("sqlite:///:memory:")
db.init_db(
"sqlite:///:memory:", engine_args={"connect_args": {"check_same_thread": False}}
)
db.create_all()
yield
@@ -34,7 +35,14 @@ def dao(server_config):
@pytest.fixture
def default_entity_dict():
# TODO: build your default entity dict
return {}
return {
"host": "127.0.0.1",
"port": 8080,
"model": "test",
"provider": "test",
"worker_type": "test",
"params": "{}",
}
def test_table_exist():
@@ -43,7 +51,14 @@ def test_table_exist():
def test_entity_create(default_entity_dict):
with db.session() as session:
entity = ServeEntity(**default_entity_dict)
entity = ServeEntity(
host=default_entity_dict["host"],
port=default_entity_dict["port"],
model=default_entity_dict["model"],
provider=default_entity_dict["provider"],
worker_type=default_entity_dict["worker_type"],
params=default_entity_dict["params"],
)
session.add(entity)
@@ -74,15 +89,14 @@ def test_entity_all():
def test_dao_create(dao, default_entity_dict):
# TODO: implement your test case
req = ServeRequest(**default_entity_dict)
res: ServerResponse = dao.create(req)
res: ServerResponse = dao.create(default_entity_dict)
assert res is not None
def test_dao_get_one(dao, default_entity_dict):
# TODO: implement your test case
req = ServeRequest(**default_entity_dict)
res: ServerResponse = dao.create(req)
req = ServeRequest()
res: ServerResponse = dao.create(default_entity_dict)
def test_get_dao_get_list(dao):

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -5,7 +5,8 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt.util import PaginationResult
from dbgpt_serve.core.tests.conftest import asystem_app, client # noqa: F401
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import asystem_app, client, config # noqa: F401
from ..api.endpoints import init_endpoints, router
from ..api.schemas import ServerResponse
@@ -19,9 +20,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
async def _create_and_validate(

View File

@@ -4,7 +4,8 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core.tests.conftest import system_app # noqa: F401
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import config, system_app # noqa: F401
from ..api.schemas import ServeRequest, ServerResponse
from ..models.models import ServeEntity
@@ -19,8 +20,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance

View File

@@ -7,6 +7,7 @@ from dbgpt.component import SystemApp
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -42,9 +43,10 @@ def mock_chunk_dao():
@pytest.fixture
def service(system_app: SystemApp, mock_dao, mock_document_dao, mock_chunk_dao):
def service(system_app: SystemApp, mock_dao, mock_document_dao, mock_chunk_dao, config):
return Service(
system_app=system_app,
config=config,
dao=mock_dao,
document_dao=mock_document_dao,
chunk_dao=mock_chunk_dao,

View File

@@ -4,9 +4,11 @@ from httpx import AsyncClient
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -22,9 +24,9 @@ def setup_and_teardown():
yield
def client_init_caller(app: FastAPI, system_app: SystemApp):
def client_init_caller(app: FastAPI, system_app: SystemApp, config: BaseServeConfig):
app.include_router(router)
init_endpoints(system_app)
init_endpoints(system_app, config)
@pytest.mark.asyncio

View File

@@ -2,9 +2,11 @@ import pytest
from dbgpt.component import SystemApp
from dbgpt.storage.metadata import db
from dbgpt_serve.core import BaseServeConfig
from dbgpt_serve.core.tests.conftest import ( # noqa: F401
asystem_app,
client,
config,
system_app,
)
@@ -19,8 +21,8 @@ def setup_and_teardown():
@pytest.fixture
def service(system_app: SystemApp):
instance = Service(system_app)
def service(system_app: SystemApp, config: BaseServeConfig):
instance = Service(system_app, config)
instance.init_app(system_app)
return instance