Ejemplo n.º 1
0
def test_receive_flush(extra, dirty):
    size = 4096
    dst = memory.Backend("r+", b"a" * size)
    src = io.BytesIO(b"b" * size)
    op = ops.Receive(dst, src, size, **extra)
    op.run()
    assert dst.dirty == dirty
Ejemplo n.º 2
0
def test_zero_repr():
    op = ops.Zero(memory.Backend("r+"), 4096)
    rep = repr(op)
    assert "Zero" in rep
    assert "offset=0" in rep
    assert "size=4096" in rep
    assert "done=0" in rep
Ejemplo n.º 3
0
def test_close_error():
    def close():
        raise IOError("backend error")

    with pytest.raises(IOError):
        with memory.Backend("r+") as m:
            m.close = close
Ejemplo n.º 4
0
def test_send_seek():
    src = memory.Backend("r", b"0123456789")
    src.seek(8)
    dst = io.BytesIO()
    op = ops.Send(src, dst, 5)
    op.run()
    assert dst.getvalue() == b"01234"
Ejemplo n.º 5
0
def test_create_with_bytes():
    m = memory.Backend("r", b"data")
    assert m.readable()
    assert not m.writable()

    b = bytearray(5)
    assert m.readinto(b) == 4
    assert b[:] == b"data\0"
Ejemplo n.º 6
0
def test_size():
    m = memory.Backend("r+", b"data")
    assert m.size() == 4
    assert m.tell() == 0
    m.zero(5)
    m.seek(3)
    assert m.size() == 5
    assert m.tell() == 3
Ejemplo n.º 7
0
def test_close():
    m = memory.Backend("r+")
    m.close()
    # All operations should fail now with:
    #     ValueError: I/O operation on closed file
    with pytest.raises(ValueError):
        m.write("more")
    with pytest.raises(ValueError):
        m.readinto(bytearray(10))
Ejemplo n.º 8
0
def test_zero_seek():
    dst = memory.Backend("r+", b"a" * 10)
    dst.seek(8)
    op = ops.Zero(dst, 5)
    op.run()
    dst.seek(0)
    b = bytearray(11)
    n = dst.readinto(b)
    assert n == 10
    assert b == b"\0\0\0\0\0aaaaa\0"
Ejemplo n.º 9
0
def test_propagate_user_error():
    class UserError(Exception):
        pass

    def close():
        raise IOError("backend error")

    with pytest.raises(UserError):
        with memory.Backend("r+") as m:
            m.close = close
            raise UserError("user error")
Ejemplo n.º 10
0
def test_receive_seek():
    dst = memory.Backend("r+", b"a" * 10)
    dst.seek(8)
    src = io.BytesIO(b"b" * 5)
    op = ops.Receive(dst, src, 5)
    op.run()
    dst.seek(0)
    b = bytearray(11)
    n = dst.readinto(b)
    assert n == 10
    assert b == b"bbbbbaaaaa\0"
Ejemplo n.º 11
0
def test_open_writeonly():
    m = memory.Backend("w")
    assert not m.readable()
    assert m.writable()

    data = b"data"
    m.write(data)
    assert m.tell() == len(data)
    with pytest.raises(IOError):
        m.readinto(bytearray(10))
    m.seek(0)
    with pytest.raises(IOError):
        m.write_to(io.BytesIO(), 10)
    m.flush()
Ejemplo n.º 12
0
def test_open_readonly():
    m = memory.Backend("r")
    assert m.readable()
    assert not m.writable()

    with pytest.raises(IOError):
        m.write(b"data")
    with pytest.raises(IOError):
        m.zero(4)
    assert m.tell() == 0
    b = bytearray(b"before")
    assert m.readinto(b) == 0
    assert b == b"before"

    dst = io.BytesIO()
    m.seek(0)
    assert m.write_to(dst, 0) == 0
    assert dst.getvalue() == b""
    m.flush()
Ejemplo n.º 13
0
def test_dirty():
    # backend created clean
    m = memory.Backend("r+", b"data")
    assert not m.dirty

    # write ans zero dirty the backend
    m.write(b"01234")
    assert m.dirty
    m.flush()
    assert not m.dirty
    m.zero(5)
    assert m.dirty
    m.flush()
    assert not m.dirty

    # readinto, seek do not affect dirty.
    b = bytearray(10)
    m.seek(0)
    assert not m.dirty
    m.readinto(b)
    assert not m.dirty
Ejemplo n.º 14
0
def test_open_read_write():
    m = memory.Backend("r+")
    assert m.readable()
    assert m.writable()
    assert not m.sparse

    data = b"data"
    m.write(data)
    assert m.tell() == len(data)

    m.zero(4)
    size = len(data) + 4
    assert m.tell() == size

    content = data + b"\0" * 4
    b = bytearray(size)
    m.seek(0)
    assert m.readinto(b) == size
    assert b == content

    dst = io.BytesIO()
    m.seek(0)
    assert m.write_to(dst, 8) == 8
    assert dst.getvalue() == content
Ejemplo n.º 15
0
def test_flush_repr():
    op = ops.Flush(memory.Backend("r"))
    rep = repr(op)
    assert "Flush" in rep
    assert "done=0" in rep
Ejemplo n.º 16
0
def test_flush():
    dst = memory.Backend("r+")
    dst.write(b"x")
    op = ops.Flush(dst)
    op.run()
    assert not dst.dirty
Ejemplo n.º 17
0
def test_zero_flush(extra, dirty):
    size = 4096
    dst = memory.Backend("r+", b"a" * size)
    op = ops.Zero(dst, size, **extra)
    op.run()
    assert dst.dirty == dirty
Ejemplo n.º 18
0
def test_invalid_mode():
    with pytest.raises(ValueError):
        memory.Backend("invalid")
Ejemplo n.º 19
0
def test_context_manager():
    with memory.Backend("r+") as m:
        m.write(b"data")
    with pytest.raises(ValueError):
        m.write("more")