|
1 | 1 | import pytest |
| 2 | +from typing import Annotated |
2 | 3 |
|
3 | 4 | from django_ai_core.contrib.agents.base import Agent, AgentParameter |
4 | 5 |
|
@@ -29,6 +30,7 @@ def test_agent_initialization(): |
29 | 30 |
|
30 | 31 | assert agent.slug == "test-agent" |
31 | 32 | assert agent.description == "Test agent for testing" |
| 33 | + assert agent.parameters |
32 | 34 | assert len(agent.parameters) == 2 |
33 | 35 |
|
34 | 36 | # Check parameter definitions |
@@ -86,3 +88,115 @@ def test_agent_parameter_dataclass(): |
86 | 88 | assert param.name == "test" |
87 | 89 | assert param.type is bool |
88 | 90 | assert param.description == "A test parameter" |
| 91 | + |
| 92 | + |
| 93 | +def test_agent_parameter_as_dict(): |
| 94 | + """Test AgentParameter.as_dict() method.""" |
| 95 | + param = AgentParameter( |
| 96 | + name="test_param", |
| 97 | + type=str, |
| 98 | + description="A test parameter", |
| 99 | + ) |
| 100 | + |
| 101 | + result = param.as_dict() |
| 102 | + |
| 103 | + assert result == { |
| 104 | + "name": "test_param", |
| 105 | + "type": "str", |
| 106 | + "description": "A test parameter", |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | +def test_derive_parameters_from_signature(): |
| 111 | + """Test _derive_parameters_from_signature method.""" |
| 112 | + |
| 113 | + class TestAgentWithAnnotations(Agent): |
| 114 | + slug = "test-annotated" |
| 115 | + description = "Test agent with type annotations" |
| 116 | + |
| 117 | + def execute( |
| 118 | + self, |
| 119 | + *, |
| 120 | + name: Annotated[str, "The name parameter"], |
| 121 | + count: Annotated[int, "The count parameter"], |
| 122 | + ): |
| 123 | + return f"Hello {name}, count is {count}" |
| 124 | + |
| 125 | + agent = TestAgentWithAnnotations() |
| 126 | + |
| 127 | + assert agent.parameters |
| 128 | + assert len(agent.parameters) == 2 |
| 129 | + |
| 130 | + name_param = agent.parameters[0] |
| 131 | + assert name_param.name == "name" |
| 132 | + assert name_param.type is str |
| 133 | + assert name_param.description == "The name parameter" |
| 134 | + |
| 135 | + count_param = agent.parameters[1] |
| 136 | + assert count_param.name == "count" |
| 137 | + assert count_param.type is int |
| 138 | + assert count_param.description == "The count parameter" |
| 139 | + |
| 140 | + |
| 141 | +def test_derive_parameters_without_descriptions(): |
| 142 | + """Test parameter derivation without Annotated descriptions.""" |
| 143 | + |
| 144 | + class TestAgentAnnotatedWithoutDescriptions(Agent): |
| 145 | + slug = "test-no-desc" |
| 146 | + description = "Test agent without descriptions" |
| 147 | + |
| 148 | + def execute(self, *, value: str, flag: bool): |
| 149 | + return "result" |
| 150 | + |
| 151 | + agent = TestAgentAnnotatedWithoutDescriptions() |
| 152 | + |
| 153 | + assert agent.parameters |
| 154 | + assert len(agent.parameters) == 2 |
| 155 | + |
| 156 | + value_param = agent.parameters[0] |
| 157 | + assert value_param.name == "value" |
| 158 | + assert value_param.type is str |
| 159 | + assert value_param.description == "" |
| 160 | + |
| 161 | + flag_param = agent.parameters[1] |
| 162 | + assert flag_param.name == "flag" |
| 163 | + assert flag_param.type is bool |
| 164 | + assert flag_param.description == "" |
| 165 | + |
| 166 | + |
| 167 | +def test_explicit_parameters_override_derived(): |
| 168 | + """Test that explicit parameters take precedence over derived ones.""" |
| 169 | + |
| 170 | + class AgentWithExplicitParams(Agent): |
| 171 | + slug = "test-explicit" |
| 172 | + description = "Test agent with explicit parameters" |
| 173 | + parameters = [ |
| 174 | + AgentParameter( |
| 175 | + name="custom", |
| 176 | + type=str, |
| 177 | + description="Custom parameter", |
| 178 | + ), |
| 179 | + ] |
| 180 | + |
| 181 | + def execute(self, *, name: Annotated[str, "Should be ignored"]): |
| 182 | + return "result" |
| 183 | + |
| 184 | + agent = AgentWithExplicitParams() |
| 185 | + |
| 186 | + assert agent.parameters |
| 187 | + assert len(agent.parameters) == 1 |
| 188 | + assert agent.parameters[0].name == "custom" |
| 189 | + assert agent.parameters[0].description == "Custom parameter" |
| 190 | + |
| 191 | + |
| 192 | +def test_agent_without_parameter_schema(): |
| 193 | + class AgentWithNoParams(Agent): |
| 194 | + slug = "test-explicit" |
| 195 | + description = "Test agent with explicit parameters" |
| 196 | + |
| 197 | + def execute(self, *, name): |
| 198 | + return "result" |
| 199 | + |
| 200 | + agent = AgentWithNoParams() |
| 201 | + |
| 202 | + assert not agent.parameters |
0 commit comments