mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-09-08 10:14:35 +08:00
tests: improve RAGFlow coverage based on Codecov report (#13200)
### What problem does this PR solve? Codecov’s coverage report shows that several RAGFlow code paths are currently untested or under-tested. This makes it easier for regressions to slip in during refactors and feature work. This PR adds targeted automated tests to cover the files and branches highlighted by Codecov, improving confidence in core behavior while keeping runtime functionality unchanged. ### Type of change - [x] Other (please describe): Test coverage improvement (adds/extends unit and integration tests to address Codecov-reported gaps)
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
import pytest
|
||||
import requests
|
||||
from common import (
|
||||
create_agent,
|
||||
create_agent_session,
|
||||
@@ -22,6 +23,7 @@ from common import (
|
||||
list_agent_sessions,
|
||||
list_agents,
|
||||
)
|
||||
from configs import HOST_ADDRESS, VERSION
|
||||
|
||||
AGENT_TITLE = "test_agent_http"
|
||||
MINIMAL_DSL = {
|
||||
@@ -87,3 +89,33 @@ class TestAgentSessions:
|
||||
|
||||
res = delete_agent_sessions(HttpApiAuth, agent_id, {"ids": [session_id]})
|
||||
assert res["code"] == 0, res
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_agent_crud_validation_contract(self, HttpApiAuth, agent_id):
|
||||
res = list_agents(HttpApiAuth, {"id": "missing-agent-id", "title": "missing-agent-title"})
|
||||
assert res["code"] == 102, res
|
||||
assert "doesn't exist" in res["message"], res
|
||||
|
||||
res = list_agents(HttpApiAuth, {"title": AGENT_TITLE, "desc": "true", "page_size": 1})
|
||||
assert res["code"] == 0, res
|
||||
|
||||
res = create_agent(HttpApiAuth, {"title": "missing-dsl-agent"})
|
||||
assert res["code"] == 101, res
|
||||
assert "No DSL data in request" in res["message"], res
|
||||
|
||||
res = create_agent(HttpApiAuth, {"dsl": MINIMAL_DSL})
|
||||
assert res["code"] == 101, res
|
||||
assert "No title in request" in res["message"], res
|
||||
|
||||
res = create_agent(HttpApiAuth, {"title": AGENT_TITLE, "dsl": MINIMAL_DSL})
|
||||
assert res["code"] == 102, res
|
||||
assert "already exists" in res["message"], res
|
||||
|
||||
update_url = f"{HOST_ADDRESS}/api/{VERSION}/agents/invalid-agent-id"
|
||||
res = requests.put(update_url, auth=HttpApiAuth, json={"title": "updated", "dsl": MINIMAL_DSL}).json()
|
||||
assert res["code"] == 103, res
|
||||
assert "Only owner of canvas authorized" in res["message"], res
|
||||
|
||||
res = delete_agent(HttpApiAuth, "invalid-agent-id")
|
||||
assert res["code"] == 103, res
|
||||
assert "Only owner of canvas authorized" in res["message"], res
|
||||
|
||||
@@ -130,3 +130,80 @@ class TestChatCompletionsOpenAI:
|
||||
)
|
||||
# Should return an error (format may vary based on implementation)
|
||||
assert "error" in res or res.get("code") != 0, f"Should return error for invalid chat: {res}"
|
||||
|
||||
@pytest.mark.p2
|
||||
@pytest.mark.parametrize(
|
||||
"payload, requires_valid_chat, expected_message",
|
||||
[
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"extra_body": "invalid_extra_body",
|
||||
},
|
||||
False,
|
||||
"extra_body must be an object.",
|
||||
),
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"extra_body": {"reference_metadata": "invalid_reference_metadata"},
|
||||
},
|
||||
False,
|
||||
"reference_metadata must be an object.",
|
||||
),
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"extra_body": {"reference_metadata": {"fields": "author"}},
|
||||
},
|
||||
False,
|
||||
"reference_metadata.fields must be an array.",
|
||||
),
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [],
|
||||
},
|
||||
False,
|
||||
"You have to provide messages.",
|
||||
),
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [{"role": "assistant", "content": "hello"}],
|
||||
},
|
||||
False,
|
||||
"The last content of this conversation is not from user.",
|
||||
),
|
||||
(
|
||||
{
|
||||
"model": "model",
|
||||
"messages": [{"role": "user", "content": "hello"}],
|
||||
"extra_body": {"metadata_condition": "invalid"},
|
||||
},
|
||||
True,
|
||||
"metadata_condition must be an object.",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_openai_chat_completion_request_validation(
|
||||
self,
|
||||
HttpApiAuth,
|
||||
request,
|
||||
payload,
|
||||
requires_valid_chat,
|
||||
expected_message,
|
||||
):
|
||||
chat_id = "invalid_chat_id"
|
||||
if requires_valid_chat:
|
||||
res = create_chat_assistant(HttpApiAuth, {"name": "openai_validation_case", "dataset_ids": []})
|
||||
assert res["code"] == 0, res
|
||||
chat_id = res["data"]["id"]
|
||||
request.addfinalizer(lambda: delete_chat_assistants(HttpApiAuth))
|
||||
|
||||
res = chat_completions_openai(HttpApiAuth, chat_id, payload)
|
||||
assert res.get("code") != 0, res
|
||||
assert expected_message in res.get("message", ""), res
|
||||
|
||||
+1127
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user