145 lines
4.8 KiB
Python
145 lines
4.8 KiB
Python
"""Юнит-тесты для S3-хранилища чекпоинтов."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
from botocore.exceptions import ClientError
|
|
|
|
from src.pipeline.checkpoint import Checkpoint, S3CheckpointStore
|
|
from src.s3.client import S3Client
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_s3():
|
|
client = MagicMock(spec=S3Client)
|
|
client.read_file = AsyncMock()
|
|
client.put_object = AsyncMock()
|
|
client.delete_object = AsyncMock()
|
|
return client
|
|
|
|
|
|
def _not_found() -> ClientError:
|
|
return ClientError(
|
|
{"Error": {"Code": "NoSuchKey", "Message": "not found"}},
|
|
"GetObject",
|
|
)
|
|
|
|
|
|
async def test_load_returns_empty_checkpoint_when_missing(mock_s3):
|
|
mock_s3.read_file.side_effect = _not_found()
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint()
|
|
mock_s3.read_file.assert_awaited_once_with("_state/checkpoints/input/data.csv.json")
|
|
|
|
|
|
async def test_load_old_checkpoint_without_offset(mock_s3):
|
|
"""Старый формат: только rows_processed — resume построчным пропуском."""
|
|
mock_s3.read_file.return_value = b'{"rows_processed": 42}'
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint(rows_processed=42)
|
|
|
|
|
|
async def test_load_checkpoint_with_offset_and_header(mock_s3):
|
|
mock_s3.read_file.return_value = (
|
|
b'{"rows_processed": 42, "byte_offset": 1234, "header": ["id", "content"]}'
|
|
)
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint(
|
|
rows_processed=42,
|
|
byte_offset=1234,
|
|
header=("id", "content"),
|
|
)
|
|
|
|
|
|
async def test_load_offset_without_header_falls_back_to_rows(mock_s3):
|
|
"""Без заголовка нельзя парсить строки с середины — идём с начала."""
|
|
mock_s3.read_file.return_value = b'{"rows_processed": 42, "byte_offset": 1234}'
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint(rows_processed=42)
|
|
|
|
|
|
async def test_load_checkpoint_with_null_offset_falls_back(mock_s3):
|
|
"""null в byte_offset не должен ронять загрузку чекпоинта."""
|
|
mock_s3.read_file.return_value = (
|
|
b'{"rows_processed": 42, "byte_offset": null, "header": ["id"]}'
|
|
)
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint(rows_processed=42)
|
|
|
|
|
|
async def test_load_corrupted_checkpoint_returns_empty(mock_s3):
|
|
mock_s3.read_file.return_value = b"not-a-json"
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == Checkpoint()
|
|
|
|
|
|
async def test_load_reraises_other_s3_errors(mock_s3):
|
|
mock_s3.read_file.side_effect = ClientError(
|
|
{"Error": {"Code": "AccessDenied", "Message": "denied"}},
|
|
"GetObject",
|
|
)
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
with pytest.raises(ClientError):
|
|
await store.load("input/data.csv")
|
|
|
|
|
|
async def test_save_writes_json(mock_s3):
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
await store.save("input/data.csv", Checkpoint(rows_processed=7))
|
|
|
|
key, body = mock_s3.put_object.await_args.args
|
|
assert key == "_state/checkpoints/input/data.csv.json"
|
|
payload = json.loads(body)
|
|
assert payload["source_key"] == "input/data.csv"
|
|
assert payload["rows_processed"] == 7
|
|
assert payload["byte_offset"] == 0
|
|
assert "header" not in payload
|
|
assert "updated_at" in payload
|
|
assert mock_s3.put_object.await_args.kwargs["content_type"] == "application/json"
|
|
|
|
|
|
async def test_save_writes_offset_and_header(mock_s3):
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
await store.save(
|
|
"input/data.csv",
|
|
Checkpoint(rows_processed=7, byte_offset=2048, header=("id", "content")),
|
|
)
|
|
|
|
_, body = mock_s3.put_object.await_args.args
|
|
payload = json.loads(body)
|
|
assert payload["rows_processed"] == 7
|
|
assert payload["byte_offset"] == 2048
|
|
assert payload["header"] == ["id", "content"]
|
|
|
|
|
|
async def test_clear_deletes_checkpoint(mock_s3):
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
await store.clear("input/data.csv")
|
|
|
|
mock_s3.delete_object.assert_awaited_once_with(
|
|
"_state/checkpoints/input/data.csv.json"
|
|
)
|
|
|
|
|
|
async def test_prefix_without_trailing_slash_is_normalized(mock_s3):
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints")
|
|
|
|
await store.save("input/data.csv", Checkpoint(rows_processed=1))
|
|
|
|
assert mock_s3.put_object.await_args.args[0] == (
|
|
"_state/checkpoints/input/data.csv.json"
|
|
)
|