@@ -44,10 +44,12 @@ def _nested_template_value(depth: int) -> object:
4444
4545
4646def test_control_definition_requires_template_fields_together () -> None :
47+ # Given: a rendered control that only includes template metadata without template values
4748 with pytest .raises (
4849 ValidationError ,
4950 match = "template and template_values must both be present or both absent" ,
5051 ):
52+ # When: validating the control definition model
5153 ControlDefinition .model_validate (
5254 {
5355 "execution" : "server" ,
@@ -63,13 +65,16 @@ def test_control_definition_requires_template_fields_together() -> None:
6365 "template" : VALID_TEMPLATE ,
6466 }
6567 )
68+ # Then: the model rejects the partial template-backed control shape
6669
6770
6871def test_control_definition_rejects_template_values_without_template () -> None :
72+ # Given: a rendered control that only includes template values without template metadata
6973 with pytest .raises (
7074 ValidationError ,
7175 match = "template and template_values must both be present or both absent" ,
7276 ):
77+ # When: validating the control definition model
7378 ControlDefinition .model_validate (
7479 {
7580 "execution" : "server" ,
@@ -85,13 +90,16 @@ def test_control_definition_rejects_template_values_without_template() -> None:
8590 "template_values" : {"pattern" : "hello" },
8691 }
8792 )
93+ # Then: the model rejects the partial template-backed control shape
8894
8995
9096def test_template_definition_rejects_invalid_parameter_name () -> None :
97+ # Given: a template definition with an invalid parameter name
9198 with pytest .raises (
9299 ValidationError ,
93100 match = r"Parameter names must match \[a-zA-Z_\]\[a-zA-Z0-9_\]\*" ,
94101 ):
102+ # When: validating the template definition model
95103 TemplateDefinition .model_validate (
96104 {
97105 "parameters" : {
@@ -103,35 +111,43 @@ def test_template_definition_rejects_invalid_parameter_name() -> None:
103111 "definition_template" : {},
104112 }
105113 )
114+ # Then: the invalid parameter name is rejected
106115
107116
108117def test_template_definition_rejects_excessive_nesting () -> None :
118+ # Given: a template definition whose structure exceeds the nesting limit
109119 with pytest .raises (
110120 ValidationError ,
111121 match = "definition_template nesting depth exceeds maximum" ,
112122 ):
123+ # When: validating the template definition model
113124 TemplateDefinition .model_validate (
114125 {
115126 "parameters" : {},
116127 "definition_template" : _nested_template_value (12 ),
117128 }
118129 )
130+ # Then: the model rejects the deeply nested template
119131
120132
121133def test_template_definition_rejects_excessive_size () -> None :
134+ # Given: a template definition whose structure exceeds the size limit
122135 with pytest .raises (
123136 ValidationError ,
124137 match = "definition_template size exceeds maximum" ,
125138 ):
139+ # When: validating the template definition model
126140 TemplateDefinition .model_validate (
127141 {
128142 "parameters" : {},
129143 "definition_template" : list (range (1001 )),
130144 }
131145 )
146+ # Then: the model rejects the oversized template
132147
133148
134149def test_create_control_request_parses_template_payload_as_template_input () -> None :
150+ # Given: a create request payload containing template-backed control input
135151 request = CreateControlRequest .model_validate (
136152 {
137153 "name" : "template-control" ,
@@ -142,17 +158,21 @@ def test_create_control_request_parses_template_payload_as_template_input() -> N
142158 }
143159 )
144160
161+ # When: the request model parses the payload
162+ # Then: the control input is discriminated as template-backed input
145163 assert isinstance (request .data , TemplateControlInput )
146164
147165
148166def test_create_control_request_rejects_mixed_raw_and_template_payload () -> None :
167+ # Given: a create request payload that mixes template and rendered control fields
149168 with pytest .raises (
150169 ValidationError ,
151170 match = (
152171 "Template-backed control input cannot mix template fields with rendered "
153172 "control fields"
154173 ),
155174 ):
175+ # When: the request model parses the mixed payload
156176 CreateControlRequest .model_validate (
157177 {
158178 "name" : "template-control" ,
@@ -163,9 +183,11 @@ def test_create_control_request_rejects_mixed_raw_and_template_payload() -> None
163183 },
164184 }
165185 )
186+ # Then: the mixed payload is rejected clearly
166187
167188
168189def test_control_definition_can_round_trip_to_template_control_input () -> None :
190+ # Given: a stored template-backed control definition
169191 control = ControlDefinition .model_validate (
170192 {
171193 "execution" : "server" ,
@@ -183,15 +205,18 @@ def test_control_definition_can_round_trip_to_template_control_input() -> None:
183205 }
184206 )
185207
208+ # When: converting the stored control back into template input
186209 template_input = control .to_template_control_input ()
187210
211+ # Then: the template metadata and values round-trip unchanged
188212 assert template_input .template .parameters ["pattern" ].label == "Pattern"
189213 assert template_input .template .parameters ["pattern" ].type == "regex_re2"
190214 assert template_input .template .definition_template == VALID_TEMPLATE ["definition_template" ]
191215 assert template_input .template_values == {"pattern" : "hello" }
192216
193217
194218def test_control_definition_to_template_control_input_rejects_raw_control () -> None :
219+ # Given: a raw control definition without template metadata
195220 control = ControlDefinition .model_validate (
196221 {
197222 "execution" : "server" ,
@@ -207,11 +232,14 @@ def test_control_definition_to_template_control_input_rejects_raw_control() -> N
207232 }
208233 )
209234
235+ # When: converting the raw control into template input
210236 with pytest .raises (ValueError , match = "not template-backed" ):
211237 control .to_template_control_input ()
238+ # Then: the helper rejects the non-template-backed control
212239
213240
214241def test_control_definition_runtime_ignores_template_metadata () -> None :
242+ # Given: a stored template-backed control definition with template metadata
215243 runtime_control = ControlDefinitionRuntime .model_validate (
216244 {
217245 "execution" : "server" ,
@@ -229,5 +257,7 @@ def test_control_definition_runtime_ignores_template_metadata() -> None:
229257 }
230258 )
231259
260+ # When: validating it through the runtime-only model
261+ # Then: runtime parsing succeeds while ignoring template metadata
232262 assert runtime_control .execution == "server"
233263 assert runtime_control .action .decision == "deny"
0 commit comments