Pillow/Tests/test_file_container.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
5.2 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2022-08-23 14:41:32 +03:00
import pytest
from PIL import ContainerIO, Image
2017-03-01 12:20:18 +03:00
2017-03-05 01:46:30 +03:00
TEST_FILE = "Tests/images/dummy.container"
2017-03-01 12:20:18 +03:00
def test_sanity() -> None:
dir(Image)
dir(ContainerIO)
2017-03-01 12:20:18 +03:00
2017-03-05 01:46:30 +03:00
def test_isatty() -> None:
2024-09-09 15:34:46 +03:00
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 0, 0)
assert container.isatty() is False
2024-07-17 09:23:29 +03:00
def test_seekable() -> None:
2024-09-09 15:34:46 +03:00
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 0, 0)
2024-07-17 09:23:29 +03:00
assert container.seekable() is True
2024-03-01 17:47:50 +03:00
@pytest.mark.parametrize(
2024-03-02 05:12:17 +03:00
"mode, expected_position",
2024-03-01 17:47:50 +03:00
(
(0, 33),
(1, 66),
(2, 100),
),
)
2024-07-17 09:23:29 +03:00
def test_seek_mode(mode: int, expected_position: int) -> None:
# Arrange
with open(TEST_FILE, "rb") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
# Act
container.seek(33, mode)
container.seek(33, mode)
# Assert
2024-03-02 05:12:17 +03:00
assert container.tell() == expected_position
2024-07-17 09:23:29 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
def test_readable(bytesmode: bool) -> None:
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
assert container.readable() is True
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
2024-01-31 13:55:32 +03:00
def test_read_n0(bytesmode: bool) -> None:
# Arrange
2022-08-23 14:41:32 +03:00
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
2022-08-23 14:41:32 +03:00
# Act
2024-07-17 09:23:29 +03:00
assert container.seek(81) == 81
2022-08-23 14:41:32 +03:00
data = container.read()
2022-08-23 14:41:32 +03:00
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nThis is line 8\n"
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
2024-01-31 13:55:32 +03:00
def test_read_n(bytesmode: bool) -> None:
# Arrange
2022-08-23 14:41:32 +03:00
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
2022-08-23 14:41:32 +03:00
# Act
2024-07-17 09:23:29 +03:00
assert container.seek(81) == 81
2022-08-23 14:41:32 +03:00
data = container.read(3)
2022-08-23 14:41:32 +03:00
# Assert
if bytesmode:
data = data.decode()
assert data == "7\nT"
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
2024-01-31 13:55:32 +03:00
def test_read_eof(bytesmode: bool) -> None:
# Arrange
2022-08-23 14:41:32 +03:00
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
2022-08-23 14:41:32 +03:00
# Act
2024-07-17 09:23:29 +03:00
assert container.seek(100) == 100
2022-08-23 14:41:32 +03:00
data = container.read()
2022-08-23 14:41:32 +03:00
# Assert
if bytesmode:
data = data.decode()
assert data == ""
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
2024-01-31 13:55:32 +03:00
def test_readline(bytesmode: bool) -> None:
2022-08-23 14:41:32 +03:00
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
2022-08-23 14:41:32 +03:00
data = container.readline()
if bytesmode:
data = data.decode()
assert data == "This is line 1\n"
2024-07-17 09:23:29 +03:00
data = container.readline(4)
if bytesmode:
data = data.decode()
assert data == "This"
2022-08-23 14:41:32 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
2024-01-31 13:55:32 +03:00
def test_readlines(bytesmode: bool) -> None:
2024-07-17 09:23:29 +03:00
expected = [
"This is line 1\n",
"This is line 2\n",
"This is line 3\n",
"This is line 4\n",
"This is line 5\n",
"This is line 6\n",
"This is line 7\n",
"This is line 8\n",
]
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
data = container.readlines()
if bytesmode:
data = [line.decode() for line in data]
assert data == expected
assert container.seek(0) == 0
data = container.readlines(2)
if bytesmode:
data = [line.decode() for line in data]
assert data == expected[:2]
@pytest.mark.parametrize("bytesmode", (True, False))
def test_write(bytesmode: bool) -> None:
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
assert container.writable() is False
with pytest.raises(NotImplementedError):
container.write(b"" if bytesmode else "")
with pytest.raises(NotImplementedError):
container.writelines([])
with pytest.raises(NotImplementedError):
container.truncate()
@pytest.mark.parametrize("bytesmode", (True, False))
def test_iter(bytesmode: bool) -> None:
# Arrange
2022-08-23 14:41:32 +03:00
expected = [
"This is line 1\n",
"This is line 2\n",
"This is line 3\n",
"This is line 4\n",
"This is line 5\n",
"This is line 6\n",
"This is line 7\n",
"This is line 8\n",
]
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
# Act
2024-07-17 09:23:29 +03:00
data = []
for line in container:
data.append(line)
2022-08-23 14:41:32 +03:00
# Assert
if bytesmode:
data = [line.decode() for line in data]
assert data == expected
2024-07-17 09:23:29 +03:00
@pytest.mark.parametrize("bytesmode", (True, False))
def test_file(bytesmode: bool) -> None:
with open(TEST_FILE, "rb" if bytesmode else "r") as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
assert isinstance(container.fileno(), int)
container.flush()
container.close()