Skip to content

Commit

Permalink
chore: disabling codecov, adding serialization function
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Cardenas committed May 7, 2024
1 parent 2a91ece commit db0c2d7
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ jobs:
- uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: true
fail_ci_if_error: false
token: ${{ secrets.CODECOV_TOKEN }}
18 changes: 11 additions & 7 deletions openapi_tester/schema_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
UrlStaticSchemaLoader,
)
from openapi_tester.response_handler_factory import ResponseHandlerFactory
from openapi_tester.utils import lazy_combinations, normalize_schema_section
from openapi_tester.utils import (
lazy_combinations,
normalize_schema_section,
serialize_schema_section_data,
)
from openapi_tester.validators import (
validate_enum,
validate_format,
Expand Down Expand Up @@ -554,8 +558,8 @@ def test_openapi_object(
f"{VALIDATE_MISSING_KEY_ERROR.format(missing_key=key, http_message=test_config.http_message)}"
"\n\nReference:"
f"\n\n{test_config.reference} > {key}"
f"\n\n{test_config.http_message.capitalize()} body:\n {json.dumps(data, indent=4)}"
f"\nSchema section:\n {json.dumps(properties, indent=4)}"
f"\n\n{test_config.http_message.capitalize()} body:\n {serialize_schema_section_data(data=data)}"
f"\nSchema section:\n {serialize_schema_section_data(data=properties)}"
"\n\nHint: Remove the key from your OpenAPI docs, or"
f" include it in your API {test_config.http_message}"
)
Expand All @@ -566,17 +570,17 @@ def test_openapi_object(
f"{VALIDATE_EXCESS_KEY_ERROR.format(excess_key=key, http_message=test_config.http_message)}"
"\n\nReference:"
f"\n\n{test_config.reference} > {key}"
f"\n\n{test_config.http_message.capitalize()} body:\n {json.dumps(data, indent=4)}"
f"\n\nSchema section:\n {json.dumps(properties, indent=4, default=str)}"
f"\n\n{test_config.http_message.capitalize()} body:\n {serialize_schema_section_data(data=data)}"
f"\n\nSchema section:\n {serialize_schema_section_data(data=properties)}"
"\n\nHint: Remove the key from your API"
f" {test_config.http_message}, or include it in your OpenAPI docs"
)
if key in write_only_properties:
raise DocumentationError(
f"{VALIDATE_WRITE_ONLY_RESPONSE_KEY_ERROR.format(write_only_key=key)}\n\nReference:"
f"\n\n{test_config.reference} > {key}"
f"\n\n{test_config.http_message.capitalize()} body:\n {json.dumps(data, indent=4)}"
f"\nSchema section:\n {json.dumps(properties, indent=4)}"
f"\n\n{test_config.http_message.capitalize()} body:\n {serialize_schema_section_data(data=data)}"
f"\nSchema section:\n {serialize_schema_section_data(data=properties)}"
f"\n\nHint: Remove the key from your API {test_config.http_message}, or"
' remove the "WriteOnly" restriction'
)
Expand Down
5 changes: 5 additions & 0 deletions openapi_tester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import annotations

import json
from copy import deepcopy
from itertools import chain, combinations
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -55,6 +56,10 @@ def normalize_schema_section(schema_section: dict[str, Any]) -> dict[str, Any]:
return output


def serialize_schema_section_data(data: dict[str, Any]) -> str:
return json.dumps(data, indent=4, default=str)


def lazy_combinations(options_list: Sequence[dict[str, Any]]) -> Iterator[dict]:
"""
Lazily evaluate possible combinations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ def test_key_in_write_only_properties_error():
{"one": 1},
OpenAPITestConfig(reference="POST /endpoint > response"),
)


def test_date_serialization():
tester = SchemaTester()
tester.test_openapi_object(
{"properties": {"updated_at": {"type": "string", "format": "date-time"}}},
{"updated_at": "2021-12-12T12:12:12Z"},
OpenAPITestConfig(reference="POST /endpoint > response"),
)


def test_wrong_date_error():
tester = SchemaTester()
expected_error_message = (
'\n\nExpected: a "date-time" formatted "string" value\n\nReceived: '
'"not-a-date"\n\nReference: \n\nPOST /endpoint > response > updated_at\n\n Response value:\n '
"not-a-date\n Schema description:\n {'type': 'string', 'format': 'date-time'}"
)

with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"properties": {"updated_at": {"type": "string", "format": "date-time"}}},
{"updated_at": "not-a-date"},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
28 changes: 27 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from openapi_tester.utils import merge_objects
from openapi_tester.utils import merge_objects, serialize_schema_section_data
from tests.utils import sort_object

object_1 = {
Expand Down Expand Up @@ -42,3 +42,29 @@ def test_merge_objects():
"properties": {"key1": {"type": "string"}, "key2": {"type": "string"}},
}
assert sort_object(merge_objects(test_schemas)) == sort_object(expected)


def test_serialize_schema_section_data():
data = {
"type": "object",
"required": ["key1", "key2"],
"properties": {"key1": {"type": "string"}, "key2": {"type": "string"}},
}
serialized_data = serialize_schema_section_data(data=data)
assert serialized_data == (
"{\n"
' "type": "object",\n'
' "required": [\n'
' "key1",\n'
' "key2"\n'
" ],\n"
' "properties": {\n'
' "key1": {\n'
' "type": "string"\n'
" },\n"
' "key2": {\n'
' "type": "string"\n'
" }\n"
" }\n"
"}"
)

0 comments on commit db0c2d7

Please sign in to comment.