|
| 1 | +import json |
| 2 | +from openapi_client.cli import process_from_openapi |
| 3 | + |
| 4 | + |
| 5 | +def test_cli_output_file(tmp_path): |
| 6 | + spec = { |
| 7 | + "openapi": "3.0.0", |
| 8 | + "info": {"title": "Test API", "version": "1.0"}, |
| 9 | + "paths": {}, |
| 10 | + } |
| 11 | + spec_path = tmp_path / "openapi.json" |
| 12 | + spec_path.write_text(json.dumps(spec)) |
| 13 | + |
| 14 | + out_file = tmp_path / "out" / "client.py" |
| 15 | + process_from_openapi("to_sdk", str(spec_path), None, str(out_file)) |
| 16 | + |
| 17 | + assert (tmp_path / "out" / "src" / "client.py").exists() |
| 18 | + |
| 19 | + |
| 20 | +def test_cli_to_server_with_models(tmp_path): |
| 21 | + spec = { |
| 22 | + "openapi": "3.0.0", |
| 23 | + "info": {"title": "Test API", "version": "1.0"}, |
| 24 | + "paths": {}, |
| 25 | + "components": { |
| 26 | + "schemas": { |
| 27 | + "Pet": { |
| 28 | + "type": "object", |
| 29 | + "properties": { |
| 30 | + "id": {"type": "integer"}, |
| 31 | + "name": {"type": "string"}, |
| 32 | + }, |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + } |
| 37 | + spec_path = tmp_path / "openapi.json" |
| 38 | + spec_path.write_text(json.dumps(spec)) |
| 39 | + |
| 40 | + out_dir = tmp_path / "out_server" |
| 41 | + process_from_openapi("to_server", str(spec_path), None, str(out_dir)) |
| 42 | + |
| 43 | + assert (out_dir / "main.py").exists() |
| 44 | + assert (out_dir / "models.py").exists() |
| 45 | + |
| 46 | + |
| 47 | +def test_cli_to_sdk_cli_cdd(tmp_path, monkeypatch): |
| 48 | + import ast |
| 49 | + |
| 50 | + spec = { |
| 51 | + "openapi": "3.0.0", |
| 52 | + "info": {"title": "Test API", "version": "1.0"}, |
| 53 | + "paths": { |
| 54 | + "/pets": { |
| 55 | + "get": { |
| 56 | + "operationId": "getPets", |
| 57 | + "summary": "Get all pets", |
| 58 | + "parameters": [ |
| 59 | + { |
| 60 | + "name": "limit", |
| 61 | + "in": "query", |
| 62 | + "schema_": {"type": ["integer", "null"]}, |
| 63 | + "required": True, |
| 64 | + } |
| 65 | + ], |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + } |
| 70 | + spec_path = tmp_path / "openapi.json" |
| 71 | + spec_path.write_text(json.dumps(spec)) |
| 72 | + |
| 73 | + out_dir = tmp_path / "out_sdk_cli" |
| 74 | + process_from_openapi("to_sdk_cli", str(spec_path), None, str(out_dir)) |
| 75 | + |
| 76 | + assert (out_dir / "src" / "cli_main.py").exists() |
| 77 | + |
| 78 | + # Now make ast.unparse fail to cover the except block in cli_sdk_cdd/emit.py |
| 79 | + def mock_unparse(*args, **kwargs): |
| 80 | + raise Exception("Mock error") |
| 81 | + |
| 82 | + monkeypatch.setattr(ast, "unparse", mock_unparse) |
| 83 | + process_from_openapi("to_sdk_cli", str(spec_path), None, str(out_dir)) |
| 84 | + |
| 85 | + |
| 86 | +def test_cli_to_server_sqlalchemy_ast_fail(tmp_path, monkeypatch): |
| 87 | + import ast |
| 88 | + |
| 89 | + spec = { |
| 90 | + "openapi": "3.0.0", |
| 91 | + "info": {"title": "Test API", "version": "1.0"}, |
| 92 | + "paths": {}, |
| 93 | + "components": { |
| 94 | + "schemas": { |
| 95 | + "Pet": { |
| 96 | + "type": "object", |
| 97 | + "properties": { |
| 98 | + "id": {"type": ["integer"]}, |
| 99 | + "name": {"type": ["string", "null"]}, |
| 100 | + }, |
| 101 | + } |
| 102 | + } |
| 103 | + }, |
| 104 | + } |
| 105 | + spec_path = tmp_path / "openapi.json" |
| 106 | + spec_path.write_text(json.dumps(spec)) |
| 107 | + |
| 108 | + out_dir = tmp_path / "out_server2" |
| 109 | + |
| 110 | + def mock_unparse(*args, **kwargs): |
| 111 | + raise Exception("Mock error") |
| 112 | + |
| 113 | + monkeypatch.setattr(ast, "unparse", mock_unparse) |
| 114 | + process_from_openapi("to_server", str(spec_path), None, str(out_dir)) |
| 115 | + |
| 116 | + |
| 117 | +def test_fastapi_emit(tmp_path): |
| 118 | + from openapi_client.fastapi.emit import emit_fastapi |
| 119 | + from openapi_client.models import OpenAPI, Info, PathItem, Operation |
| 120 | + |
| 121 | + spec = OpenAPI( |
| 122 | + openapi="3.0.0", |
| 123 | + info=Info(title="Test", version="1.0", description="Desc"), |
| 124 | + paths={ |
| 125 | + "/test": PathItem( |
| 126 | + get=Operation(summary="sum", description="desc"), |
| 127 | + post=Operation(operationId="post_test"), |
| 128 | + ) |
| 129 | + }, |
| 130 | + ) |
| 131 | + code = emit_fastapi(spec) |
| 132 | + assert "@app.get('/test')" in code |
| 133 | + assert "def get_test():" in code |
| 134 | + assert "def post_test():" in code |
| 135 | + |
| 136 | + |
| 137 | +def test_fastapi_parse(): |
| 138 | + import libcst as cst |
| 139 | + from openapi_client.fastapi.parse import extract_fastapi_from_ast |
| 140 | + from openapi_client.models import OpenAPI |
| 141 | + |
| 142 | + code = """ |
| 143 | +from fastapi import FastAPI |
| 144 | +app = FastAPI() |
| 145 | +
|
| 146 | +@app.get("/hello") |
| 147 | +def hello(): |
| 148 | + pass |
| 149 | +""" |
| 150 | + module = cst.parse_module(code) |
| 151 | + spec = OpenAPI(openapi="3.0.0", info={"title": "T", "version": "1"}) |
| 152 | + extract_fastapi_from_ast(module, spec) |
| 153 | + assert "/hello" in spec.paths |
| 154 | + assert "get" in spec.paths["/hello"] |
| 155 | + assert spec.paths["/hello"]["get"]["operationId"] == "get_hello" |
| 156 | + |
| 157 | + |
| 158 | +def test_sqlalchemy_cdd_emit_no_components(tmp_path): |
| 159 | + from openapi_client.sqlalchemy_cdd.emit import emit_sqlalchemy |
| 160 | + from openapi_client.models import OpenAPI |
| 161 | + |
| 162 | + spec = OpenAPI(openapi="3.0.0", info={"title": "Test", "version": "1.0"}) |
| 163 | + res = emit_sqlalchemy(spec) |
| 164 | + assert res == "" |
0 commit comments