Example #1
0
def test_read_not_exists(pathlib_path: MagicMock):
    """Should raise error if file does not exist."""
    path = _support.make_path("{}", exists=False)
    pathlib_path.return_value = path

    with pytest.raises(FileNotFoundError):
        _fio.read(path, ["prefix", "subprefix"])
Example #2
0
    def from_file(
        cls,
        path: typing.Union[str, pathlib.Path],
        prefix: typing.Union[str, typing.Iterable[str]] = None,
        client_overrides: typing.Dict[str, typing.Any] = None,
    ) -> "Lobotomy":
        """
        Create a patch instance.

        :param path:
            Path to a configuration file containing the lobotomy data to use
            in mock calls during the lifetime of this patch. This can be the
            default value if a data value is set instead.
        :param prefix:
            An optional key or multi-key prefix within the path-loaded data
            object where the lobotomy data resides. Use this when the
            configuration file for the test has a broader scope.
        :param client_overrides:
            Optional dictionary containing mappings of service names to clients
            that should be used in place of lobotomy clients during the lifecycle
            of this lobotomy patch. Useful for mixing lobotomy clients with other
            mocking clients in complex testing scenarios.
        """
        p = pathlib.Path(path)
        return cls(data=_fio.read(p, prefix),
                   client_overrides=client_overrides)
Example #3
0
def run(context: "_definitions.CliContext") -> "_definitions.ExecutionResult":
    """Add a new call to the specified command actions."""
    file_format = context.args.file_format
    path = _get_path(context)
    prefix = [item for item in (context.args.prefix or "").split(".") if item]

    try:
        configs = _fio.read(path, prefix,
                            file_format=file_format)  # type: ignore
    except (AttributeError, FileNotFoundError, TypeError):
        configs = {}

    service_name, method_name = context.args.boto_operation.split(".")
    service = _services.load_definition(service_name)
    method = service.lookup(method_name)

    _mutator.add_service_response(configs, method)

    if path:
        _fio.write(path, configs, prefix, file_format=file_format)
        return _definitions.ExecutionResult(
            code="ADDED",
            message="New call has been added to the configs.").echo()

    if file_format == "json":
        print(json.dumps(configs, indent=2))
    elif file_format == "toml":
        print(toml.dumps(configs))
    else:
        print(yaml.dump(configs))

    return _definitions.ExecutionResult(
        code="ECHOED", message="New call has been echoed to stdout.")
Example #4
0
def test_read_no_prefix(pathlib_path: MagicMock):
    """Should successfully read the file with no key prefix."""
    path = _support.make_path("{}", "json")
    pathlib_path.return_value = path
    assert _fio.read(path) == {}
Example #5
0
def test_read_new_prefix(pathlib_path: MagicMock, scenario: dict):
    """Should successfully read the file even non-existent prefix."""
    path = _support.make_path(**scenario)
    pathlib_path.return_value = path
    result = _fio.read(path, ["foo", "bar"])
    assert result == {}
Example #6
0
def test_read(pathlib_path: MagicMock, scenario: dict):
    """Should successfully read the file."""
    path = _support.make_path(**scenario)
    pathlib_path.return_value = path
    result = _fio.read(path, ["prefix", "subprefix"])
    assert result == SOURCE_DATA["prefix"]["subprefix"]