コード例 #1
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_staggered_read(mode):
    """Read bytes repeatedly."""
    filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt')
    file = yield from aioopen(filename, mode=mode)

    yield from file.seek(0)  # Needed for the append mode.

    actual = []
    while True:
        char = yield from file.read(1)
        if char:
            actual.append(char)
        else:
            break

    assert '' == (yield from file.read())

    expected = []
    with open(filename, mode='r') as f:
        while True:
            char = f.read(1)
            if char:
                expected.append(char)
            else:
                break

    assert actual == expected

    yield from file.close()
コード例 #2
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_staggered_read(mode, buffering):
    """Read bytes repeatedly."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')
    async with aioopen(filename, mode=mode, buffering=buffering) as file:
        await file.seek(0)  # Needed for the append mode.

        actual = []
        while True:
            byte = await file.read(1)
            if byte:
                actual.append(byte)
            else:
                break

        assert b'' == (await file.read())

        expected = []
        with open(filename, mode='rb') as f:
            while True:
                byte = f.read(1)
                if byte:
                    expected.append(byte)
                else:
                    break

    assert actual == expected
コード例 #3
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_iteration(mode, buffering):
    """Test iterating over lines from a file."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    async with aioopen(filename, mode=mode, buffering=buffering) as file:
        # Append mode needs us to seek.
        await file.seek(0)

        counter = 1
        # The old iteration pattern:
        while True:
            line = await file.readline()
            if not line:
                break
            assert line.strip() == b'line ' + str(counter).encode()
            counter += 1

        counter = 1
        await file.seek(0)
        # The new iteration pattern:
        async for line in file:
            assert line.strip() == b'line ' + str(counter).encode()
            counter += 1

    assert file.closed
コード例 #4
0
ファイル: test_binary.py プロジェクト: Eyepea/aiofiles
def test_simple_truncate(mode, buffering, tmpdir):
    """Test truncating files."""
    filename = 'bigfile.bin'
    content = b'0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write_binary(content)

    file = yield from aioopen(str(full_file), mode=mode,
                              buffering=buffering)

    # The append modes want us to seek first.
    yield from file.seek(0)

    if 'w' in mode:
        # We've just erased the entire file.
        yield from file.write(content)
        yield from file.flush()
        yield from file.seek(0)

    yield from file.truncate()

    yield from file.close()

    assert b'' == full_file.read_binary()
コード例 #5
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_read(mode, buffering):
    """Just read some bytes from a test file."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')
    async with aioopen(filename, mode=mode, buffering=buffering) as file:
        await file.seek(0)  # Needed for the append mode.

        actual = await file.read()

        assert b'' == (await file.read())
    assert actual == open(filename, mode='rb').read()
コード例 #6
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_readinto(mode, buffering):
    """Test the readinto functionality."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')
    async with aioopen(filename, mode=mode, buffering=buffering) as file:
        await file.seek(0)  # Needed for the append mode.

        array = bytearray(4)
        bytes_read = await file.readinto(array)

        assert bytes_read == 4
        assert array == open(filename, mode='rb').read(4)
コード例 #7
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_seek(mode, buffering, tmpdir):
    """Test seeking and then reading."""
    filename = 'bigfile.bin'
    content = b'0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write_binary(content)

    async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
        await file.seek(4)

        assert b'4' == (await file.read(1))
コード例 #8
0
ファイル: test_text_35.py プロジェクト: farakavco/aiofiles
async def test_simple_read(mode):
    """Just read some bytes from a test file."""
    filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt')
    async with aioopen(filename, mode=mode) as file:
        await file.seek(0)  # Needed for the append mode.

        actual = await file.read()

        assert '' == (await file.read())
    assert actual == open(filename, mode='r').read()

    assert file.closed
コード例 #9
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_read(mode):
    """Just read some bytes from a test file."""
    filename = join(dirname(__file__), '..', 'resources', 'test_file1.txt')
    file = yield from aioopen(filename, mode=mode)

    yield from file.seek(0)  # Needed for the append mode.

    actual = yield from file.read()

    assert '' == (yield from file.read())
    assert actual == open(filename, mode='r').read()

    yield from file.close()
コード例 #10
0
ファイル: test_text_35.py プロジェクト: farakavco/aiofiles
async def test_simple_read_fail(mode, tmpdir):
    """Try reading some bytes and fail."""
    filename = 'bigfile.bin'
    content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)
    with pytest.raises(ValueError):
        async with aioopen(str(full_file), mode=mode) as file:
            await file.seek(0)  # Needed for the append mode.

            await file.read()

    assert file.closed
コード例 #11
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_close_ctx_mgr(mode, buffering, tmpdir):
    """Open a file, read a byte, and close it."""
    filename = 'bigfile.bin'
    content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write_binary(content)

    async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
        assert not file.closed
        assert not file._file.closed

    assert file.closed
    assert file._file.closed
コード例 #12
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_readlines(mode, buffering):
    """Test the readlines functionality."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    with open(filename, mode='rb') as f:
        expected = f.readlines()

    async with aioopen(str(filename), mode=mode) as file:
        # Append mode needs us to seek.
        await file.seek(0)

        actual = await file.readlines()

    assert actual == expected
コード例 #13
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_seek(mode, tmpdir):
    """Test seeking and then reading."""
    filename = 'bigfile.bin'
    content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)

    file = yield from aioopen(str(full_file), mode=mode)

    yield from file.seek(4)

    assert '4' == (yield from file.read(1))

    yield from file.close()
コード例 #14
0
ファイル: test_text.py プロジェクト: zsrinivas/aiofiles
async def test_simple_iteration_ctx_mgr(mode):
    """Test iterating over lines from a file."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    async with aioopen(filename, mode=mode) as file:
        assert not file.closed
        await file.seek(0)

        counter = 1

        async for line in file:
            assert line.strip() == 'line ' + str(counter)
            counter += 1

    assert file.closed
コード例 #15
0
def test_simple_seek(mode, tmpdir):
    """Test seeking and then reading."""
    filename = 'bigfile.bin'
    content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)

    file = yield from aioopen(str(full_file), mode=mode)

    yield from file.seek(4)

    assert '4' == (yield from file.read(1))

    yield from file.close()
コード例 #16
0
ファイル: test_binary_35.py プロジェクト: farakavco/aiofiles
async def test_simple_write(mode, buffering, tmpdir):
    """Test writing into a file."""
    filename = 'bigfile.bin'
    content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)

    if 'r' in mode:
        full_file.ensure()  # Read modes want it to already exist.

    async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
        bytes_written = await file.write(content)

    assert bytes_written == len(content)
    assert content == full_file.read_binary()
コード例 #17
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_write(mode, tmpdir):
    """Test writing into a file."""
    filename = 'bigfile.bin'
    content = '0' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)

    if 'r' in mode:
        full_file.ensure()  # Read modes want it to already exist.

    file = yield from aioopen(str(full_file), mode=mode)
    bytes_written = yield from file.write(content)
    yield from file.close()

    assert bytes_written == len(content)
    assert content == full_file.read()
コード例 #18
0
ファイル: test_text.py プロジェクト: zhazhajust/aiofiles
async def test_simple_readlines(mode):
    """Test the readlines functionality."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    with open(filename, mode='r') as f:
        expected = f.readlines()

    async with aioopen(filename, mode=mode) as file:
        # Append mode needs us to seek.
        await file.seek(0)

        actual = await file.readlines()

    assert file.closed

    assert actual == expected
コード例 #19
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_readlines(mode):
    """Test the readlines functionality."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    with open(filename, mode='r') as f:
        expected = f.readlines()

    file = yield from aioopen(filename, mode=mode)

    # Append mode needs us to seek.
    yield from file.seek(0)

    actual = yield from file.readlines()

    yield from file.close()

    assert actual == expected
コード例 #20
0
ファイル: test_binary.py プロジェクト: zsrinivas/aiofiles
async def test_simple_detach(tmpdir):
    """Test detaching for buffered streams."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)
    full_file.write_binary(b'0123456789')

    with pytest.raises(ValueError):
        async with aioopen(str(full_file), mode='rb') as file:
            raw_file = file.detach()

            assert raw_file

            with pytest.raises(ValueError):
                await file.read()

    assert b'0123456789' == raw_file.read(10)
コード例 #21
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_detach(tmpdir):
    """Test detaching for buffered streams."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)
    full_file.write('0123456789')

    file = yield from aioopen(str(full_file), mode='r')

    raw_file = file.detach()

    assert raw_file

    with pytest.raises(ValueError):
        yield from file.read()

    assert b'0123456789' == raw_file.read(10)
コード例 #22
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_close(mode, tmpdir):
    """Open a file, read a byte, and close it."""
    filename = 'bigfile.bin'
    content = '0' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)

    file = yield from aioopen(str(full_file), mode=mode)

    assert not file.closed
    assert not file._file.closed

    yield from file.close()

    assert file.closed
    assert file._file.closed
コード例 #23
0
ファイル: test_binary.py プロジェクト: Eyepea/aiofiles
def test_simple_readall(tmpdir):
    """Test the readall function by reading a large file in.

    Only RawIOBase supports readall().
    """
    filename = 'bigfile.bin'
    content = b'0' * 4 * io.DEFAULT_BUFFER_SIZE  # Hopefully several reads.

    sync_file = tmpdir.join(filename)
    sync_file.write_binary(content)

    file = yield from aioopen(str(sync_file), mode='rb', buffering=0)

    actual = yield from file.readall()

    assert actual == content

    yield from file.close()
コード例 #24
0
ファイル: test_text_35.py プロジェクト: farakavco/aiofiles
async def test_simple_flush(mode, tmpdir):
    """Test flushing to a file."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)

    if 'r' in mode:
        full_file.ensure()  # Read modes want it to already exist.

    async with aioopen(str(full_file), mode=mode) as file:
        await file.write('0')  # Shouldn't flush.

        assert '' == full_file.read_text(encoding='utf8')

        await file.flush()

        assert '0' == full_file.read_text(encoding='utf8')

    assert file.closed
コード例 #25
0
ファイル: test_text.py プロジェクト: zhazhajust/aiofiles
async def test_simple_flush(mode, tmpdir):
    """Test flushing to a file."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)

    if 'r' in mode:
        full_file.ensure()  # Read modes want it to already exist.

    async with aioopen(str(full_file), mode=mode) as file:
        await file.write('0')  # Shouldn't flush.

        assert '' == full_file.read_text(encoding='utf8')

        await file.flush()

        assert '0' == full_file.read_text(encoding='utf8')

    assert file.closed
コード例 #26
0
ファイル: test_open.py プロジェクト: zsrinivas/aiofiles
def test_file_not_found(mode):
    filename = 'non_existent'

    try:
        open(filename, mode=mode)
    except Exception as e:
        expected = e

    assert expected

    try:
        yield from aioopen(filename, mode=mode)
    except Exception as e:
        actual = e

    assert actual

    assert actual.errno == expected.errno
    assert str(actual) == str(expected)
コード例 #27
0
ファイル: test_binary.py プロジェクト: zsrinivas/aiofiles
async def test_simple_peek(mode, tmpdir):
    """Test flushing to a file."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)
    full_file.write_binary(b'0123456789')

    async with aioopen(str(full_file), mode=mode) as file:
        if 'a' in mode:
            await file.seek(0)  # Rewind for append modes.

        peeked = await file.peek(1)

        # Technically it's OK for the peek to return less bytes than requested.
        if peeked:
            assert peeked.startswith(b'0')

            read = await file.read(1)

            assert peeked.startswith(read)
コード例 #28
0
ファイル: test_binary.py プロジェクト: zsrinivas/aiofiles
async def test_simple_flush(mode, buffering, tmpdir):
    """Test flushing to a file."""
    filename = 'file.bin'

    full_file = tmpdir.join(filename)

    if 'r' in mode:
        full_file.ensure()  # Read modes want it to already exist.

    async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
        await file.write(b'0')  # Shouldn't flush.

        if buffering == -1:
            assert b'' == full_file.read_binary()
        else:
            assert b'0' == full_file.read_binary()

        await file.flush()

        assert b'0' == full_file.read_binary()
コード例 #29
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_iteration(mode):
    """Test iterating over lines from a file."""
    filename = join(dirname(__file__), '..', 'resources', 'multiline_file.txt')

    file = yield from aioopen(filename, mode=mode)

    # Append mode needs us to seek.
    yield from file.seek(0)

    counter = 1

    # The recommended iteration pattern:
    while True:
        line = yield from file.readline()
        if not line:
            break
        assert line.strip() == 'line ' + str(counter)
        counter += 1

    yield from file.close()
コード例 #30
0
ファイル: test_binary.py プロジェクト: zsrinivas/aiofiles
async def test_simple_truncate(mode, buffering, tmpdir):
    """Test truncating files."""
    filename = 'bigfile.bin'
    content = b'0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write_binary(content)

    async with aioopen(str(full_file), mode=mode, buffering=buffering) as file:
        # The append modes want us to seek first.
        await file.seek(0)

        if 'w' in mode:
            # We've just erased the entire file.
            await file.write(content)
            await file.flush()
            await file.seek(0)

        await file.truncate()

    assert b'' == full_file.read_binary()
コード例 #31
0
ファイル: test_text_35.py プロジェクト: farakavco/aiofiles
async def test_simple_truncate(mode, tmpdir):
    """Test truncating files."""
    filename = 'bigfile.bin'
    content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)

    async with aioopen(str(full_file), mode=mode) as file:
        # The append modes want us to seek first.
        await file.seek(0)

        if 'w' in mode:
            # We've just erased the entire file.
            await file.write(content)
            await file.flush()
            await file.seek(0)

        await file.truncate()

    assert '' == full_file.read()
コード例 #32
0
ファイル: test_text.py プロジェクト: Eyepea/aiofiles
def test_simple_truncate(mode, tmpdir):
    """Test truncating files."""
    filename = 'bigfile.bin'
    content = '0123456789' * 4 * io.DEFAULT_BUFFER_SIZE

    full_file = tmpdir.join(filename)
    full_file.write(content)

    file = yield from aioopen(str(full_file), mode=mode)

    # The append modes want us to seek first.
    yield from file.seek(0)

    if 'w' in mode:
        # We've just erased the entire file.
        yield from file.write(content)
        yield from file.flush()
        yield from file.seek(0)

    yield from file.truncate()

    yield from file.close()

    assert '' == full_file.read()