2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
import pytest
|
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from PIL import ContainerIO, Image
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
from .helper import hopper
|
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
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
dir(Image)
|
|
|
|
dir(ContainerIO)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2017-03-05 01:46:30 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_isatty() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
with hopper() as im:
|
|
|
|
container = ContainerIO.ContainerIO(im, 0, 0)
|
|
|
|
|
|
|
|
assert container.isatty() is False
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_mode_0() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
# Arrange
|
|
|
|
mode = 0
|
2019-11-03 23:51:40 +03:00
|
|
|
with open(TEST_FILE, "rb") as fh:
|
2019-11-04 01:18:55 +03:00
|
|
|
container = ContainerIO.ContainerIO(fh, 22, 100)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
container.seek(33, mode)
|
|
|
|
container.seek(33, mode)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert container.tell() == 33
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_mode_1() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
# Arrange
|
|
|
|
mode = 1
|
2019-11-03 23:51:40 +03:00
|
|
|
with open(TEST_FILE, "rb") as fh:
|
2019-11-04 01:18:55 +03:00
|
|
|
container = ContainerIO.ContainerIO(fh, 22, 100)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
container.seek(33, mode)
|
|
|
|
container.seek(33, mode)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert container.tell() == 66
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_seek_mode_2() -> None:
|
2019-11-04 01:18:55 +03:00
|
|
|
# Arrange
|
|
|
|
mode = 2
|
2019-11-03 23:51:40 +03:00
|
|
|
with open(TEST_FILE, "rb") as fh:
|
2019-11-04 01:18:55 +03:00
|
|
|
container = ContainerIO.ContainerIO(fh, 22, 100)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
container.seek(33, mode)
|
|
|
|
container.seek(33, mode)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert container.tell() == 100
|
|
|
|
|
|
|
|
|
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:
|
2019-11-04 01:18:55 +03:00
|
|
|
# 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)
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
|
|
|
container.seek(81)
|
|
|
|
data = container.read()
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
if bytesmode:
|
|
|
|
data = data.decode()
|
|
|
|
assert data == "7\nThis is line 8\n"
|
2019-11-04 01:18:55 +03:00
|
|
|
|
|
|
|
|
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:
|
2019-11-04 01:18:55 +03:00
|
|
|
# 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)
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
|
|
|
container.seek(81)
|
|
|
|
data = container.read(3)
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
if bytesmode:
|
|
|
|
data = data.decode()
|
|
|
|
assert data == "7\nT"
|
2019-11-04 01:18:55 +03:00
|
|
|
|
|
|
|
|
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:
|
2019-11-04 01:18:55 +03:00
|
|
|
# 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)
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
|
|
|
container.seek(100)
|
|
|
|
data = container.read()
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
if bytesmode:
|
|
|
|
data = data.decode()
|
|
|
|
assert data == ""
|
2019-11-04 01:18:55 +03:00
|
|
|
|
|
|
|
|
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:
|
2019-11-04 01:18:55 +03:00
|
|
|
# Arrange
|
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)
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
|
|
|
data = container.readline()
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
if bytesmode:
|
|
|
|
data = data.decode()
|
|
|
|
assert data == "This is line 1\n"
|
2019-11-04 01:18:55 +03:00
|
|
|
|
|
|
|
|
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:
|
2019-11-04 01:18:55 +03:00
|
|
|
# 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
|
|
|
|
data = container.readlines()
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
if bytesmode:
|
|
|
|
data = [line.decode() for line in data]
|
|
|
|
assert data == expected
|