2016-12-23 18:29:24 +03:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
from itertools import repeat
|
2018-09-07 01:45:19 +03:00
|
|
|
import os
|
2015-01-20 21:53:52 +03:00
|
|
|
|
2016-12-23 18:29:24 +03:00
|
|
|
import hypothesis.strategies as st
|
2016-12-23 19:06:56 +03:00
|
|
|
import pytest
|
|
|
|
from hypothesis import assume
|
|
|
|
from hypothesis import given
|
|
|
|
|
|
|
|
from mardor.utils import auto_decompress_stream
|
|
|
|
from mardor.utils import bz2_compress_stream
|
|
|
|
from mardor.utils import bz2_decompress_stream
|
2018-09-07 01:45:19 +03:00
|
|
|
from mardor.utils import filesize
|
2016-12-23 19:06:56 +03:00
|
|
|
from mardor.utils import mkdir
|
2017-07-27 17:54:12 +03:00
|
|
|
from mardor.utils import safejoin
|
2016-12-23 19:06:56 +03:00
|
|
|
from mardor.utils import takeexactly
|
2015-01-20 21:53:52 +03:00
|
|
|
|
|
|
|
|
2018-01-18 18:07:36 +03:00
|
|
|
@given(st.lists(st.binary()))
|
|
|
|
def test_takeexactly(data):
|
|
|
|
n = len(b''.join(data))
|
|
|
|
for i in range(n+1):
|
|
|
|
assert len(b''.join(takeexactly(data, i))) == i
|
2015-01-20 21:53:52 +03:00
|
|
|
|
2015-09-10 17:49:07 +03:00
|
|
|
|
2018-01-18 18:07:36 +03:00
|
|
|
@given(st.lists(st.binary()))
|
|
|
|
def test_takeexactly_notenough(data):
|
|
|
|
n = len(b''.join(data))
|
2016-12-23 18:29:24 +03:00
|
|
|
with pytest.raises(ValueError):
|
2018-01-18 18:07:36 +03:00
|
|
|
b''.join(takeexactly(data, n+1))
|
2015-09-10 17:49:07 +03:00
|
|
|
|
2016-12-23 18:29:24 +03:00
|
|
|
|
|
|
|
@given(st.lists(st.binary()), st.integers(min_value=1, max_value=9))
|
|
|
|
def test_bz2_streams(data, level):
|
|
|
|
stream = bz2_decompress_stream(bz2_compress_stream(data, level))
|
|
|
|
assert b''.join(stream) == b''.join(data)
|
|
|
|
|
|
|
|
|
|
|
|
def test_bz2_stream_large():
|
|
|
|
# This is only to test the case where the compressor returns data before
|
|
|
|
# the stream ends
|
|
|
|
n = 70000
|
|
|
|
stream = repeat(b'hello', n)
|
|
|
|
stream = bz2_decompress_stream(bz2_compress_stream(stream, level=1))
|
|
|
|
assert b''.join(stream) == b'hello' * n
|
|
|
|
|
|
|
|
|
|
|
|
def test_bz2_stream_exact_blocksize():
|
|
|
|
stream = [b'0' * 100000]
|
|
|
|
stream = bz2_decompress_stream(bz2_compress_stream(stream, level=1))
|
|
|
|
assert b''.join(stream) == b'0' * 100000
|
|
|
|
|
|
|
|
|
|
|
|
def test_auto_decompress():
|
|
|
|
n = 10000
|
|
|
|
stream = repeat(b'hello', n)
|
|
|
|
stream = auto_decompress_stream(bz2_compress_stream(stream))
|
|
|
|
assert b''.join(stream) == b'hello' * n
|
|
|
|
|
|
|
|
n = 10000
|
|
|
|
stream = repeat(b'hello', n)
|
|
|
|
stream = auto_decompress_stream(stream)
|
|
|
|
assert b''.join(stream) == b'hello' * n
|
|
|
|
|
|
|
|
|
|
|
|
def test_mkdir(tmpdir):
|
|
|
|
d = tmpdir.join('foo')
|
|
|
|
mkdir(str(d))
|
|
|
|
assert d.isdir()
|
|
|
|
|
|
|
|
|
|
|
|
def test_mkdir_existing(tmpdir):
|
|
|
|
d = tmpdir.join('foo')
|
|
|
|
d.mkdir()
|
|
|
|
mkdir(str(d))
|
|
|
|
assert d.isdir()
|
|
|
|
|
|
|
|
|
|
|
|
def test_mkdir_existingfile(tmpdir):
|
|
|
|
d = tmpdir.join('foo')
|
|
|
|
d.write('helloworld')
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
mkdir(str(d))
|
2017-07-27 17:54:12 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_safejoin():
|
|
|
|
assert safejoin('/path/to/t', 'tnew/foo/bar') == '/path/to/t/tnew/foo/bar'
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
safejoin('/path/to/t', '../tnew/foo/bar')
|
2018-09-07 01:45:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_filesize():
|
2022-09-01 11:21:25 +03:00
|
|
|
with open(__file__, 'rb') as f:
|
|
|
|
assert os.path.getsize(__file__) == filesize(f)
|