Skip to content

Commit

Permalink
chore: Convert all FailureContext class attributes to instance attr…
Browse files Browse the repository at this point in the history
…ibutes

For simpler serialization via `attrs`
  • Loading branch information
Stranger6667 committed Jun 13, 2021
1 parent b6b3c59 commit 7ed2306
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Expand Up @@ -9,6 +9,7 @@ Changelog
- ``ExecutionEvent.asdict`` adds the ``event_type`` field which is the event class name.
- Add API schema to the ``Initialized`` event.
- **Internal**: Add ``SerializedCase.cookies``
- Convert all ``FailureContext`` class attributes to instance attributes. For simpler serialization via ``attrs``.

`3.9.0`_ - 2021-06-07
---------------------
Expand Down
51 changes: 31 additions & 20 deletions src/schemathesis/failures.py
Expand Up @@ -11,81 +11,89 @@ class FailureContext:
title: str
# A longer one
message: str
type: str


@attr.s(slots=True, repr=False)
class ValidationErrorContext(FailureContext):
"""Additional information about JSON Schema validation errors."""

title = "Non-conforming response payload"
message = "Response does not conform to the defined schema"
validation_message: str = attr.ib()
schema_path: List[Union[str, int]] = attr.ib()
schema: Union[Dict[str, Any], bool] = attr.ib()
instance_path: List[Union[str, int]] = attr.ib()
instance: Union[None, bool, float, str, list, Dict[str, Any]] = attr.ib()
title: str = attr.ib(default="Non-conforming response payload")
message: str = attr.ib(default="Response does not conform to the defined schema")
type: str = attr.ib(default="json_schema")


@attr.s(slots=True, repr=False)
class JSONDecodeErrorContext(FailureContext):
"""Failed to decode JSON."""

title = "JSON deserialization error"
message = "Response is not a valid JSON"
validation_message: str = attr.ib()
document: str = attr.ib()
position: int = attr.ib()
lineno: int = attr.ib()
colno: int = attr.ib()
title: str = attr.ib(default="JSON deserialization error")
message: str = attr.ib(default="Response is not a valid JSON")
type: str = attr.ib(default="json_decode")


@attr.s(slots=True, repr=False)
class ServerError(FailureContext):
title = "Internal server error"
message = "Server got itself in trouble"
status_code: int = attr.ib()
title: str = attr.ib(default="Internal server error")
message: str = attr.ib(default="Server got itself in trouble")
type: str = attr.ib(default="server_error")


@attr.s(slots=True, repr=False)
class MissingContentType(FailureContext):
"""Content type header is missing."""

title = "Missing Content-Type header"
message = "Response is missing the `Content-Type` header"
media_types: List[str] = attr.ib()
title: str = attr.ib(default="Missing Content-Type header")
message: str = attr.ib(default="Response is missing the `Content-Type` header")
type: str = attr.ib(default="missing_content_type")


@attr.s(slots=True, repr=False)
class UndefinedContentType(FailureContext):
"""Response has Content-Type that is not defined in the schema."""

title = "Undefined Content-Type"
message = "Response has `Content-Type` that is not declared in the schema"
content_type: str = attr.ib()
defined_content_types: List[str] = attr.ib()
title: str = attr.ib(default="Undefined Content-Type")
message: str = attr.ib(default="Response has `Content-Type` that is not declared in the schema")
type: str = attr.ib(default="undefined_content_type")


@attr.s(slots=True, repr=False)
class UndefinedStatusCode(FailureContext):
"""Response has a status code that is not defined in the schema."""

title = "Undefined status code"
message = "Response has a status code that is not declared in the schema"
# Response's status code
status_code: int = attr.ib()
# Status codes as defined in schema
defined_status_codes: List[str] = attr.ib()
# Defined status code with expanded wildcards
allowed_status_codes: List[int] = attr.ib()
title: str = attr.ib(default="Undefined status code")
message: str = attr.ib(default="Response has a status code that is not declared in the schema")
type: str = attr.ib(default="undefined_status_code")


@attr.s(slots=True, repr=False)
class MissingHeaders(FailureContext):
"""Some required headers are missing."""

title = "Missing required headers"
message = "Response is missing headers required by the schema"
missing_headers: List[str] = attr.ib()
title: str = attr.ib(default="Missing required headers")
message: str = attr.ib(default="Response is missing headers required by the schema")
type: str = attr.ib(default="missing_headers")


@attr.s(slots=True, repr=False)
Expand All @@ -95,26 +103,29 @@ class MalformedMediaType(FailureContext):
Example: `application-json` instead of `application/json`
"""

title = "Malformed media type name"
message = "Media type name is not valid"
actual: str = attr.ib()
defined: str = attr.ib()
title: str = attr.ib(default="Malformed media type name")
message: str = attr.ib(default="Media type name is not valid")
type: str = attr.ib(default="malformed_media_type")


@attr.s(slots=True, repr=False)
class ResponseTimeExceeded(FailureContext):
"""Response took longer than expected."""

title = "Response time exceeded"
message = "Response time exceeds the deadline"
elapsed: float = attr.ib()
deadline: int = attr.ib()
title: str = attr.ib(default="Response time exceeded")
message: str = attr.ib(default="Response time exceeds the deadline")
type: str = attr.ib(default="response_time_exceeded")


@attr.s(slots=True, repr=False)
class RequestTimeout(FailureContext):
"""Request took longer than timeout."""

title = "Request timeout"
message = "The request timed out"
timeout: int = attr.ib()
title: str = attr.ib(default="Request timeout")
message: str = attr.ib(default="The request timed out")
type: str = attr.ib(default="request_timeout")

0 comments on commit 7ed2306

Please sign in to comment.