mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-14 18:18:08 +00:00
More tests
This commit is contained in:
1 parent
d5c0322e2a
commit
a3b08a5b44
37 files changed
+21904
-6
No files matched your search
@@ -330,3 +330,180 @@ def test_execute_agent_node_raises_when_schema_set_and_response_not_json(monkeyp
|
||||
match="Structured output was expected but response was not valid JSON",
|
||||
):
|
||||
list(engine._execute_agent_node(node))
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Coverage — additional uncovered lines: 204, 213-215, 223, 283-284, 289,
|
||||
# 355, 375
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
class TestWorkflowEngineAdditionalCoverage:
|
||||
|
||||
def test_agent_node_prompt_template_empty_uses_query(self, monkeypatch):
|
||||
"""Cover line 204: prompt_template is empty, uses state query."""
|
||||
engine = create_engine()
|
||||
engine.state["query"] = "What is the answer?"
|
||||
node = create_agent_node(node_id="n1")
|
||||
node.config["prompt_template"] = ""
|
||||
|
||||
node_events = [{"answer": "42"}]
|
||||
monkeypatch.setattr(
|
||||
WorkflowNodeAgentFactory,
|
||||
"create",
|
||||
staticmethod(lambda **kwargs: StubNodeAgent(node_events)),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_api_key_for_provider",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_provider_from_model_id",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_model_capabilities",
|
||||
lambda _: None,
|
||||
)
|
||||
|
||||
list(engine._execute_agent_node(node))
|
||||
assert engine.state["node_n1_output"] == "42"
|
||||
|
||||
def test_agent_node_model_config_override(self, monkeypatch):
|
||||
"""Cover lines 213-215: node_config with model_id and llm_name."""
|
||||
engine = create_engine()
|
||||
engine.state["query"] = "test"
|
||||
node = create_agent_node(node_id="n2")
|
||||
node.config["model_id"] = "gpt-4o"
|
||||
node.config["llm_name"] = "openai"
|
||||
|
||||
node_events = [{"answer": "result"}]
|
||||
monkeypatch.setattr(
|
||||
WorkflowNodeAgentFactory,
|
||||
"create",
|
||||
staticmethod(lambda **kwargs: StubNodeAgent(node_events)),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_api_key_for_provider",
|
||||
lambda _: "key",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_provider_from_model_id",
|
||||
lambda _: "openai",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_model_capabilities",
|
||||
lambda _: None,
|
||||
)
|
||||
|
||||
list(engine._execute_agent_node(node))
|
||||
assert engine.state["node_n2_output"] == "result"
|
||||
|
||||
def test_agent_node_unsupported_structured_output_raises(self, monkeypatch):
|
||||
"""Cover line 223: model does not support structured output raises."""
|
||||
engine = create_engine()
|
||||
engine.state["query"] = "test"
|
||||
node = create_agent_node(
|
||||
node_id="n3",
|
||||
json_schema={"type": "object", "properties": {"a": {"type": "string"}}},
|
||||
)
|
||||
node.config["model_id"] = "model-no-struct"
|
||||
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_api_key_for_provider",
|
||||
lambda _: "key",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_provider_from_model_id",
|
||||
lambda _: "openai",
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_model_capabilities",
|
||||
lambda _: {"supports_structured_output": False},
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="does not support structured output"):
|
||||
list(engine._execute_agent_node(node))
|
||||
|
||||
def test_structured_output_with_structured_response(self, monkeypatch):
|
||||
"""Cover lines 283-284: structured response parsed and validated."""
|
||||
engine = create_engine()
|
||||
engine.state["query"] = "test"
|
||||
node = create_agent_node(
|
||||
node_id="n4",
|
||||
output_variable="result",
|
||||
json_schema={"type": "object", "properties": {"key": {"type": "string"}}},
|
||||
)
|
||||
|
||||
node_events = [
|
||||
{"answer": '{"key": "val"}', "structured": True},
|
||||
]
|
||||
monkeypatch.setattr(
|
||||
WorkflowNodeAgentFactory,
|
||||
"create",
|
||||
staticmethod(lambda **kwargs: StubNodeAgent(node_events)),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_api_key_for_provider",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_provider_from_model_id",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_model_capabilities",
|
||||
lambda _: {"supports_structured_output": True},
|
||||
)
|
||||
|
||||
list(engine._execute_agent_node(node))
|
||||
assert engine.state["result"] == {"key": "val"}
|
||||
|
||||
def test_json_schema_no_structured_flag_parses_response(self, monkeypatch):
|
||||
"""Cover line 289: json_schema set but no structured flag; non-JSON response raises."""
|
||||
engine = create_engine()
|
||||
engine.state["query"] = "test"
|
||||
node = create_agent_node(
|
||||
node_id="n5",
|
||||
json_schema={"type": "object", "properties": {"x": {"type": "string"}}},
|
||||
)
|
||||
|
||||
node_events = [{"answer": "not valid json"}]
|
||||
monkeypatch.setattr(
|
||||
WorkflowNodeAgentFactory,
|
||||
"create",
|
||||
staticmethod(lambda **kwargs: StubNodeAgent(node_events)),
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_api_key_for_provider",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_provider_from_model_id",
|
||||
lambda _: None,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"application.core.model_utils.get_model_capabilities",
|
||||
lambda _: {"supports_structured_output": True},
|
||||
)
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="Structured output was expected but response was not valid JSON",
|
||||
):
|
||||
list(engine._execute_agent_node(node))
|
||||
|
||||
def test_parse_structured_output_empty_string(self):
|
||||
"""Cover line 355: _parse_structured_output with empty string."""
|
||||
engine = create_engine()
|
||||
success, result = engine._parse_structured_output("")
|
||||
assert success is False
|
||||
assert result is None
|
||||
|
||||
def test_normalize_node_json_schema_invalid(self):
|
||||
"""Cover line 375: _normalize_node_json_schema with invalid schema raises."""
|
||||
engine = create_engine()
|
||||
# A non-dict schema triggers JsonSchemaValidationError
|
||||
with pytest.raises(ValueError, match="Invalid JSON schema"):
|
||||
engine._normalize_node_json_schema("not_a_dict", "TestNode")
|
||||
Reference in new issue
Block a user