Skip to content

Commit e97d12b

Browse files
committed
fix(ci): restore test files to maintain 100% coverage
Accidentally removed real test files that were mimicking the naming convention of the scratch files. Restoring them brings test coverage back to 100% and resolves the pre-commit badge hook failure in CI.
1 parent 2e1fba4 commit e97d12b

4 files changed

Lines changed: 227 additions & 2 deletions

File tree

‎.gitignore‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,3 @@ celerybeat.pid
169169
/update_tests*.py
170170
/fix_*.py
171171

172-
# Scratch numbered tests
173-
tests/*[0-9].py

‎tests/test_cli_extra2.py‎

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 == ""

‎tests/test_cli_extra3.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json
2+
from openapi_client.cli import process_from_openapi, sync_to_openapi
3+
4+
5+
def test_snapshot_roundtrip(tmp_path):
6+
"""Test that a snapshot is created during to_sdk and correctly parsed during from_sdk."""
7+
spec_json = {
8+
"openapi": "3.2.0",
9+
"info": {"title": "Test", "version": "1.0"},
10+
"paths": {},
11+
"components": {"schemas": {}},
12+
"webhooks": {
13+
"myWebhook": {
14+
"post": {
15+
"requestBody": {
16+
"content": {"application/json": {"schema": {"type": "object"}}}
17+
},
18+
"responses": {"200": {"description": "OK"}},
19+
}
20+
}
21+
},
22+
}
23+
24+
in_file = tmp_path / "in.json"
25+
in_file.write_text(json.dumps(spec_json))
26+
27+
out_sdk_dir = tmp_path / "sdk"
28+
out_sdk_dir.mkdir()
29+
30+
# Run to_sdk
31+
process_from_openapi("to_sdk", str(in_file), None, str(out_sdk_dir))
32+
33+
assert (out_sdk_dir / "src" / "openapi.snapshot.json").exists()
34+
35+
# Run sync_to_openapi (to_openapi)
36+
out_spec = tmp_path / "out.json"
37+
sync_to_openapi(out_sdk_dir, out_spec)
38+
39+
out_spec_parsed = json.loads(out_spec.read_text())
40+
assert "webhooks" in out_spec_parsed
41+
assert "myWebhook" in out_spec_parsed["webhooks"]

‎tests/test_coverage_emit_test2.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def test_emit_tests_other_param_exception_real():
2+
from openapi_client.tests.emit import emit_operation_test
3+
from openapi_client.models import Operation, Parameter
4+
5+
op = Operation(
6+
operationId="test_other_param_exception",
7+
parameters=[Parameter(name="other_param_3", in_="query")],
8+
)
9+
func = emit_operation_test("post", "/test", op)
10+
assert func.name.value == "test_test_other_param_exception"
11+
12+
13+
def test_emit_tests_body_param_exception_real():
14+
from openapi_client.tests.emit import emit_operation_test
15+
from openapi_client.models import Operation, Parameter
16+
17+
op = Operation(
18+
operationId="test_body_param_exception_real",
19+
parameters=[Parameter(name="body_param_real", in_="body")],
20+
)
21+
func = emit_operation_test("post", "/test", op)
22+
assert func.name.value == "test_test_body_param_exception_real"

0 commit comments

Comments
 (0)