Ejemplo n.º 1
0
    def backup_single_in_multiple_out(
        self,
        source: Source,
        destinations: Sequence[Destination],
    ):
        """
        Backup the selected source into the destinations provided.

        A broadcast strategy will be used: the content
        of a single source will flow into every destination
        provided.

        :param source: The sources to backup.
        :param destinations: The destinations of backup.
        """
        assert isinstance(source, Source)
        assert isinstance(destinations, (Tuple, List))
        assert all(
            isinstance(destination, Destination)
            for destination in destinations)

        raw = bytes(source)
        bytesource = BytesSource(raw)
        return [
            self.backup_siso(bytesource, destination)
            for destination in destinations
        ]
Ejemplo n.º 2
0
def test_boa_backup_siso():
    boa = Boa()
    msg = b"foo"

    source = BytesSource(msg)
    destination = FileStreamDestination(io.BytesIO())
    boa.backup(source, destination)
    destination.filestream.seek(0)
    assert destination.filestream.read() == msg
Ejemplo n.º 3
0
def test_boa_backup_mimo():
    boa = Boa()
    msg = b"foo"
    length = 3

    sources = [BytesSource(msg) for _ in range(length)]
    destinations = [FileStreamDestination(io.BytesIO()) for _ in range(length)]
    boa.backup(sources, destinations)
    for message, destination in zip([msg] * length, destinations):
        destination.filestream.seek(0)
        assert destination.filestream.read() == message

    # mismatch length
    sources = [BytesSource(msg) for _ in range(length)]
    destinations = [
        FileStreamDestination(io.BytesIO()) for _ in range(length + 1)
    ]
    with pytest.raises(ValueError):
        boa.backup(sources, destinations)
Ejemplo n.º 4
0
def test_boa_backup_miso():
    boa = Boa()
    msg = b"foo"
    length = 3

    sources = [BytesSource(msg) for _ in range(length)]
    destination = FileStreamDestination(io.BytesIO())
    boa.backup(sources, destination)
    destination.filestream.seek(0)
    assert destination.filestream.read() == msg * length
Ejemplo n.º 5
0
def test_boa_backup_simo():
    boa = Boa()
    msg = b"foo"
    length = 3

    source = BytesSource(msg)
    destinations = [FileStreamDestination(io.BytesIO()) for _ in range(length)]
    boa.backup(source, destinations)
    for destination in destinations:
        destination.filestream.seek(0)
        assert destination.filestream.read() == msg
Ejemplo n.º 6
0
    def backup_multiple_in_single_out(
        self,
        sources: Sequence[Source],
        destination: Destination,
    ):
        """
        Backup the selected sources into the destination.

        The content will be merged across sources into
        a single destination.

        :param sources: The sources to backup.
        :param destination: The destination of backup.
        """
        assert isinstance(sources, (Tuple, List))
        assert all(isinstance(source, Source) for source in sources)
        assert isinstance(destination, Destination)

        raws = [bytes(source) for source in sources]
        raw = b"".join(raws)
        source = BytesSource(raw)
        return self.backup_siso(source, destination)
Ejemplo n.º 7
0
    # mismatch length
    sources = [BytesSource(msg) for _ in range(length)]
    destinations = [
        FileStreamDestination(io.BytesIO()) for _ in range(length + 1)
    ]
    with pytest.raises(ValueError):
        boa.backup(sources, destinations)


@pytest.mark.parametrize(
    "src",
    [
        b"foo",
        tempfile.NamedTemporaryFile("w+b", delete=False).name,
        io.StringIO("hello world"),
        BytesSource(b"hello"),
        [b"foo", b"bar"],
    ],
)
@pytest.mark.parametrize(
    "dst",
    [
        tempfile.NamedTemporaryFile("w+b", delete=False).name,
        io.BytesIO(),
        FileStreamDestination(io.BytesIO()),
        [io.BytesIO(), io.BytesIO()],
    ],
)
@pytest.mark.parametrize("return_wrappers", [True, False])
def test_backup(src, dst, return_wrappers):
    # we already know that backup works, so we