96 lines
2.8 KiB
Python
96 lines
2.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 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_zero_when_checkpoint_missing(mock_s3):
|
|
mock_s3.read_file.side_effect = _not_found()
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == 0
|
|
mock_s3.read_file.assert_awaited_once_with("_state/checkpoints/input/data.csv.json")
|
|
|
|
|
|
async def test_load_returns_saved_rows(mock_s3):
|
|
mock_s3.read_file.return_value = b'{"rows_processed": 42}'
|
|
store = S3CheckpointStore(mock_s3, "_state/checkpoints/")
|
|
|
|
assert await store.load("input/data.csv") == 42
|
|
|
|
|
|
async def test_load_corrupted_checkpoint_returns_zero(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") == 0
|
|
|
|
|
|
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", 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 "updated_at" in payload
|
|
assert mock_s3.put_object.await_args.kwargs["content_type"] == "application/json"
|
|
|
|
|
|
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", 1)
|
|
|
|
assert mock_s3.put_object.await_args.args[0] == (
|
|
"_state/checkpoints/input/data.csv.json"
|
|
)
|