def test_binary_zlibfile_bad_compression_levels(tmpdir, bad_value): filename = tmpdir.join('test.pkl').strpath with raises(ValueError) as excinfo: BinaryZlibFile(filename, 'wb', compresslevel=bad_value) pattern = re.escape("'compresslevel' must be an integer between 1 and 9. " "You provided 'compresslevel={}'".format(bad_value)) excinfo.match(pattern)
def test_binary_zlibfile_invalid_filename_type(bad_file): with raises(TypeError) as excinfo: BinaryZlibFile(bad_file, 'rb') excinfo.match("filename must be a str or bytes object, or a file")
def test_binary_zlibfile_invalid_modes(tmpdir, bad_mode): filename = tmpdir.join('test.pkl').strpath with raises(ValueError) as excinfo: BinaryZlibFile(filename, bad_mode) excinfo.match("Invalid mode")
def test_binary_zlibfile(tmpdir, data, compress_level): filename = tmpdir.join('test.pkl').strpath # Regular cases with open(filename, 'wb') as f: with BinaryZlibFile(f, 'wb', compresslevel=compress_level) as fz: assert fz.writable() fz.write(data) assert fz.fileno() == f.fileno() with raises(io.UnsupportedOperation): fz._check_can_read() with raises(io.UnsupportedOperation): fz._check_can_seek() assert fz.closed with raises(ValueError): fz._check_not_closed() with open(filename, 'rb') as f: with BinaryZlibFile(f) as fz: assert fz.readable() assert fz.seekable() assert fz.fileno() == f.fileno() assert fz.read() == data with raises(io.UnsupportedOperation): fz._check_can_write() assert fz.seekable() fz.seek(0) assert fz.tell() == 0 assert fz.closed # Test with a filename as input with BinaryZlibFile(filename, 'wb', compresslevel=compress_level) as fz: assert fz.writable() fz.write(data) with BinaryZlibFile(filename, 'rb') as fz: assert fz.read() == data assert fz.seekable() # Test without context manager fz = BinaryZlibFile(filename, 'wb', compresslevel=compress_level) assert fz.writable() fz.write(data) fz.close() fz = BinaryZlibFile(filename, 'rb') assert fz.read() == data fz.close()
def test_binary_zlib_file(tmpdir, filename): """Testing creation of files depending on the type of the filenames.""" binary_file = BinaryZlibFile(tmpdir.join(filename).strpath, mode='wb') binary_file.close()