Example #1
0
async def test_staggered_read(mode: str) -> None:
    """Read bytes repeatedly."""
    filename = join(dirname(__file__), "resources", "test_file1.txt")
    async with aiopen(filename, mode=mode) as file:
        await file.seek(0)  # Needed for the append mode.

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

        assert "" == (await 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

    assert file.closed
Example #2
0
async def test_staggered_read(mode: str, buffering: int) -> None:
    """Read bytes repeatedly."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")
    async with aiopen(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
Example #3
0
async def test_simple_iteration(mode: str) -> None:
    """Test iterating over lines from a file."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")

    async with aiopen(filename, mode=mode) 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() == "line " + str(counter)
            counter += 1

        await file.seek(0)
        counter = 1

        # The new iteration pattern:
        async for line in file:
            assert line.strip() == "line " + str(counter)
            counter += 1

    assert file.closed
Example #4
0
async def test_simple_read(mode: str, buffering: int) -> None:
    """Just read some bytes from a test file."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")
    async with aiopen(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()
Example #5
0
async def test_simple_readinto(mode: str, buffering: int) -> None:
    """Test the readinto functionality."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")
    async with aiopen(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)
Example #6
0
async def test_simple_read(mode: str) -> None:
    """Just read some bytes from a test file."""
    filename = join(dirname(__file__), "resources", "test_file1.txt")
    async with aiopen(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
Example #7
0
async def test_simple_seek(mode: str, tmpdir: LocalPath) -> None:
    """Test seeking and then reading."""
    filename = "bigfile.bin"
    content = "0123456789" * 4 * io.DEFAULT_BUFFER_SIZE

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

    async with aiopen(str(full_file), mode=mode) as file:
        await file.seek(4)
        assert "4" == (await file.read(1))

    assert file.closed
Example #8
0
async def test_simple_close(mode: str, tmpdir: LocalPath) -> None:
    """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)

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

    assert file.closed
    assert file._file.closed
Example #9
0
async def test_simple_readlines(mode: str, buffering: int) -> None:
    """Test the readlines functionality."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")

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

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

        actual = await file.readlines()

    assert actual == expected
Example #10
0
async def test_simple_read_fail(mode: str, tmpdir: LocalPath) -> None:
    """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 aiopen(str(full_file), mode=mode) as file:
            await file.seek(0)  # Needed for the append mode.

            await file.read()

    assert file.closed
Example #11
0
async def test_simple_iteration_ctx_mgr(mode: str) -> None:
    """Test iterating over lines from a file."""
    filename = join(dirname(__file__), "resources", "multiline_file.txt")

    async with aiopen(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
Example #12
0
async def test_simple_write(mode: str, tmpdir: LocalPath) -> None:
    """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.

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

    assert bytes_written == len(content)
    assert content == full_file.read()
    assert file.closed
Example #13
0
async def test_simple_detach(tmpdir: LocalPath) -> None:
    """Test detaching for buffered streams."""
    filename = "file.bin"

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

    with pytest.raises(ValueError):  # Close will error out.
        async with aiopen(str(full_file), mode="r") as file:
            raw_file = file.detach()

            assert raw_file

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

            assert b"0123456789" == raw_file.read(10)
Example #14
0
async def test_simple_flush(mode: str, tmpdir: LocalPath) -> None:
    """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 aiopen(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
Example #15
0
async def test_simple_peek(mode: str, tmpdir: LocalPath) -> None:
    """Test flushing to a file."""
    filename = "file.bin"

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

    async with aiopen(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)
Example #16
0
async def test_simple_truncate(mode: str, tmpdir: LocalPath) -> None:
    """Test truncating files."""
    filename = "bigfile.bin"
    content = "0123456789" * 4 * io.DEFAULT_BUFFER_SIZE

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

    async with aiopen(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()
Example #17
0
async def test_simple_flush(mode: str, buffering: int,
                            tmpdir: LocalPath) -> None:
    """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 aiopen(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()