|
| 1 | +import libcst as cst |
| 2 | +from unittest.mock import patch |
| 3 | +from openapi_client.classes.parse import ClassExtractor, extract_classes_from_ast |
| 4 | +from openapi_client.models import OpenAPI |
| 5 | +from openapi_client.cli_sdk.emit import emit_cli |
| 6 | +from openapi_client.models import PathItem, Operation, Parameter |
| 7 | + |
| 8 | +from openapi_client.classes.emit import emit_classes |
| 9 | +from openapi_client.models import Schema |
| 10 | + |
| 11 | + |
| 12 | +def test_classes_emit_branches(): |
| 13 | + # 67->71: empty schemas |
| 14 | + emit_classes({}) |
| 15 | + |
| 16 | + # 69->68: non-object schema |
| 17 | + schema = Schema(type="string") |
| 18 | + emit_classes({"test": schema}) |
| 19 | + |
| 20 | + schema_obj = Schema(type="object", properties={"test_prop": Schema(type="array")}) |
| 21 | + emit_classes({"test_obj": schema_obj}) |
| 22 | + |
| 23 | + |
| 24 | +def test_classes_parse_branches(): |
| 25 | + # We will construct a cst.Module with various class definitions to hit the branches. |
| 26 | + code = """ |
| 27 | +class MyClass: |
| 28 | + # 45->44: statement is not SimpleStatementLine (it's a FunctionDef) |
| 29 | + def my_method(self): |
| 30 | + pass |
| 31 | +
|
| 32 | + # 49->46: target is not Name (it's Attribute) |
| 33 | + some_obj.attr: int |
| 34 | +
|
| 35 | + # 60->71: slice_elements[0].slice is not Index (it's Slice) |
| 36 | + prop1: Optional[1:2] |
| 37 | +
|
| 38 | + # 64->71: index_val is not Name (it's SimpleString) |
| 39 | + prop2: Optional["string"] |
| 40 | +
|
| 41 | + # normal Optional |
| 42 | + prop3: Optional[str] |
| 43 | +""" |
| 44 | + module = cst.parse_module(code) |
| 45 | + spec = OpenAPI( |
| 46 | + openapi="3.1.0", info={"title": "Test", "version": "1.0.0"}, components=None |
| 47 | + ) |
| 48 | + extract_classes_from_ast(module, spec) |
| 49 | + |
| 50 | + |
| 51 | +def test_classes_parse_schema_properties_none(): |
| 52 | + code = "class MyClass:\n prop: int\n" |
| 53 | + module = cst.parse_module(code) |
| 54 | + spec = OpenAPI(openapi="3.1.0", info={"title": "Test", "version": "1.0.0"}) |
| 55 | + visitor = ClassExtractor(spec) |
| 56 | + # mock schema.properties to be None during the loop |
| 57 | + from unittest.mock import patch |
| 58 | + |
| 59 | + with patch("openapi_client.classes.parse.Schema") as mock_schema_cls: |
| 60 | + mock_schema_instance = mock_schema_cls.return_value |
| 61 | + mock_schema_instance.properties = None |
| 62 | + module.visit(visitor) |
| 63 | + |
| 64 | + |
| 65 | +def test_classes_parse_components_none(): |
| 66 | + code = "class MyClass:\n pass\n" |
| 67 | + module = cst.parse_module(code) |
| 68 | + spec = OpenAPI(openapi="3.1.0", info={"title": "Test", "version": "1.0.0"}) |
| 69 | + visitor = ClassExtractor(spec) |
| 70 | + visitor.spec.components = None # set it to None to hit 76->exit |
| 71 | + module.visit(visitor) |
| 72 | + |
| 73 | + |
| 74 | +class MockSchemaForProperties: |
| 75 | + def __init__(self, **kwargs): |
| 76 | + self._properties = None |
| 77 | + |
| 78 | + @property |
| 79 | + def properties(self): |
| 80 | + return self._properties |
| 81 | + |
| 82 | + @properties.setter |
| 83 | + def properties(self, value): |
| 84 | + # ignore assignment |
| 85 | + pass |
| 86 | + |
| 87 | + |
| 88 | +def test_classes_parse_schema_properties_none_fix(): |
| 89 | + code = "class MyClass:\n prop: int\n" |
| 90 | + module = cst.parse_module(code) |
| 91 | + spec = OpenAPI(openapi="3.1.0", info={"title": "Test", "version": "1.0.0"}) |
| 92 | + visitor = ClassExtractor(spec) |
| 93 | + with patch("openapi_client.classes.parse.Schema", MockSchemaForProperties): |
| 94 | + module.visit(visitor) |
| 95 | + |
| 96 | + |
| 97 | +def test_cli_sdk_emit_branches(): |
| 98 | + spec = OpenAPI(openapi="3.1.0", info={"title": "Test", "version": "1.0.0"}) |
| 99 | + |
| 100 | + # Path with an operation that has NO parameters (hits 127->85) |
| 101 | + # And another operation that HAS parameters, but not required (hits 143->152) |
| 102 | + spec.paths = { |
| 103 | + "/test1": PathItem( |
| 104 | + get=Operation( |
| 105 | + operationId="test1", |
| 106 | + responses={}, |
| 107 | + parameters=[], # empty parameters |
| 108 | + ), |
| 109 | + post=Operation( |
| 110 | + operationId="test2", |
| 111 | + responses={}, |
| 112 | + parameters=[ |
| 113 | + Parameter(name="param1", **{"in": "query", "required": False}) |
| 114 | + ], # not required |
| 115 | + ), |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + module = emit_cli(spec) |
| 120 | + assert module is not None |
0 commit comments