From 7ed2306bd70481ca634914b2f82c1f2f8c038e83 Mon Sep 17 00:00:00 2001 From: Dmitry Dygalo Date: Sun, 13 Jun 2021 16:49:27 +0200 Subject: [PATCH] chore: Convert all `FailureContext` class attributes to instance attributes For simpler serialization via `attrs` --- docs/changelog.rst | 1 + src/schemathesis/failures.py | 51 ++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 3d03151321..8b704b5db5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 --------------------- diff --git a/src/schemathesis/failures.py b/src/schemathesis/failures.py index d0e5936898..0f03660695 100644 --- a/src/schemathesis/failures.py +++ b/src/schemathesis/failures.py @@ -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) @@ -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")