Пример #1
0
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)
Пример #2
0
def test_binary_zlibfile():
    filename = env['filename'] + str(random.randint(0, 1000))

    # Test bad compression levels
    for bad_value in (-1, 10, 15, 'a', (), {}):
        nose.tools.assert_raises(ValueError,
                                 BinaryZlibFile,
                                 filename,
                                 'wb',
                                 compresslevel=bad_value)

    # Test invalid modes
    for bad_mode in ('a', 'x', 'r', 'w', 1, 2):
        nose.tools.assert_raises(ValueError, BinaryZlibFile, filename,
                                 bad_mode)

    # Test wrong filename type (not a string or a file)
    for bad_file in (1, (), {}):
        nose.tools.assert_raises(TypeError, BinaryZlibFile, bad_file, 'rb')

    for d in (
            b'a little data as bytes.',
            # More bytes
            10000 *
            "{0}".format(random.randint(0, 1000) * 1000).encode('latin-1')):
        # Regular cases
        for compress_level in (1, 3, 9):
            with open(filename, 'wb') as f:
                with BinaryZlibFile(f, 'wb',
                                    compresslevel=compress_level) as fz:
                    nose.tools.assert_true(fz.writable())
                    fz.write(d)
                    nose.tools.assert_equal(fz.fileno(), f.fileno())
                    nose.tools.assert_raises(io.UnsupportedOperation,
                                             fz._check_can_read)
                    nose.tools.assert_raises(io.UnsupportedOperation,
                                             fz._check_can_seek)
                nose.tools.assert_true(fz.closed)
                nose.tools.assert_raises(ValueError, fz._check_not_closed)

            with open(filename, 'rb') as f:
                with BinaryZlibFile(f) as fz:
                    nose.tools.assert_true(fz.readable())
                    if PY3_OR_LATER:
                        nose.tools.assert_true(fz.seekable())
                    nose.tools.assert_equal(fz.fileno(), f.fileno())
                    nose.tools.assert_equal(fz.read(), d)
                    nose.tools.assert_raises(io.UnsupportedOperation,
                                             fz._check_can_write)
                    if PY3_OR_LATER:
                        # io.BufferedIOBase doesn't have seekable() method in
                        # python 2
                        nose.tools.assert_true(fz.seekable())
                        fz.seek(0)
                        nose.tools.assert_equal(fz.tell(), 0)
                nose.tools.assert_true(fz.closed)

            os.remove(filename)

            # Test with a filename as input
            with BinaryZlibFile(filename, 'wb',
                                compresslevel=compress_level) as fz:
                nose.tools.assert_true(fz.writable())
                fz.write(d)

            with BinaryZlibFile(filename, 'rb') as fz:
                nose.tools.assert_equal(fz.read(), d)
                nose.tools.assert_true(fz.seekable())

            # Test without context manager
            fz = BinaryZlibFile(filename, 'wb', compresslevel=compress_level)
            nose.tools.assert_true(fz.writable())
            fz.write(d)
            fz.close()

            fz = BinaryZlibFile(filename, 'rb')
            nose.tools.assert_equal(fz.read(), d)
            fz.close()
Пример #3
0
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")
Пример #4
0
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")
Пример #5
0
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()
            if PY3_OR_LATER:
                assert fz.seekable()
            assert fz.fileno() == f.fileno()
            assert fz.read() == data
            with raises(io.UnsupportedOperation):
                fz._check_can_write()
            if PY3_OR_LATER:
                # io.BufferedIOBase doesn't have seekable() method in
                # python 2
                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()
Пример #6
0
def test_binary_zlibfile():
    filename = env['filename'] + str(random.randint(0, 1000))

    # Test bad compression levels
    for bad_value in (-1, 10, 15, 'a', (), {}):
        assert_raises(ValueError, BinaryZlibFile, filename, 'wb',
                      compresslevel=bad_value)

    # Test invalid modes
    for bad_mode in ('a', 'x', 'r', 'w', 1, 2):
        assert_raises(ValueError, BinaryZlibFile, filename, bad_mode)

    # Test wrong filename type (not a string or a file)
    for bad_file in (1, (), {}):
        assert_raises(TypeError, BinaryZlibFile, bad_file, 'rb')

    for d in (b'a little data as bytes.',
              # More bytes
              10000 * "{}"
              .format(random.randint(0, 1000) * 1000).encode('latin-1')):
        # Regular cases
        for compress_level in (1, 3, 9):
            with open(filename, 'wb') as f:
                with BinaryZlibFile(f, 'wb',
                                    compresslevel=compress_level) as fz:
                    assert fz.writable()
                    fz.write(d)
                    assert fz.fileno() == f.fileno()
                    assert_raises(io.UnsupportedOperation, fz._check_can_read)
                    assert_raises(io.UnsupportedOperation, fz._check_can_seek)
                assert fz.closed
                assert_raises(ValueError, fz._check_not_closed)

            with open(filename, 'rb') as f:
                with BinaryZlibFile(f) as fz:
                    assert fz.readable()
                    if PY3_OR_LATER:
                        assert fz.seekable()
                    assert fz.fileno() == f.fileno()
                    assert fz.read() == d
                    assert_raises(io.UnsupportedOperation,
                                  fz._check_can_write)
                    if PY3_OR_LATER:
                        # io.BufferedIOBase doesn't have seekable() method in
                        # python 2
                        assert fz.seekable()
                        fz.seek(0)
                        assert fz.tell() == 0
                assert fz.closed

            os.remove(filename)

            # Test with a filename as input
            with BinaryZlibFile(filename, 'wb',
                                compresslevel=compress_level) as fz:
                assert fz.writable()
                fz.write(d)

            with BinaryZlibFile(filename, 'rb') as fz:
                assert fz.read() == d
                assert fz.seekable()

            # Test without context manager
            fz = BinaryZlibFile(filename, 'wb', compresslevel=compress_level)
            assert fz.writable()
            fz.write(d)
            fz.close()

            fz = BinaryZlibFile(filename, 'rb')
            assert fz.read() == d
            fz.close()