Skip to content

Commit

Permalink
chore: splitting and improving tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matias Cardenas committed May 4, 2024
1 parent 750743b commit 7f2a582
Show file tree
Hide file tree
Showing 3 changed files with 269 additions and 264 deletions.
264 changes: 0 additions & 264 deletions tests/test_errors.py

This file was deleted.

58 changes: 58 additions & 0 deletions tests/test_openapi_object_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest

from openapi_tester import SchemaTester
from openapi_tester.config import OpenAPITestConfig
from openapi_tester.exceptions import DocumentationError


def test_missing_response_key_error():
expected_error_message = (
'The following property was found in the schema definition, but is missing from the response data: "one"'
"\n\nReference:\n\nPOST /endpoint > response > one"
'\n\nResponse body:\n {\n "two": 2\n}'
'\nSchema section:\n {\n "one": {\n "type": "int"\n }\n}'
"\n\nHint: Remove the key from your OpenAPI docs, or include it in your API response"
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"required": ["one"], "properties": {"one": {"type": "int"}}},
{"two": 2},
OpenAPITestConfig(reference="POST /endpoint > response"),
)


def test_missing_schema_key_error():
expected_error_message = (
'The following property was found in the response data, but is missing from the schema definition: "two"'
"\n\nReference:"
"\n\nPOST /endpoint > response > two"
'\n\nResponse body:\n {\n "one": 1,\n "two": 2\n}'
'\n\nSchema section:\n {\n "one": {\n "type": "int"\n }\n}'
"\n\nHint: Remove the key from your API response, or include it in your OpenAPI docs"
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"required": ["one"], "properties": {"one": {"type": "int"}}},
{"one": 1, "two": 2},
OpenAPITestConfig(reference="POST /endpoint > response"),
)


def test_key_in_write_only_properties_error():
expected_error_message = (
'The following property was found in the response, but is documented as being "writeOnly": "one"'
"\n\nReference:"
"\n\nPOST /endpoint > response > one"
'\n\nResponse body:\n {\n "one": 1\n}'
'\nSchema section:\n {\n "one": {\n "type": "int",\n "writeOnly": true\n }\n}'
'\n\nHint: Remove the key from your API response, or remove the "WriteOnly" restriction'
)
tester = SchemaTester()
with pytest.raises(DocumentationError, match=expected_error_message):
tester.test_openapi_object(
{"properties": {"one": {"type": "int", "writeOnly": True}}},
{"one": 1},
OpenAPITestConfig(reference="POST /endpoint > response"),
)
Loading

0 comments on commit 7f2a582

Please sign in to comment.