1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
"""PEP 0492/Python 3.5+ tests for text files."""
import io
from os.path import dirname, join
import pytest
from aiofiles.threadpool import open as aioopen
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async def test_simple_iteration(mode):
"""Test iterating over lines from a file."""
filename = join(dirname(__file__), "..", "resources", "multiline_file.txt")
async with aioopen(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
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async def test_simple_readlines(mode):
"""Test the readlines functionality."""
filename = join(dirname(__file__), "..", "resources", "multiline_file.txt")
with open(filename) 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
@pytest.mark.parametrize("mode", ["r+", "w", "a"])
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
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
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).read()
assert file.closed
@pytest.mark.parametrize("mode", ["w", "a"])
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
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async def test_staggered_read(mode):
"""Read bytes repeatedly."""
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 = []
while True:
char = await file.read(1)
if char:
actual.append(char)
else:
break
assert "" == (await file.read())
expected = []
with open(filename) as f:
while True:
char = f.read(1)
if char:
expected.append(char)
else:
break
assert actual == expected
assert file.closed
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async 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)
async with aioopen(str(full_file), mode=mode) as file:
await file.seek(4)
assert "4" == (await file.read(1))
assert file.closed
@pytest.mark.parametrize("mode", ["w", "r", "r+", "w+", "a", "a+"])
async 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)
async with aioopen(str(full_file), mode=mode) as file:
assert not file.closed
assert not file._file.closed
assert file.closed
assert file._file.closed
@pytest.mark.parametrize("mode", ["r+", "w", "a+"])
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()
@pytest.mark.parametrize("mode", ["w", "r+", "w+", "a", "a+"])
async 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.
async with aioopen(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
async def test_simple_detach(tmpdir):
"""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 aioopen(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)
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
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
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async def test_name_property(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 file.name == filename
assert file.closed
@pytest.mark.parametrize("mode", ["r", "r+", "a+"])
async def test_mode_property(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 file.mode == mode
assert file.closed
|