From 63910ee486874a33313e25d65e47d8b182de7308 Mon Sep 17 00:00:00 2001 From: Nicola Coretti Date: Wed, 23 Oct 2024 15:44:42 +0200 Subject: [PATCH] Apply the code formatter to the entire code base. --- doc/conf.py | 1 - exasol/error/__init__.py | 5 +- exasol/error/_error.py | 66 +++-- exasol/error/_parse.py | 55 ++-- exasol/error/_report.py | 11 +- exasol/error/version.py | 7 +- .../error_message_builder.py | 1 + exasol_error_reporting_python/exa_error.py | 3 +- .../parameters_mapper.py | 20 +- exasol_error_reporting_python/placeholder.py | 5 +- .../placeholder_handler.py | 23 +- noxconfig.py | 7 +- noxfile.py | 1 + test/unit/cli_test.py | 7 +- test/unit/error_test.py | 96 +++---- test/unit/named_parameters_test.py | 56 +++-- test/unit/regression_test.py | 19 +- tests/test_error_message_builder.py | 237 +++++++++++------- tests/test_exa_error.py | 2 +- 19 files changed, 371 insertions(+), 251 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 0cc0804..d3c4121 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -75,4 +75,3 @@ "github_url": "https://github.com/exasol/error-reporting-python", "accent_color": "grass", } - diff --git a/exasol/error/__init__.py b/exasol/error/__init__.py index b7afcb0..146e6a8 100644 --- a/exasol/error/__init__.py +++ b/exasol/error/__init__.py @@ -1,3 +1,6 @@ -from exasol.error._error import ExaError, Parameter +from exasol.error._error import ( + ExaError, + Parameter, +) __all__ = ["ExaError", "Parameter"] diff --git a/exasol/error/_error.py b/exasol/error/_error.py index 7d4a603..456d24e 100644 --- a/exasol/error/_error.py +++ b/exasol/error/_error.py @@ -1,8 +1,14 @@ import warnings from dataclasses import dataclass from inspect import cleandoc -from typing import Dict, Iterable, List, Mapping, Union from pathlib import Path +from typing import ( + Dict, + Iterable, + List, + Mapping, + Union, +) with warnings.catch_warnings(): warnings.simplefilter("ignore") @@ -18,11 +24,11 @@ class Parameter: class Error: def __init__( - self, - code: str, - message: str, - mitigations: Union[str, Iterable[str]], - parameters: Dict[str, Union[str, Parameter]], + self, + code: str, + message: str, + mitigations: Union[str, Iterable[str]], + parameters: Dict[str, Union[str, Parameter]], ): # This function maybe flattened into or moved out of the constructor in the future. def build_error(code, msg, mitigations, params): @@ -62,11 +68,11 @@ def __str__(self) -> str: "messagePlaceholders": [ { "placeholder": "code", - "description": "Error code which was causing the error." + "description": "Error code which was causing the error.", } ], "mitigations": ["Ensure you follow the standard error code format."], - "sourceFile": Path(__file__).name + "sourceFile": Path(__file__).name, }, "E-ERP-2": { "identifier": "E-ERP-2", @@ -74,26 +80,30 @@ def __str__(self) -> str: "messagePlaceholders": [ { "placeholder": "traceback", - "description": "Exception traceback which lead to the generation of this error." + "description": "Exception traceback which lead to the generation of this error.", } ], "description": "An unexpected error occurred during the creation of the error", - "mitigations": [cleandoc(""" + "mitigations": [ + cleandoc( + """ A good starting point would be to investigate the cause of the attached exception. Trackback: {{traceback}} - """)], - "sourceFile": Path(__file__).name + """ + ) + ], + "sourceFile": Path(__file__).name, }, } def ExaError( - code: str, - message: str, - mitigations: Union[str, List[str]], - parameters: Mapping[str, Union[str, Parameter]], + code: str, + message: str, + mitigations: Union[str, List[str]], + parameters: Mapping[str, Union[str, Parameter]], ) -> Error: """Create a new ExaError. @@ -112,36 +122,38 @@ def ExaError( try: return Error(code, message, mitigations, parameters) except InvalidErrorCode: - error_code = 'E-ERP-1' + error_code = "E-ERP-1" error_details = LIBRARY_ERRORS[error_code] return Error( - code=error_details['identifier'], - message=error_details['message'], - mitigations=error_details['mitigations'], - parameters={"code": code} + code=error_details["identifier"], + message=error_details["message"], + mitigations=error_details["mitigations"], + parameters={"code": code}, ) except Exception as ex: import traceback + tb = traceback.format_exc() parameters = {"traceback": tb} - error_code = 'E-ERP-2' + error_code = "E-ERP-2" error_details = LIBRARY_ERRORS[error_code] return Error( - code=error_details['identifier'], - message=error_details['message'], - mitigations=error_details['mitigations'], - parameters=parameters + code=error_details["identifier"], + message=error_details["message"], + mitigations=error_details["mitigations"], + parameters=parameters, ) def _create_error_code_definitions(version=None): from exasol.error.version import VERSION + version = version or VERSION return { "$schema": "https://schemas.exasol.com/error_code_report-1.0.0.json", "projectName": "exasol-error-reporting", "projectVersion": version, - "errorCodes": [code for code in LIBRARY_ERRORS.values()] + "errorCodes": [code for code in LIBRARY_ERRORS.values()], } diff --git a/exasol/error/_parse.py b/exasol/error/_parse.py index a4cc14e..e357599 100644 --- a/exasol/error/_parse.py +++ b/exasol/error/_parse.py @@ -3,9 +3,19 @@ from contextlib import ExitStack from dataclasses import dataclass from pathlib import Path -from typing import Generator, Iterable, List, Optional, Tuple, Union +from typing import ( + Generator, + Iterable, + List, + Optional, + Tuple, + Union, +) -from exasol.error._report import ErrorCodeDetails, Placeholder +from exasol.error._report import ( + ErrorCodeDetails, + Placeholder, +) class _ExaErrorNodeWalker: @@ -39,7 +49,12 @@ def _extract_parameters(node: ast.Call): for keyword_argument in node.keywords: kwargs[keyword_argument.arg] = keyword_argument.value - return kwargs["code"], kwargs["message"], kwargs["mitigations"], kwargs["parameters"] + return ( + kwargs["code"], + kwargs["message"], + kwargs["mitigations"], + kwargs["parameters"], + ) class Validator: @@ -69,7 +84,7 @@ def warnings(self) -> Iterable["Validator.Warning"]: return self._warnings def validate( - self, node: ast.Call, file: str + self, node: ast.Call, file: str ) -> Tuple[List["Validator.Error"], List["Validator.Warning"]]: code: ast.Constant message: ast.Constant @@ -110,8 +125,7 @@ def _validate_message(self, node: ast.Constant, file: str): if not isinstance(node, ast.Constant): self._errors.append( self.Error( - message=self._error_msg.format( - type="message", value=type(node)), + message=self._error_msg.format(type="message", value=type(node)), file=file, line_number=node.lineno, ) @@ -150,8 +164,7 @@ def _validate_parameters(self, node: ast.Dict, file: str): if not isinstance(key, ast.Constant): self._errors.append( self.Error( - message=self._error_msg.format( - type="key", value=type(key)), + message=self._error_msg.format(type="key", value=type(key)), file=file, line_number=key.lineno, ) @@ -215,12 +228,14 @@ def normalize(params): internalDescription=None, potentialCauses=None, mitigations=( - [m.value for m in mitigations.elts] - if isinstance(mitigations, ast.List) - else [mitigations.value] - ) - if not isinstance(mitigations, str) - else [mitigations], + ( + [m.value for m in mitigations.elts] + if isinstance(mitigations, ast.List) + else [mitigations.value] + ) + if not isinstance(mitigations, str) + else [mitigations] + ), sourceFile=self._filename, sourceLine=node.lineno, contextHash=None, @@ -236,21 +251,15 @@ def collect(self) -> None: self._error_definitions.append(error_definition) -def parse_file( - file: Union[str, Path, io.FileIO] -) -> Tuple[ +def parse_file(file: Union[str, Path, io.FileIO]) -> Tuple[ Iterable[ErrorCodeDetails], Iterable["Validator.Warning"], Iterable["Validator.Error"], ]: with ExitStack() as stack: - f = ( - file - if isinstance(file, io.TextIOBase) - else stack.enter_context(open(file, "r")) - ) + f = file if isinstance(file, io.TextIOBase) else stack.enter_context(open(file)) root_node = ast.parse(f.read()) - name = f.name if hasattr(f, 'name') else f'<{f.__class__.__name__}>' + name = f.name if hasattr(f, "name") else f"<{f.__class__.__name__}>" collector = ErrorCollector(root_node, name) collector.collect() diff --git a/exasol/error/_report.py b/exasol/error/_report.py index 0132d2d..b4aad6f 100644 --- a/exasol/error/_report.py +++ b/exasol/error/_report.py @@ -1,6 +1,13 @@ import json -from dataclasses import asdict, dataclass, is_dataclass -from typing import List, Optional +from dataclasses import ( + asdict, + dataclass, + is_dataclass, +) +from typing import ( + List, + Optional, +) class JsonEncoder(json.JSONEncoder): diff --git a/exasol/error/version.py b/exasol/error/version.py index b922350..191e772 100644 --- a/exasol/error/version.py +++ b/exasol/error/version.py @@ -1,5 +1,10 @@ +# ATTENTION: +# This file is generated by exasol/toolbox/pre_commit_hooks/package_version.py when using: +# * either "poetry run nox -s fix" +# * or "poetry run version-check --fix" +# Do not edit this file manually! +# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`. MAJOR = 0 MINOR = 4 PATCH = 0 - VERSION = f"{MAJOR}.{MINOR}.{PATCH}" diff --git a/exasol_error_reporting_python/error_message_builder.py b/exasol_error_reporting_python/error_message_builder.py index cf368a8..b0e230b 100644 --- a/exasol_error_reporting_python/error_message_builder.py +++ b/exasol_error_reporting_python/error_message_builder.py @@ -1,5 +1,6 @@ import re from typing import Any + from exasol_error_reporting_python.parameters_mapper import ParametersMapper from exasol_error_reporting_python.placeholder_handler import PlaceholderHandler diff --git a/exasol_error_reporting_python/exa_error.py b/exasol_error_reporting_python/exa_error.py index 806f66d..2d9e26d 100644 --- a/exasol_error_reporting_python/exa_error.py +++ b/exasol_error_reporting_python/exa_error.py @@ -1,5 +1,4 @@ -from exasol_error_reporting_python.error_message_builder import \ - ErrorMessageBuilder +from exasol_error_reporting_python.error_message_builder import ErrorMessageBuilder class ExaError: diff --git a/exasol_error_reporting_python/parameters_mapper.py b/exasol_error_reporting_python/parameters_mapper.py index 9242f87..a0aa334 100644 --- a/exasol_error_reporting_python/parameters_mapper.py +++ b/exasol_error_reporting_python/parameters_mapper.py @@ -1,6 +1,6 @@ from typing import Any -from exasol_error_reporting_python.placeholder_handler\ - import PlaceholderHandler + +from exasol_error_reporting_python.placeholder_handler import PlaceholderHandler class ParametersMapper: @@ -8,6 +8,7 @@ class ParametersMapper: This class is responsible for mapping positional arguments to corresponding placeholders in messages. """ + def __init__(self, text: str, arguments: str): self._text = text self._parameters = arguments @@ -38,8 +39,7 @@ def _map_parameters(self): for placeholder in PlaceholderHandler.get_placeholders(self._text): if self._is_parameter_present(): - self._parameter_dict[placeholder.name] = \ - self._get_current_parameter() + self._parameter_dict[placeholder.name] = self._get_current_parameter() self._next_parameters() def _get_current_parameter(self) -> Any: @@ -48,8 +48,11 @@ def _get_current_parameter(self) -> Any: :return: a parameter in the parameters list or None """ - return self._parameters[self._parameter_idx] \ - if self._is_parameter_present() else None + return ( + self._parameters[self._parameter_idx] + if self._is_parameter_present() + else None + ) def _is_parameter_present(self) -> bool: """ @@ -57,8 +60,7 @@ def _is_parameter_present(self) -> bool: :return: True if present, False otherwise """ - return self._parameters and \ - self._parameter_idx < len(self._parameters) + return self._parameters and self._parameter_idx < len(self._parameters) def _next_parameters(self): """ @@ -66,5 +68,3 @@ def _next_parameters(self): """ self._parameter_idx += 1 - - diff --git a/exasol_error_reporting_python/placeholder.py b/exasol_error_reporting_python/placeholder.py index 29c47d1..40ab156 100644 --- a/exasol_error_reporting_python/placeholder.py +++ b/exasol_error_reporting_python/placeholder.py @@ -1,4 +1,3 @@ - class Placeholder: """ A data class holds information about Placeholder. @@ -35,6 +34,4 @@ def _parse_unquoted_placeholder_name(self) -> str: :return: the text in which the unquotation suffix is removed """ - return self.text[:-len(self._unquoted_suffix)] - - + return self.text[: -len(self._unquoted_suffix)] diff --git a/exasol_error_reporting_python/placeholder_handler.py b/exasol_error_reporting_python/placeholder_handler.py index 0772bfe..bdaed7e 100644 --- a/exasol_error_reporting_python/placeholder_handler.py +++ b/exasol_error_reporting_python/placeholder_handler.py @@ -1,5 +1,9 @@ import re -from typing import Any, Iterable +from typing import ( + Any, + Iterable, +) + from exasol_error_reporting_python.placeholder import Placeholder @@ -24,9 +28,7 @@ def get_placeholders(cls, text: str) -> Iterable[Placeholder]: """ for match in re.finditer(cls.PLACEHOLDER_PATTERN, text): - yield Placeholder( - key=match.group(0), - text=match.group(1)) + yield Placeholder(key=match.group(0), text=match.group(1)) @classmethod def replace_placeholder(cls, text: str, parameter_dict: dict) -> str: @@ -41,14 +43,14 @@ def replace_placeholder(cls, text: str, parameter_dict: dict) -> str: """ for placeholder in cls.get_placeholders(text): - replacement = cls.get_parameter_from_dict( - placeholder, parameter_dict) + replacement = cls.get_parameter_from_dict(placeholder, parameter_dict) text = text.replace(placeholder.key, replacement) return text @classmethod - def get_parameter_from_dict(cls, placeholder: Placeholder, - parameter_dict: dict) -> Any: + def get_parameter_from_dict( + cls, placeholder: Placeholder, parameter_dict: dict + ) -> Any: """ Return parameter for the given placeholder, if any, considering whether the parameter will be quoted or not. Otherwise, returns . @@ -66,7 +68,7 @@ def get_parameter_from_dict(cls, placeholder: Placeholder, if cls.is_unquoted_param(placeholder, parameter): return str(parameter) else: - return "'{}'".format(parameter) + return f"'{parameter}'" @classmethod def is_unquoted_param(cls, placeholder: Placeholder, parameter: Any): @@ -78,5 +80,4 @@ def is_unquoted_param(cls, placeholder: Placeholder, parameter: Any): :return: True if the given parameter will not be quoted. """ - return placeholder.is_unquoted() \ - or not isinstance(parameter, str) + return placeholder.is_unquoted() or not isinstance(parameter, str) diff --git a/noxconfig.py b/noxconfig.py index 04e8140..113d6f9 100644 --- a/noxconfig.py +++ b/noxconfig.py @@ -1,4 +1,5 @@ """Configuration for nox based task runner""" + from __future__ import annotations from dataclasses import dataclass @@ -14,9 +15,11 @@ class UpdateErrorCodes: @hookimpl def prepare_release_update_version(self, session, config, version): import json + from exasol.error._error import _create_error_code_definitions - definitions = _create_error_code_definitions(f'{version}') - with open(UpdateErrorCodes.ERROR_CODES, 'w') as f: + + definitions = _create_error_code_definitions(f"{version}") + with open(UpdateErrorCodes.ERROR_CODES, "w") as f: json.dump(definitions, f) @hookimpl diff --git a/noxfile.py b/noxfile.py index 41d2ca8..f367b3c 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,4 +1,5 @@ """defines nox tasks/targets for this project""" + import nox # imports all nox task provided by the toolbox diff --git a/test/unit/cli_test.py b/test/unit/cli_test.py index 61e8307..3f83c18 100644 --- a/test/unit/cli_test.py +++ b/test/unit/cli_test.py @@ -4,7 +4,12 @@ import pytest -from exasol.error._parse import ErrorCodeDetails, ErrorCollector, Placeholder, Validator +from exasol.error._parse import ( + ErrorCodeDetails, + ErrorCollector, + Placeholder, + Validator, +) AST_NAME_CLASS = "ast.Name" if sys.version_info.minor > 8 else "_ast.Name" diff --git a/test/unit/error_test.py b/test/unit/error_test.py index 7ae28b8..b4e0f6d 100644 --- a/test/unit/error_test.py +++ b/test/unit/error_test.py @@ -5,7 +5,10 @@ import pytest -from exasol.error import ExaError, Parameter +from exasol.error import ( + ExaError, + Parameter, +) Data = namedtuple("Data", ["code", "message", "mitigations", "parameters"]) @@ -22,51 +25,51 @@ def does_not_raise(): "expected,data", [ ( - cleandoc( - """ + cleandoc( + """ E-TEST-1: Not enough space on device '/dev/sda1'. Known mitigations: * Delete something from '/dev/sda1'. * Create larger partition. """ - ), - Data( - code="E-TEST-1", - message="Not enough space on device {{device}}.", - mitigations=[ - "Delete something from {{device}}.", - "Create larger partition.", - ], - parameters={"device": Parameter("/dev/sda1", "name of the device")}, - ), + ), + Data( + code="E-TEST-1", + message="Not enough space on device {{device}}.", + mitigations=[ + "Delete something from {{device}}.", + "Create larger partition.", + ], + parameters={"device": Parameter("/dev/sda1", "name of the device")}, + ), ), ( - cleandoc( - """ + cleandoc( + """ E-TEST-1: Not enough space on device '/dev/sda1'. Known mitigations: * Delete something from '/dev/sda1'. * Create larger partition. """ - ), - Data( - code="E-TEST-1", - message="Not enough space on device {{device}}.", - mitigations=[ - "Delete something from {{device}}.", - "Create larger partition.", - ], - parameters={"device": "/dev/sda1"}, - ), + ), + Data( + code="E-TEST-1", + message="Not enough space on device {{device}}.", + mitigations=[ + "Delete something from {{device}}.", + "Create larger partition.", + ], + parameters={"device": "/dev/sda1"}, + ), ), ( - cleandoc( - "E-ERP-1: Invalid error code 'WRONGCODE'. Ensure you follow the standard error code format." - ), - Data( - code="WRONGCODE", - message="some error message", - mitigations=["unrecoverable ;P"], - parameters={}, - ), + cleandoc( + "E-ERP-1: Invalid error code 'WRONGCODE'. Ensure you follow the standard error code format." + ), + Data( + code="WRONGCODE", + message="some error message", + mitigations=["unrecoverable ;P"], + parameters={}, + ), ), ], ) @@ -80,15 +83,15 @@ def test_exa_error_as_string(expected, data): "data", [ ( - Data( - code="BROKEN_ERROR_CODE", - message='"Not enough space on device {{device}}."', - mitigations=[ - "Delete something from {{device}}.", - "Create larger partition.", - ], - parameters={"device": Parameter("/dev/sda1", "name of the device")}, - ) + Data( + code="BROKEN_ERROR_CODE", + message='"Not enough space on device {{device}}."', + mitigations=[ + "Delete something from {{device}}.", + "Create larger partition.", + ], + parameters={"device": Parameter("/dev/sda1", "name of the device")}, + ) ), ], ) @@ -110,6 +113,7 @@ def test_exa_error_does_not_throw_error_on_invalid(data): ) def test_raising_message_builder(data): from unittest.mock import patch + from exasol_error_reporting_python.exa_error import ExaError as Error actual_impl = Error.message_builder @@ -123,11 +127,13 @@ def builder(error_code): mock.message_builder = builder error = ExaError(data.code, data.message, data.mitigations, data.parameters) actual = str(error) - expected = cleandoc(""" + expected = cleandoc( + """ E-ERP-2: Unknown error/exception occurred. A good starting point would be to investigate the cause of the attached exception. Trackback: - """) + """ + ) assert expected in actual diff --git a/test/unit/named_parameters_test.py b/test/unit/named_parameters_test.py index 0f39f4d..e1d3a9d 100644 --- a/test/unit/named_parameters_test.py +++ b/test/unit/named_parameters_test.py @@ -1,7 +1,8 @@ import io -import pytest from inspect import cleandoc +import pytest + from exasol.error._parse import parse_file from exasol.error._report import ErrorCodeDetails @@ -11,25 +12,27 @@ def expected(): yield ( [ ErrorCodeDetails( - identifier='E-TEST-1', - message='this is an error', + identifier="E-TEST-1", + message="this is an error", messagePlaceholders=[], description=None, internalDescription=None, potentialCauses=None, - mitigations=['no mitigation available'], - sourceFile='', + mitigations=["no mitigation available"], + sourceFile="", sourceLine=6, - contextHash=None), + contextHash=None, + ), ], [], - [] + [], ) def test_use_only_named_parameters(expected): - file = io.StringIO(initial_value=cleandoc( - """ + file = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -42,7 +45,8 @@ def test_use_only_named_parameters(expected): parameters={}, ) """ - )) + ) + ) expected_defs, expected_warnings, expected_errors = expected definitions, warnings, errors = parse_file(file) @@ -53,8 +57,9 @@ def test_use_only_named_parameters(expected): def test_dont_name_the_code_parameter(expected): - file = io.StringIO(initial_value=cleandoc( - """ + file = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -67,7 +72,8 @@ def test_dont_name_the_code_parameter(expected): parameters={}, ) """ - )) + ) + ) expected_defs, expected_warnings, expected_errors = expected definitions, warnings, errors = parse_file(file) @@ -78,8 +84,9 @@ def test_dont_name_the_code_parameter(expected): def test_dont_name_the_code_and_message_parameter(expected): - file = io.StringIO(initial_value=cleandoc( - """ + file = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -92,7 +99,8 @@ def test_dont_name_the_code_and_message_parameter(expected): parameters={}, ) """ - )) + ) + ) expected_defs, expected_warnings, expected_errors = expected definitions, warnings, errors = parse_file(file) @@ -103,8 +111,9 @@ def test_dont_name_the_code_and_message_parameter(expected): def test_dont_name_the_code_message_and_mitigations_parameter(expected): - file = io.StringIO(initial_value=cleandoc( - """ + file = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -117,7 +126,8 @@ def test_dont_name_the_code_message_and_mitigations_parameter(expected): parameters={}, ) """ - )) + ) + ) expected_defs, expected_warnings, expected_errors = expected definitions, warnings, errors = parse_file(file) @@ -128,8 +138,9 @@ def test_dont_name_the_code_message_and_mitigations_parameter(expected): def test_dont_name_any_parameter(expected): - file = io.StringIO(initial_value=cleandoc( - """ + file = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -142,7 +153,8 @@ def test_dont_name_any_parameter(expected): {}, ) """ - )) + ) + ) expected_defs, expected_warnings, expected_errors = expected definitions, warnings, errors = parse_file(file) diff --git a/test/unit/regression_test.py b/test/unit/regression_test.py index cd69f92..81317fe 100644 --- a/test/unit/regression_test.py +++ b/test/unit/regression_test.py @@ -7,8 +7,9 @@ # regression test for GitHub Issue #26 def test_single_mitigation(): - f = io.StringIO(initial_value=cleandoc( - """ + f = io.StringIO( + initial_value=cleandoc( + """ from exasol import error from exasol.error import Parameter @@ -21,20 +22,22 @@ def test_single_mitigation(): {}, ) """ - )) + ) + ) expected_definitions = [ ErrorCodeDetails( - identifier='E-TEST-1', - message='this is an error', + identifier="E-TEST-1", + message="this is an error", messagePlaceholders=[], description=None, internalDescription=None, potentialCauses=None, - mitigations=['no mitigation available'], - sourceFile='', + mitigations=["no mitigation available"], + sourceFile="", sourceLine=6, - contextHash=None), + contextHash=None, + ), ] expected_warnings = [] expected_errors = [] diff --git a/tests/test_error_message_builder.py b/tests/test_error_message_builder.py index 630fc55..65deddd 100644 --- a/tests/test_error_message_builder.py +++ b/tests/test_error_message_builder.py @@ -1,196 +1,253 @@ -from exasol_error_reporting_python.error_message_builder import \ - ErrorMessageBuilder +from exasol_error_reporting_python.error_message_builder import ErrorMessageBuilder def test_message(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Test message")) + message = str(ErrorMessageBuilder("E-ERJ-TEST-1").message("Test message")) assert message == "E-ERJ-TEST-1: Test message" def test_message_with_parameter(): - message = str(ErrorMessageBuilder('E-ERJ-TEST-1') - .message("Test message {{my_placeholder}} " - "and a number {{number}}.") - .parameter("my_placeholder", "my_value") - .parameter("number", 1, "a number")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Test message {{my_placeholder}} " "and a number {{number}}.") + .parameter("my_placeholder", "my_value") + .parameter("number", 1, "a number") + ) assert message == "E-ERJ-TEST-1: Test message 'my_value' and a number 1." def test_message_with_none_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("{{my_placeholder}}") - .parameter("my_placeholder", None)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("{{my_placeholder}}") + .parameter("my_placeholder", None) + ) assert message == "E-ERJ-TEST-1: " def test_message_without_parameter_name(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("test {{}}")) # TODO: different imp. + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message("test {{}}") + ) # TODO: different imp. assert message == "E-ERJ-TEST-1: test " def test_message_without_parameter_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("test {{my_placeholder}}")) # TODO: different imp. + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message("test {{my_placeholder}}") + ) # TODO: different imp. assert message == "E-ERJ-TEST-1: test " def test_message_with_group_reference_char(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("test {{my_placeholder}}") - .parameter("my_placeholder", "$2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("test {{my_placeholder}}") + .parameter("my_placeholder", "$2") + ) assert message == "E-ERJ-TEST-1: test '$2'" def test_single_mitigation(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Fix it.")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Fix it.") + ) assert message == "E-ERJ-TEST-1: Something went wrong. Fix it." def test_single_mitigation_with_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete line {{LINE_NR}}.") - .parameter("LINE_NR", 1)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete line {{LINE_NR}}.") + .parameter("LINE_NR", 1) + ) assert message == "E-ERJ-TEST-1: Something went wrong. Delete line 1." def test_mitigations(): support_hotline = "1234/56789" - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Fix it.") - .mitigation("Contact support under {{SUPPORT_HOTLINE}}.") - .parameter("SUPPORT_HOTLINE", support_hotline)) - assert message == "E-ERJ-TEST-1: Something went wrong. Known mitigations:" \ - "\n* Fix it.\n* Contact support under '1234/56789'." + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Fix it.") + .mitigation("Contact support under {{SUPPORT_HOTLINE}}.") + .parameter("SUPPORT_HOTLINE", support_hotline) + ) + assert ( + message == "E-ERJ-TEST-1: Something went wrong. Known mitigations:" + "\n* Fix it.\n* Contact support under '1234/56789'." + ) def test_ticket_mitigation(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .ticket_mitigation()) - assert message == "E-ERJ-TEST-1: Something went wrong. " \ - "This is an internal error that should not happen. " \ - "Please report it by opening a GitHub issue." + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .ticket_mitigation() + ) + assert ( + message == "E-ERJ-TEST-1: Something went wrong. " + "This is an internal error that should not happen. " + "Please report it by opening a GitHub issue." + ) def test_mitigation_inline_single_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete line {{LINE_NR}}.", 1)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete line {{LINE_NR}}.", 1) + ) assert message == "E-ERJ-TEST-1: Something went wrong. Delete line 1." def test_mitigation_inline_multiple_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete lines {{LINE_NR1}}, {{LINE_NR2}}", 1, 2)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete lines {{LINE_NR1}}, {{LINE_NR2}}", 1, 2) + ) assert message == "E-ERJ-TEST-1: Something went wrong. Delete lines 1, 2" def test_mitigation_inline_parameter_without_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete line {{LINE_NR}}.")) # TODO: different imp. + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete line {{LINE_NR}}.") + ) # TODO: different imp. assert message == "E-ERJ-TEST-1: Something went wrong. Delete line ." def test_mitigation_inline_single_null_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete line {{LINE_NR}}.", None)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete line {{LINE_NR}}.", None) + ) assert message == "E-ERJ-TEST-1: Something went wrong. Delete line ." def test_mitigation_inline_multiple_null_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Something went wrong.") - .mitigation("Delete lines {{LINE1}}, {{LINE2}}.", None, None)) - assert message == "E-ERJ-TEST-1: Something went wrong. " \ - "Delete lines , ." + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Something went wrong.") + .mitigation("Delete lines {{LINE1}}, {{LINE2}}.", None, None) + ) + assert ( + message == "E-ERJ-TEST-1: Something went wrong. " "Delete lines , ." + ) def test_message_inline_single_unquoted_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name|uq}}.", "value")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name|uq}}.", "value" + ) + ) assert message == "E-ERJ-TEST-1: Message with value." def test_message_inline_single_quoted_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name}}.", "value")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name}}.", "value" + ) + ) assert message == "E-ERJ-TEST-1: Message with 'value'." def test_message_inline_multiple_unquoted_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name1|uq}} and " - "{{parameter_name2|uq}}.", "value1", "value2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name1|uq}} and " "{{parameter_name2|uq}}.", + "value1", + "value2", + ) + ) assert message == "E-ERJ-TEST-1: Message with value1 and value2." def test_message_inline_multiple_quoted_parameter(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name1}} and " - "{{parameter_name2}}.", "value1", "value2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name1}} and " "{{parameter_name2}}.", + "value1", + "value2", + ) + ) assert message == "E-ERJ-TEST-1: Message with 'value1' and 'value2'." def test_message_inline_unquoted_parameter_without_name(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{|uq}}.", "value")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message("Message with {{|uq}}.", "value") + ) assert message == "E-ERJ-TEST-1: Message with value." def test_message_inline_unquoted_parameter_without_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name|uq}}.")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name|uq}}." + ) + ) assert message == "E-ERJ-TEST-1: Message with ." def test_message_inline_single_unquoted_parameter_none_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name|uq}}.", None)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name|uq}}.", None + ) + ) assert message == "E-ERJ-TEST-1: Message with ." def test_message_inline_multiple_unquoted_parameter_none_value(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name|uq}} and " - "{{parameter_name2}}.", None, None)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1").message( + "Message with {{parameter_name|uq}} and " "{{parameter_name2}}.", None, None + ) + ) assert message == "E-ERJ-TEST-1: Message with and ." def test_message_inline_multiple_unquoted_parameter_none_parameters(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name|uq}} and " - "{{parameter_name2}}.") - .parameter("parameter_name", None) - .parameter("parameter_name2", None)) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Message with {{parameter_name|uq}} and " "{{parameter_name2}}.") + .parameter("parameter_name", None) + .parameter("parameter_name2", None) + ) assert message == "E-ERJ-TEST-1: Message with and ." def test_message_inline_unquoted_parameter_with_group_reference_char(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("test {{my_placeholder|uq}}") - .parameter("my_placeholder", "$2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("test {{my_placeholder|uq}}") + .parameter("my_placeholder", "$2") + ) assert message == "E-ERJ-TEST-1: test $2" def test_message_inline_and_outline_order(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name1}}", "value1") - .message(" {{parameter_name2}}.", "value2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Message with {{parameter_name1}}", "value1") + .message(" {{parameter_name2}}.", "value2") + ) assert message == "E-ERJ-TEST-1: Message with 'value1' 'value2'." def test_message_inline_and_outline_unquoted_parameters(): - message = str(ErrorMessageBuilder("E-ERJ-TEST-1") - .message("Message with {{parameter_name1|uq}}", "value1") - .message(" {{parameter_name2|uq}}.", "value2")) + message = str( + ErrorMessageBuilder("E-ERJ-TEST-1") + .message("Message with {{parameter_name1|uq}}", "value1") + .message(" {{parameter_name2|uq}}.", "value2") + ) assert message == "E-ERJ-TEST-1: Message with value1 value2." - diff --git a/tests/test_exa_error.py b/tests/test_exa_error.py index eb124c9..1a01a95 100644 --- a/tests/test_exa_error.py +++ b/tests/test_exa_error.py @@ -2,4 +2,4 @@ def test_message_builder(): - assert str(ExaError.message_builder("E-ERJ-TEST-1")) == "E-ERJ-TEST-1" \ No newline at end of file + assert str(ExaError.message_builder("E-ERJ-TEST-1")) == "E-ERJ-TEST-1"