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
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
def test_close_error(): def close(): raise IOError("backend error") with pytest.raises(IOError): with memory.Backend("r+") as m: m.close = close
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"
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"
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
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))
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"
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")
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"
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()
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()
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
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
def test_flush_repr(): op = ops.Flush(memory.Backend("r")) rep = repr(op) assert "Flush" in rep assert "done=0" in rep
def test_flush(): dst = memory.Backend("r+") dst.write(b"x") op = ops.Flush(dst) op.run() assert not dst.dirty
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
def test_invalid_mode(): with pytest.raises(ValueError): memory.Backend("invalid")
def test_context_manager(): with memory.Backend("r+") as m: m.write(b"data") with pytest.raises(ValueError): m.write("more")