Ejemplo n.º 1
0
def parse_params(
        args: argparse.Namespace) -> Tuple[Optional[Params], List[str]]:
    """
    Try to parse the parameters of the command "test".

    Return (parsed parameters, errors if any).
    """
    errors = []  # type: List[str]

    path = pathlib.Path(args.path)

    settings = collections.OrderedDict()  # type: MutableMapping[str, Any]

    settings_parsing = None  # type: Optional[SettingsParsing]

    if args.settings is not None:
        for i, statement in enumerate(args.settings):
            mtch = _SETTING_STATEMENT_RE.match(statement)
            if not mtch:
                errors.append(
                    "Invalid setting statement {}. Expected statement to match {}, "
                    "but got: {}".format(i + 1, _SETTING_STATEMENT_RE.pattern,
                                         statement))

                return None, errors

            identifier = mtch.group("identifier")
            value_str = mtch.group("value")

            try:
                value = json.loads(value_str)
            except json.decoder.JSONDecodeError as error:
                errors.append(
                    "Failed to parse the value of the setting {}: {}".format(
                        identifier, error))
                return None, errors

            settings[identifier] = value

        settings_parsing, settings_errors = _parse_hypothesis_settings(
            settings=settings)
        if settings_errors:
            errors.extend(settings_errors)
        else:
            assert settings_parsing is not None

    if errors:
        return None, errors

    return (
        Params(path=path,
               settings_parsing=settings_parsing,
               do_inspect=args.inspect),
        errors,
    )
Ejemplo n.º 2
0
def _inspect_test_function_point(
        point: _general.FunctionPoint,
        settings_parsing: Optional[SettingsParsing]) -> Tuple[str, List[str]]:
    """
    Inspect what test will executed to test a single function point.

    Return (inspection, errors if any).
    """
    errors = []  # type: List[str]

    func = point.func  # Optimize the look-up

    try:
        strategy = icontract_hypothesis.infer_strategy(func=func)
    except Exception as error:
        errors.append(
            ("Inference of the search strategy failed for the function: {}. "
             "The error was: {}").format(func, error))
        return "", errors

    parts = [
        textwrap.dedent("""\
        hypothesis.given(
        {}
        )""").format(textwrap.indent(str(strategy), "    "))
    ]

    if settings_parsing and len(settings_parsing.parsed_kwargs) > 0:
        if len(settings_parsing.parsed_kwargs) == 1:
            identifier, value = list(settings_parsing.parsed_kwargs.items())[0]

            settings_str = f"hypothesis.settings({identifier}={value})"
        else:
            lines = ["hypothesis.settings("]
            items = list(settings_parsing.parsed_kwargs.items())
            for i, (identifier, value) in enumerate(items):
                if i < len(items) - 1:
                    lines.append(f"    {identifier}={value},")
                else:
                    lines.append(f"    {identifier}={value}")
            lines.append(")")

            settings_str = "\n".join(lines)

        parts.append(settings_str)

    return "\n".join(parts), []
Ejemplo n.º 3
0
def _test_function_point(
        point: _general.FunctionPoint,
        hypothesis_settings: Optional[hypothesis.settings]) -> List[str]:
    """
    Test a single function point.

    Return errors if any.
    """
    errors = []  # type: List[str]

    func = point.func  # Optimize the look-up

    try:
        strategy = icontract_hypothesis.infer_strategy(func=func)
    except AssertionError:
        raise
    except Exception as error:
        error_as_str = str(error)
        if error_as_str == "":
            error_as_str = str(type(error))

        errors.append(
            f"Inference of the search strategy failed for the function: {func}. "
            f"The error was: {error_as_str}")
        return errors

    def execute(kwargs: Dict[str, Any]) -> None:
        func(**kwargs)

    wrapped = hypothesis.given(strategy)(execute)
    if hypothesis_settings:
        wrapped = hypothesis_settings(wrapped)

    try:
        wrapped()
    except hypothesis.errors.FailedHealthCheck as err:
        return [
            f"Failed to test the function: "
            f"{func.__name__} from {inspect.getfile(func)} "
            f"due to the failed Hypothesis health check: {err}"
        ]

    return []
Ejemplo n.º 4
0
def _parse_hypothesis_settings(
    settings: Mapping[str,
                      Any]) -> Tuple[Optional[SettingsParsing], List[str]]:
    """
    Try to parse the setting as Hypothesis settings.

    Return (settings, errors if any).
    """
    errors = []

    kwargs = collections.OrderedDict()  # type: MutableMapping[str, Any]

    for identifier, value in settings.items():
        if identifier not in _HYPOTHESIS_SETTINGS_PARAMETERS:
            errors.append(f"Invalid Hypothesis setting: {identifier!r}")
        else:
            if identifier == "verbosity":
                if value not in _HYPOTHESIS_VERBOSITY_MAP:
                    errors.append(
                        f"Invalid Hypothesis setting {identifier!r}: {value!r}"
                    )
                else:
                    kwargs[identifier] = _HYPOTHESIS_VERBOSITY_MAP[value]

            elif identifier == "phase":
                if not isinstance(value, list):
                    errors.append(
                        f"Invalid Hypothesis setting {identifier!r}: "
                        f"expected a list, but got {value!r}")
                else:
                    parsed_items = []  # type: List[Any]
                    for item in value:
                        if item not in _HYPOTHESIS_PHASE_MAP:
                            errors.append(
                                f"Invalid {hypothesis.Phase.__name__} in the Hypothesis setting "
                                f"{identifier!r}: {item!r}")
                        else:
                            parsed_items.append(_HYPOTHESIS_PHASE_MAP[item])
                    kwargs[identifier] = parsed_items

            elif identifier == "suppress_health_check":
                if not isinstance(value, list):
                    errors.append(
                        f"Invalid Hypothesis setting {identifier!r}: "
                        f"expected a list, but got {value!r}")
                else:
                    parsed_items = []
                    for item in value:
                        if item not in _HYPOTHESIS_HEALTH_CHECK_MAP:
                            errors.append(
                                f"Invalid {hypothesis.HealthCheck.__name__} in the setting "
                                f"{identifier!r}: {item!r}")
                        else:
                            parsed_items.append(
                                _HYPOTHESIS_HEALTH_CHECK_MAP[item])
                    kwargs[identifier] = parsed_items
            else:
                kwargs[identifier] = value

    if errors:
        return None, errors

    try:
        return (
            SettingsParsing(parsed_kwargs=kwargs,
                            product=hypothesis.settings(**kwargs)),
            [],
        )
    except hypothesis.errors.InvalidArgument as error:
        return None, [f"Invalid Hypothesis settings: {error}"]
Ejemplo n.º 5
0
    if wsgi_auth:
        headers["Authorization"] = wsgi_auth
    return headers


def _run_checks(case: Case, checks: Iterable[Check], result: TestResult,
                response: GenericResponse) -> None:
    errors = []

    for check in checks:
        check_name = check.__name__
        try:
            check(response, case)
            result.add_success(check_name, case)
        except AssertionError as exc:
            errors.append(exc)
            result.add_failure(check_name, case, str(exc))

    if errors:
        raise get_grouped_exception(*errors)


def prepare_timeout(timeout: Optional[int]) -> Optional[float]:
    """Request timeout is in milliseconds, but `requests` uses seconds."""
    output: Optional[Union[int, float]] = timeout
    if timeout is not None:
        output = timeout / 1000
    return output


@contextmanager