|
5 | 5 | """ |
6 | 6 |
|
7 | 7 | import importlib |
| 8 | +import inspect |
8 | 9 |
|
9 | 10 | import pytest |
10 | 11 |
|
@@ -216,3 +217,152 @@ async def test_all_use_cases_MUST_have_matching_response(self, repo): |
216 | 217 | assert not violations, "Use cases missing matching responses:\n" + "\n".join( |
217 | 218 | violations |
218 | 219 | ) |
| 220 | + |
| 221 | + @pytest.mark.asyncio |
| 222 | + async def test_execute_MUST_accept_matching_request(self, repo): |
| 223 | + """execute() MUST declare its first parameter as {Prefix}Request. |
| 224 | +
|
| 225 | + Ensures the request class is part of the method's contract, not |
| 226 | + just present in the module. |
| 227 | + """ |
| 228 | + use_case = ListUseCasesUseCase(repo) |
| 229 | + response = await use_case.execute(ListCodeArtifactsRequest()) |
| 230 | + |
| 231 | + bounded_contexts = await repo.list_all() |
| 232 | + import_paths = {bc.slug: bc.import_path for bc in bounded_contexts} |
| 233 | + |
| 234 | + violations = [] |
| 235 | + suffix_len = len(USE_CASE_SUFFIX) |
| 236 | + for artifact in response.artifacts: |
| 237 | + name = artifact.artifact.name |
| 238 | + ctx = artifact.bounded_context |
| 239 | + if name in GENERIC_BASE_CLASSES: |
| 240 | + continue |
| 241 | + if not name.endswith(USE_CASE_SUFFIX): |
| 242 | + continue |
| 243 | + |
| 244 | + prefix = name[:-suffix_len] |
| 245 | + expected_request = f"{prefix}{REQUEST_SUFFIX}" |
| 246 | + |
| 247 | + bc_import_path = import_paths.get(ctx) |
| 248 | + cls = ( |
| 249 | + _resolve_class(bc_import_path, artifact.artifact.file, name) |
| 250 | + if bc_import_path |
| 251 | + else None |
| 252 | + ) |
| 253 | + |
| 254 | + if cls is not None: |
| 255 | + execute = getattr(cls, "execute", None) |
| 256 | + if not callable(execute): |
| 257 | + continue |
| 258 | + sig = inspect.signature(execute) |
| 259 | + params = [p for p in sig.parameters.values() if p.name != "self"] |
| 260 | + if not params: |
| 261 | + violations.append( |
| 262 | + f"{ctx}.{name}: execute() has no request parameter" |
| 263 | + ) |
| 264 | + continue |
| 265 | + annotation = params[0].annotation |
| 266 | + actual_name = ( |
| 267 | + annotation.__name__ |
| 268 | + if hasattr(annotation, "__name__") |
| 269 | + else str(annotation) |
| 270 | + ) |
| 271 | + else: |
| 272 | + # Fall back to AST-parsed signature |
| 273 | + execute_method = next( |
| 274 | + (m for m in artifact.artifact.methods if m.name == "execute"), |
| 275 | + None, |
| 276 | + ) |
| 277 | + if execute_method is None: |
| 278 | + continue |
| 279 | + if not execute_method.parameters: |
| 280 | + violations.append( |
| 281 | + f"{ctx}.{name}: execute() has no request parameter" |
| 282 | + ) |
| 283 | + continue |
| 284 | + actual_name = execute_method.parameters[0].type_annotation |
| 285 | + |
| 286 | + if actual_name != expected_request: |
| 287 | + violations.append( |
| 288 | + f"{ctx}.{name}: execute() first parameter is '{actual_name}'" |
| 289 | + f", expected '{expected_request}'" |
| 290 | + ) |
| 291 | + |
| 292 | + assert not violations, "Use cases with wrong request type:\n" + "\n".join( |
| 293 | + violations |
| 294 | + ) |
| 295 | + |
| 296 | + @pytest.mark.asyncio |
| 297 | + async def test_execute_MUST_return_matching_response(self, repo): |
| 298 | + """execute() MUST declare its return type as {Prefix}Response. |
| 299 | +
|
| 300 | + Ensures the response class is actually wired into execute(), not |
| 301 | + just present in the module. |
| 302 | + """ |
| 303 | + use_case = ListUseCasesUseCase(repo) |
| 304 | + response = await use_case.execute(ListCodeArtifactsRequest()) |
| 305 | + |
| 306 | + bounded_contexts = await repo.list_all() |
| 307 | + import_paths = {bc.slug: bc.import_path for bc in bounded_contexts} |
| 308 | + |
| 309 | + violations = [] |
| 310 | + suffix_len = len(USE_CASE_SUFFIX) |
| 311 | + for artifact in response.artifacts: |
| 312 | + name = artifact.artifact.name |
| 313 | + ctx = artifact.bounded_context |
| 314 | + if name in GENERIC_BASE_CLASSES: |
| 315 | + continue |
| 316 | + if not name.endswith(USE_CASE_SUFFIX): |
| 317 | + continue |
| 318 | + |
| 319 | + prefix = name[:-suffix_len] |
| 320 | + expected_response = f"{prefix}{RESPONSE_SUFFIX}" |
| 321 | + |
| 322 | + bc_import_path = import_paths.get(ctx) |
| 323 | + cls = ( |
| 324 | + _resolve_class(bc_import_path, artifact.artifact.file, name) |
| 325 | + if bc_import_path |
| 326 | + else None |
| 327 | + ) |
| 328 | + |
| 329 | + if cls is not None: |
| 330 | + execute = getattr(cls, "execute", None) |
| 331 | + if not callable(execute): |
| 332 | + continue |
| 333 | + sig = inspect.signature(execute) |
| 334 | + return_annotation = sig.return_annotation |
| 335 | + if return_annotation is inspect.Parameter.empty: |
| 336 | + violations.append( |
| 337 | + f"{ctx}.{name}: execute() has no return annotation" |
| 338 | + ) |
| 339 | + continue |
| 340 | + actual_name = ( |
| 341 | + return_annotation.__name__ |
| 342 | + if hasattr(return_annotation, "__name__") |
| 343 | + else str(return_annotation) |
| 344 | + ) |
| 345 | + else: |
| 346 | + # Fall back to AST-parsed signature |
| 347 | + execute_method = next( |
| 348 | + (m for m in artifact.artifact.methods if m.name == "execute"), |
| 349 | + None, |
| 350 | + ) |
| 351 | + if execute_method is None: |
| 352 | + continue |
| 353 | + if not execute_method.return_type: |
| 354 | + violations.append( |
| 355 | + f"{ctx}.{name}: execute() has no return annotation" |
| 356 | + ) |
| 357 | + continue |
| 358 | + actual_name = execute_method.return_type |
| 359 | + |
| 360 | + if actual_name != expected_response: |
| 361 | + violations.append( |
| 362 | + f"{ctx}.{name}: execute() returns '{actual_name}'" |
| 363 | + f", expected '{expected_response}'" |
| 364 | + ) |
| 365 | + |
| 366 | + assert not violations, "Use cases with wrong return type:\n" + "\n".join( |
| 367 | + violations |
| 368 | + ) |
0 commit comments