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 5, 2024
1 parent 2a91ece commit 8ef27d9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
1 change: 0 additions & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,4 @@ jobs:
- uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
fail_ci_if_error: true
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-12"},
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"),
)

0 comments on commit 8ef27d9

Please sign in to comment.