Skip to content

Commit 0f0e386

Browse files
committed
fix: refactored tests to remove code smell
1 parent 2a90803 commit 0f0e386

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1015,8 +1015,8 @@ def __init__(
10151015
def get_openapi_schema(
10161016
self,
10171017
*,
1018-
title: str,
1019-
version: str,
1018+
title: str = "Powertools API",
1019+
version: str = "1.0.0",
10201020
openapi_version: str = "3.1.0",
10211021
summary: Optional[str] = None,
10221022
description: Optional[str] = None,
@@ -1140,8 +1140,8 @@ def get_openapi_schema(
11401140
def get_openapi_json_schema(
11411141
self,
11421142
*,
1143-
title: str,
1144-
version: str,
1143+
title: str = "Powertools API",
1144+
version: str = "1.0.0",
11451145
openapi_version: str = "3.1.0",
11461146
summary: Optional[str] = None,
11471147
description: Optional[str] = None,

tests/functional/event_handler/test_openapi_params.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@
99
Schema,
1010
)
1111

12+
JSON_CONTENT_TYPE = "application/json"
13+
1214

1315
def test_openapi_no_params():
1416
app = ApiGatewayResolver()
1517

1618
@app.get("/")
1719
def handler():
18-
pass
20+
raise NotImplementedError()
1921

20-
schema = app.get_openapi_schema(title="Hello API", version="1.0.0")
22+
schema = app.get_openapi_schema()
23+
assert schema.info.title == "Powertools API"
24+
assert schema.info.version == "1.0.0"
2125

2226
assert len(schema.paths.keys()) == 1
2327
assert "/" in schema.paths
@@ -33,8 +37,8 @@ def handler():
3337
response = get.responses["200"]
3438
assert response.description == "Success"
3539

36-
assert "application/json" in response.content
37-
json_response = response.content["application/json"]
40+
assert JSON_CONTENT_TYPE in response.content
41+
json_response = response.content[JSON_CONTENT_TYPE]
3842
assert json_response.schema_ == Schema()
3943
assert not json_response.examples
4044
assert not json_response.encoding
@@ -45,9 +49,11 @@ def test_openapi_with_scalar_params():
4549

4650
@app.get("/users/<user_id>")
4751
def handler(user_id: str, include_extra: bool = False):
48-
pass
52+
raise NotImplementedError()
4953

50-
schema = app.get_openapi_schema(title="Hello API", version="1.0.0")
54+
schema = app.get_openapi_schema(title="My API", version="0.2.2")
55+
assert schema.info.title == "My API"
56+
assert schema.info.version == "0.2.2"
5157

5258
assert len(schema.paths.keys()) == 1
5359
assert "/users/<user_id>" in schema.paths
@@ -86,13 +92,13 @@ def test_openapi_with_scalar_returns():
8692
def handler() -> str:
8793
return "Hello, world"
8894

89-
schema = app.get_openapi_schema(title="Hello API", version="1.0.0")
95+
schema = app.get_openapi_schema()
9096
assert len(schema.paths.keys()) == 1
9197

9298
get = schema.paths["/"].get
9399
assert get.parameters is None
94100

95-
response = get.responses["200"].content["application/json"]
101+
response = get.responses["200"].content[JSON_CONTENT_TYPE]
96102
assert response.schema_.title == "Return"
97103
assert response.schema_.type == "string"
98104

@@ -107,13 +113,13 @@ class User(BaseModel):
107113
def handler() -> User:
108114
return User(name="Ruben Fonseca")
109115

110-
schema = app.get_openapi_schema(title="Hello API", version="1.0.0")
116+
schema = app.get_openapi_schema()
111117
assert len(schema.paths.keys()) == 1
112118

113119
get = schema.paths["/"].get
114120
assert get.parameters is None
115121

116-
response = get.responses["200"].content["application/json"]
122+
response = get.responses["200"].content[JSON_CONTENT_TYPE]
117123
reference = response.schema_
118124
assert reference.ref == "#/components/schemas/User"
119125

@@ -135,13 +141,13 @@ class User:
135141
def handler() -> User:
136142
return User(name="Ruben Fonseca")
137143

138-
schema = app.get_openapi_schema(title="Hello API", version="1.0.0")
144+
schema = app.get_openapi_schema()
139145
assert len(schema.paths.keys()) == 1
140146

141147
get = schema.paths["/"].get
142148
assert get.parameters is None
143149

144-
response = get.responses["200"].content["application/json"]
150+
response = get.responses["200"].content[JSON_CONTENT_TYPE]
145151
reference = response.schema_
146152
assert reference.ref == "#/components/schemas/User"
147153

0 commit comments

Comments
 (0)