init
This commit is contained in:
1 parent
84ae2122b7
commit
3c4e8e6cee
54 files changed
+4329
No files matched your search
@@ -0,0 +1,227 @@
|
||||
"""Юнит-тесты для S3-клиента."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from src.config.settings import Settings
|
||||
from src.s3.client import S3Client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def settings() -> Settings:
|
||||
return Settings(
|
||||
s3_endpoint_url="http://localhost:9000",
|
||||
s3_access_key_id="minioadmin",
|
||||
s3_secret_access_key="minioadmin",
|
||||
s3_bucket="test-bucket",
|
||||
s3_prefix="input/",
|
||||
s3_processed_prefix="processed/",
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_s3_client_obj(mocker, settings):
|
||||
"""Патчит aioboto3.Session и возвращает мок S3-клиента."""
|
||||
mock_client = MagicMock()
|
||||
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
|
||||
mock_client.__aexit__ = AsyncMock(return_value=None)
|
||||
|
||||
mock_session = MagicMock()
|
||||
mock_session.client = MagicMock(return_value=mock_client)
|
||||
|
||||
mocker.patch("src.s3.client.aioboto3.Session", return_value=mock_session)
|
||||
return mock_client
|
||||
|
||||
|
||||
# --- Тесты lifecycle ---
|
||||
|
||||
|
||||
async def test_start_creates_client(mock_s3_client_obj, settings):
|
||||
client = S3Client(settings)
|
||||
|
||||
await client.start()
|
||||
|
||||
assert client._client is not None
|
||||
mock_s3_client_obj.__aenter__.assert_awaited_once()
|
||||
|
||||
|
||||
async def test_stop_closes_client(mock_s3_client_obj, settings):
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
await client.stop()
|
||||
|
||||
mock_s3_client_obj.__aexit__.assert_awaited_once()
|
||||
assert client._client is None
|
||||
|
||||
|
||||
async def test_stop_when_not_started(settings):
|
||||
client = S3Client(settings)
|
||||
# Не должно выбросить исключение
|
||||
await client.stop()
|
||||
|
||||
|
||||
async def test_context_manager(mock_s3_client_obj, settings):
|
||||
async with S3Client(settings) as client:
|
||||
assert client._client is not None
|
||||
assert client._client is None
|
||||
|
||||
|
||||
# --- Тесты list_json_files ---
|
||||
|
||||
|
||||
async def test_list_json_files_returns_only_json(mock_s3_client_obj, settings):
|
||||
"""Возвращает только .json файлы из prefix."""
|
||||
mock_paginator = MagicMock()
|
||||
mock_page = {
|
||||
"Contents": [
|
||||
{"Key": "input/file1.json"},
|
||||
{"Key": "input/file2.json"},
|
||||
{"Key": "input/readme.txt"},
|
||||
{"Key": "input/data.csv"},
|
||||
]
|
||||
}
|
||||
|
||||
async def async_paginate(*args, **kwargs):
|
||||
yield mock_page
|
||||
|
||||
mock_paginator.paginate = async_paginate
|
||||
mock_s3_client_obj.get_paginator = MagicMock(return_value=mock_paginator)
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
keys = await client.list_json_files()
|
||||
|
||||
assert keys == ["input/file1.json", "input/file2.json"]
|
||||
|
||||
|
||||
async def test_list_json_files_empty_prefix(mock_s3_client_obj, settings):
|
||||
"""Пустой prefix → пустой список."""
|
||||
mock_paginator = MagicMock()
|
||||
|
||||
async def async_paginate(*args, **kwargs):
|
||||
yield {"Contents": []}
|
||||
|
||||
mock_paginator.paginate = async_paginate
|
||||
mock_s3_client_obj.get_paginator = MagicMock(return_value=mock_paginator)
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
keys = await client.list_json_files()
|
||||
|
||||
assert keys == []
|
||||
|
||||
|
||||
async def test_list_json_files_ignores_non_json(mock_s3_client_obj, settings):
|
||||
"""Игнорирует файлы без .json расширения."""
|
||||
mock_paginator = MagicMock()
|
||||
|
||||
async def async_paginate(*args, **kwargs):
|
||||
yield {
|
||||
"Contents": [
|
||||
{"Key": "input/sub/dir/"},
|
||||
{"Key": "input/file.json"},
|
||||
{"Key": "input/file.json.bak"},
|
||||
]
|
||||
}
|
||||
|
||||
mock_paginator.paginate = async_paginate
|
||||
mock_s3_client_obj.get_paginator = MagicMock(return_value=mock_paginator)
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
keys = await client.list_json_files()
|
||||
|
||||
assert keys == ["input/file.json"]
|
||||
|
||||
|
||||
async def test_list_json_files_raises_when_not_started(settings):
|
||||
client = S3Client(settings)
|
||||
|
||||
with pytest.raises(RuntimeError, match="S3-клиент не запущен"):
|
||||
await client.list_json_files()
|
||||
|
||||
|
||||
# --- Тесты read_file ---
|
||||
|
||||
|
||||
async def test_read_file_returns_bytes(mock_s3_client_obj, settings):
|
||||
"""Возвращает bytes содержимого файла."""
|
||||
mock_body = MagicMock()
|
||||
mock_body.read = AsyncMock(return_value=b'{"text": "hello"}')
|
||||
|
||||
mock_s3_client_obj.get_object = AsyncMock(
|
||||
return_value={"Body": mock_body}
|
||||
)
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
data = await client.read_file("input/file.json")
|
||||
|
||||
assert data == b'{"text": "hello"}'
|
||||
mock_s3_client_obj.get_object.assert_awaited_once_with(
|
||||
Bucket="test-bucket",
|
||||
Key="input/file.json",
|
||||
)
|
||||
|
||||
|
||||
async def test_read_file_raises_when_not_started(settings):
|
||||
client = S3Client(settings)
|
||||
|
||||
with pytest.raises(RuntimeError, match="S3-клиент не запущен"):
|
||||
await client.read_file("input/file.json")
|
||||
|
||||
|
||||
# --- Тесты move_file ---
|
||||
|
||||
|
||||
async def test_move_file_copies_and_deletes(mock_s3_client_obj, settings):
|
||||
"""Copy в processed_prefix + delete оригинала."""
|
||||
mock_s3_client_obj.copy_object = AsyncMock()
|
||||
mock_s3_client_obj.delete_object = AsyncMock()
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
await client.move_file("input/file.json")
|
||||
|
||||
mock_s3_client_obj.copy_object.assert_awaited_once_with(
|
||||
Bucket="test-bucket",
|
||||
Key="processed/file.json",
|
||||
CopySource={"Bucket": "test-bucket", "Key": "input/file.json"},
|
||||
)
|
||||
mock_s3_client_obj.delete_object.assert_awaited_once_with(
|
||||
Bucket="test-bucket",
|
||||
Key="input/file.json",
|
||||
)
|
||||
|
||||
|
||||
async def test_move_file_with_nested_key(mock_s3_client_obj, settings):
|
||||
"""Корректное извлечение имени файла из вложенного ключа."""
|
||||
mock_s3_client_obj.copy_object = AsyncMock()
|
||||
mock_s3_client_obj.delete_object = AsyncMock()
|
||||
|
||||
client = S3Client(settings)
|
||||
await client.start()
|
||||
|
||||
await client.move_file("input/sub/dir/file.json")
|
||||
|
||||
mock_s3_client_obj.copy_object.assert_awaited_once_with(
|
||||
Bucket="test-bucket",
|
||||
Key="processed/file.json",
|
||||
CopySource={"Bucket": "test-bucket", "Key": "input/sub/dir/file.json"},
|
||||
)
|
||||
|
||||
|
||||
async def test_move_file_raises_when_not_started(settings):
|
||||
client = S3Client(settings)
|
||||
|
||||
with pytest.raises(RuntimeError, match="S3-клиент не запущен"):
|
||||
await client.move_file("input/file.json")
|
||||
Reference in new issue
Block a user