Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
danut.turta1 committed Apr 3, 2024
1 parent ba2eb98 commit b2dbea0
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 26 deletions.
95 changes: 85 additions & 10 deletions openapi_tester/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,109 @@ def request(self, **kwargs) -> Response: # type: ignore[override]
return response

# pylint: disable=W0622
def post(self, path, data=None, format=None, content_type="application/json", follow=False, **extra):
def post(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().post(path, data=data, format=format, content_type=content_type, follow=follow, **extra)
return super().post(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def put(self, path, data=None, format=None, content_type="application/json", follow=False, **extra):
def put(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().put(path, data=data, format=format, content_type=content_type, follow=follow, **extra)
return super().put(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def patch(self, path, data=None, format=None, content_type="application/json", follow=False, **extra):
def patch(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().patch(path, data=data, format=format, content_type=content_type, follow=follow, **extra)
return super().patch(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def delete(self, path, data=None, format=None, content_type="application/json", follow=False, **extra):
def delete(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().delete(path, data=data, format=format, content_type=content_type, follow=follow, **extra)
return super().delete(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

# pylint: disable=W0622
def options(self, path, data=None, format=None, content_type="application/json", follow=False, **extra):
def options(
self,
path,
data=None,
format=None,
content_type="application/json",
follow=False,
**extra,
):
if data and content_type == "application/json":
data = self._serialize(data)
return super().options(path, data=data, format=format, content_type=content_type, follow=follow, **extra)
return super().options(
path,
data=data,
format=format,
content_type=content_type,
follow=follow,
**extra,
)

@staticmethod
def _is_successful_response(response: Response) -> bool:
Expand Down
4 changes: 1 addition & 3 deletions openapi_tester/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
VALIDATE_MINIMUM_NUMBER_OF_PROPERTIES_ERROR = "The number of properties in {data} is fewer than the specified minimum number of properties of {min_length}"
VALIDATE_MAXIMUM_NUMBER_OF_PROPERTIES_ERROR = "The number of properties in {data} exceeds the specified maximum number of properties of {max_length}"
VALIDATE_UNIQUE_ITEMS_ERROR = "The array {data} must contain unique items only"
VALIDATE_NONE_ERROR = (
"A property received a null value in the {http_message} data, but is a non-nullable object in the schema definition"
)
VALIDATE_NONE_ERROR = "A property received a null value in the {http_message} data, but is a non-nullable object in the schema definition"
VALIDATE_MISSING_KEY_ERROR = (
"The following property was found in the schema definition, "
'but is missing from the {http_message} data: "{missing_key}"'
Expand Down
15 changes: 11 additions & 4 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,9 @@ def test_openapi_object(
test_config=drill_down_test_config,
)

def test_openapi_array(self, schema_section: dict[str, Any], data: dict, test_config: OpenAPITestConfig) -> None:
def test_openapi_array(
self, schema_section: dict[str, Any], data: dict, test_config: OpenAPITestConfig
) -> None:
for array_item in data:
array_item_test_config = copy(test_config)
array_item_test_config.reference = f"{test_config.reference}"
Expand Down Expand Up @@ -633,9 +635,12 @@ def validate_request(
test_config.http_message = "request"
else:
test_config = OpenAPITestConfig(
http_message="request", reference=f"{request['REQUEST_METHOD']} {request['PATH_INFO']} > request"
http_message="request",
reference=f"{request['REQUEST_METHOD']} {request['PATH_INFO']} > request",
)
request_body_schema = self.get_request_body_schema_section(request, test_config=test_config)
request_body_schema = self.get_request_body_schema_section(
request, test_config=test_config
)

if request_body_schema:
self.test_schema_section(
Expand Down Expand Up @@ -669,7 +674,9 @@ def validate_response(
http_message="response",
reference=f"{request['REQUEST_METHOD']} {request['PATH_INFO']} > response > {response.status_code}",
)
response_schema = self.get_response_schema_section(response_handler, test_config=test_config)
response_schema = self.get_response_schema_section(
response_handler, test_config=test_config
)
self.test_schema_section(
schema_section=response_schema,
data=response_handler.data,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ def test_get_request(cars_api_schema: "Path"):


def test_post_request(openapi_client):
response = openapi_client.post(path="/api/v1/vehicles", data={"vehicle_type": "suv"})
response = openapi_client.post(
path="/api/v1/vehicles", data={"vehicle_type": "suv"}
)

assert response.status_code == status.HTTP_201_CREATED

Expand Down
6 changes: 5 additions & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ def test_null_error():
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_schema_section(
{"type": "object"}, None, OpenAPITestConfig(reference="POST /endpoint > response > nonNullableObject")
{"type": "object"},
None,
OpenAPITestConfig(
reference="POST /endpoint > response > nonNullableObject"
),
)


Expand Down
27 changes: 20 additions & 7 deletions tests/test_schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def test_example_schemas(filename):
response_handler = ResponseHandlerFactory.create(response)
assert sorted(
schema_tester.get_response_schema_section(
response_handler, test_config=OpenAPITestConfig(case_tester=is_pascal_case)
response_handler,
test_config=OpenAPITestConfig(case_tester=is_pascal_case),
)
) == sorted(schema_section)

Expand Down Expand Up @@ -391,10 +392,14 @@ def test_is_openapi_schema_false():
assert schema_tester.is_openapi_schema() is False


def test_get_request_body_schema_section(pets_post_request: dict[str, Any], pets_api_schema: Path):
def test_get_request_body_schema_section(
pets_post_request: dict[str, Any], pets_api_schema: Path
):
test_config = OpenAPITestConfig(case_tester=is_pascal_case)
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
schema_section = schema_tester.get_request_body_schema_section(pets_post_request, test_config=test_config)
schema_section = schema_tester.get_request_body_schema_section(
pets_post_request, test_config=test_config
)
assert schema_section == {
"type": "object",
"required": ["name"],
Expand All @@ -408,15 +413,21 @@ def test_get_request_body_schema_section_content_type_no_application_json(
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
test_config = OpenAPITestConfig(case_tester=is_pascal_case)
pets_post_request["CONTENT_TYPE"] = "application/xml"
schema_section = schema_tester.get_request_body_schema_section(pets_post_request, test_config=test_config)
schema_section = schema_tester.get_request_body_schema_section(
pets_post_request, test_config=test_config
)
assert schema_section == {}


def test_get_request_body_schema_section_no_content_request(pets_post_request: dict[str, Any], pets_api_schema: Path):
def test_get_request_body_schema_section_no_content_request(
pets_post_request: dict[str, Any], pets_api_schema: Path
):
test_config = OpenAPITestConfig(case_tester=is_pascal_case)
schema_tester = SchemaTester(schema_file_path=str(pets_api_schema))
del pets_post_request["wsgi.input"]
schema_section = schema_tester.get_request_body_schema_section(pets_post_request, test_config=test_config)
schema_section = schema_tester.get_request_body_schema_section(
pets_post_request, test_config=test_config
)
assert schema_section == {}


Expand Down Expand Up @@ -472,7 +483,9 @@ def test_nullable_validation():
# A null value should always raise an error
with pytest.raises(
DocumentationError,
match=VALIDATE_NONE_ERROR.format(expected=OPENAPI_PYTHON_MAPPING[schema["type"]], http_message="response"),
match=VALIDATE_NONE_ERROR.format(
expected=OPENAPI_PYTHON_MAPPING[schema["type"]], http_message="response"
),
):
tester.test_schema_section(schema, None)

Expand Down

0 comments on commit b2dbea0

Please sign in to comment.