Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/lint-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ permissions:

jobs:
lint-and-test:
uses: esclient/tools/.github/workflows/lint-and-test-python.yml@v1.0.4
uses: esclient/tools/.github/workflows/lint-and-test-python.yml@v1.0.1
with:
python-version: '3.13.7'
python-version: "3.13.7"
source: "modservice"
sonar-inclusions: "src/**,Dockerfile"
sonar-exclusions: "**/grpc/**"
sonar-coverage-exclusions: "src/modservice/server.py,src/modservice/settings.py,src/modservice/s3_client.py"
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ LOAD_ENVS_URL := 'https://raw.githubusercontent.com/esclient/tools/refs/heads/ma
PROTO_TAG := 'v0.1.2'
PROTO_NAME := 'mod.proto'
TMP_DIR := '.proto'
OUT_DIR := 'src/modservice/grpc'
SERVICE_NAME := 'mod'
SOURCE := 'modservice'
OUT_DIR := 'src/' + SOURCE + '/grpc'

MKDIR_TOOLS := 'mkdir -p tools'

Expand Down
295 changes: 293 additions & 2 deletions pdm.lock

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ explicit_package_bases = true
mypy_path = ["src"]
plugins = ["pydantic.mypy"]

[tool.pytest.ini_options]
addopts = "--import-mode=importlib"
asyncio_mode = "strict"
testpaths = ["tests"]
python_files = ["test_*.py"]

[tool.coverage.run]
source = ["src/modservice"]
relative_files = true

[tool.coverage.report]
include = [
"src/modservice/handler/*",
"src/modservice/service/*",
"src/modservice/repository/*",
]
omit = ["src/modservice/server.py"]

[project]
name = "modservice"
version = "0.0.1"
Expand Down Expand Up @@ -121,7 +139,11 @@ dev = [
"grpc-stubs==1.53.0.6",
"protobuf==6.32.1",
"pytest==8.4.2",
"pytest-asyncio==0.24.0",
"pytest-cov==5.0.0",
"pytest-mock==3.15.1",
"pytest-faker==2.0.0",
"Faker==37.11.0",
"black==25.9.0",
"isort==6.0.1",
"flake8==7.3.0",
Expand All @@ -130,4 +152,8 @@ dev = [
"mypy==1.18.2",
"types-protobuf==6.32.1.20250918",
"types-psycopg2==2.9.21.20250915",
"types-aioboto3==15.4.0",
"types-aiofiles==25.1.0.20251011",
"botocore-stubs==1.40.55",
"asyncpg-stubs==0.30.2",
]
2 changes: 1 addition & 1 deletion src/modservice/repository/create_mod.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asyncpg import Pool # type: ignore[import-untyped]
from asyncpg import Pool


async def create_mod(
Expand Down
10 changes: 7 additions & 3 deletions src/modservice/repository/get_mod_s3_key.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from asyncpg import Pool # type: ignore[import-untyped]
from typing import Literal

from asyncpg import Pool

async def get_mod_s3_key(db_pool: Pool, id: int) -> int:

async def get_mod_s3_key(db_pool: Pool, id: int) -> str | Literal[0]:
async with db_pool.acquire() as conn:
s3_key = await conn.fetchval(
"""
Expand All @@ -12,4 +14,6 @@ async def get_mod_s3_key(db_pool: Pool, id: int) -> int:
""",
id,
)
return s3_key if s3_key else 0
if s3_key is None:
return 0
return str(s3_key)
2 changes: 1 addition & 1 deletion src/modservice/repository/get_mods.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from asyncpg import Pool # type: ignore[import-untyped]
from asyncpg import Pool


async def get_mods(
Expand Down
2 changes: 1 addition & 1 deletion src/modservice/repository/insert_s3_key.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asyncpg import Pool # type: ignore[import-untyped]
from asyncpg import Pool


def generate_s3_key(author_id: int, mod_id: int) -> str:
Expand Down
2 changes: 1 addition & 1 deletion src/modservice/repository/repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any

from asyncpg import Pool # type: ignore[import-untyped]
from asyncpg import Pool

from modservice.repository.create_mod import create_mod as _create_mod
from modservice.repository.get_mod_s3_key import (
Expand Down
2 changes: 1 addition & 1 deletion src/modservice/repository/set_status.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asyncpg import Pool # type: ignore[import-untyped]
from asyncpg import Pool


async def set_status(db_pool: Pool, mod_id: int, status: str) -> bool:
Expand Down
24 changes: 14 additions & 10 deletions src/modservice/s3_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from typing import Any

import aioboto3
from botocore.config import Config
import aiofiles
from aiobotocore.config import AioConfig

logger = logging.getLogger(__name__)

Expand All @@ -22,7 +23,7 @@ def __init__(
self.bucket_name = bucket_name
self.ssl_verify = verify

self.config = Config(
self.config = AioConfig(
signature_version="s3v4",
s3={"addressing_style": "virtual"},
region_name="ru-central-1",
Expand Down Expand Up @@ -51,11 +52,14 @@ async def upload_file(self, file_path: str, s3_key: str) -> bool:
try:
logger.info(f"Загружаем файл {file_path} как {s3_key}")

async with self.get_client() as client:
with open(file_path, "rb") as file:
await client.put_object(
Bucket=self.bucket_name, Key=s3_key, Body=file.read()
)
async with (
self.get_client() as client,
aiofiles.open(file_path, "rb") as file,
):
payload = await file.read()
await client.put_object(
Bucket=self.bucket_name, Key=s3_key, Body=payload
)

logger.info(f"Файл успешно загружен: {s3_key}")
return True
Expand All @@ -79,16 +83,16 @@ async def download_file(self, s3_key: str, local_path: str) -> bool:
async with response["Body"] as stream:
content = await stream.read()

with open(local_path, "wb") as file:
file.write(content)
async with aiofiles.open(local_path, "wb") as file:
await file.write(content)

return True

except Exception as e:
logger.error(f"Ошибка при скачивании файла {s3_key}: {e!s}")
return False

def time_format(self, seconds: int) -> str:
def time_format(self, seconds: int | None) -> str:
if seconds is not None:
seconds = int(seconds)
d = seconds // (3600 * 24)
Expand Down
37 changes: 35 additions & 2 deletions tests/handler/test_create_mod.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
from unittest.mock import AsyncMock

import grpc
import pytest
from faker import Faker
from pytest_mock import MockerFixture

from modservice.grpc import mod_pb2
from modservice.handler.create_mod import CreateMod
from modservice.service.service import ModService


@pytest.mark.asyncio
async def test_create_mod_returns_response(
mocker: MockerFixture, faker: Faker
) -> None:
context = mocker.Mock(spec=grpc.ServicerContext)
service = mocker.Mock(spec=ModService)
mod_id = faker.random_int(min=1, max=100000)
s3_key = f"{faker.random_int(min=1, max=100000)}/{mod_id}"
upload_url = faker.uri()
service.create_mod = AsyncMock(return_value=(mod_id, s3_key, upload_url))

request = mod_pb2.CreateModRequest(
title=faker.sentence(nb_words=4),
author_id=faker.random_int(min=1, max=100000),
description=faker.text(),
)

response = await CreateMod(service, request, context)

def test_create_mod_success(mocker: MockerFixture) -> None:
assert True
assert isinstance(response, mod_pb2.CreateModResponse)
assert response.mod_id == mod_id
assert response.s3_key == s3_key
assert response.upload_url == upload_url
service.create_mod.assert_awaited_once_with(
request.title, request.author_id, request.description
)
28 changes: 28 additions & 0 deletions tests/handler/test_get_download_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from unittest.mock import AsyncMock

import grpc
import pytest
from faker import Faker
from pytest_mock import MockerFixture

from modservice.grpc import mod_pb2
from modservice.handler.get_mod_download_link import GetDownloadLink
from modservice.service.service import ModService


@pytest.mark.asyncio
async def test_get_download_link_returns_url(
mocker: MockerFixture, faker: Faker
) -> None:
context = mocker.Mock(spec=grpc.ServicerContext)
service = mocker.Mock(spec=ModService)
url = faker.uri()
service.get_mod_download_link = AsyncMock(return_value=url)

mod_id = faker.random_int(min=1, max=100000)
request = mod_pb2.GetModDownloadLinkRequest(mod_id=mod_id)
response = await GetDownloadLink(service, request, context)

assert isinstance(response, mod_pb2.GetModDownloadLinkResponse)
assert response.link_url == url
service.get_mod_download_link.assert_awaited_once_with(mod_id)
75 changes: 75 additions & 0 deletions tests/handler/test_get_mods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from datetime import UTC, datetime
from unittest.mock import AsyncMock

import grpc
import pytest
from faker import Faker
from google.protobuf.timestamp_pb2 import Timestamp
from pytest_mock import MockerFixture

from modservice.constants import STATUS_BANNED, STATUS_UPLOADED
from modservice.grpc import mod_pb2
from modservice.handler.get_mods import GetMods
from modservice.service.service import ModService


def _ts_from_datetime(dt: datetime) -> Timestamp:
ts = Timestamp()
ts.FromDatetime(dt)
return ts


@pytest.mark.asyncio
async def test_get_mods_converts_models(
mocker: MockerFixture, faker: Faker
) -> None:
context = mocker.Mock(spec=grpc.ServicerContext)
service = mocker.Mock(spec=ModService)

created_at_first = faker.date_time(tzinfo=UTC)
created_at_second = faker.date_time(tzinfo=UTC)

mods_data = [
{
"id": faker.random_int(min=1, max=100000),
"author_id": faker.random_int(min=1, max=100000),
"title": faker.sentence(nb_words=3),
"description": faker.text(),
"version": faker.random_int(min=1, max=10),
"s3_key": faker.file_path(depth=2),
"status": STATUS_UPLOADED,
"created_at": _ts_from_datetime(created_at_first),
},
{
"id": faker.random_int(min=1, max=100000),
"author_id": faker.random_int(min=1, max=100000),
"title": faker.sentence(nb_words=4),
"description": faker.text(),
"version": faker.random_int(min=1, max=10),
"s3_key": faker.file_path(depth=2),
"status": STATUS_BANNED,
"created_at": _ts_from_datetime(created_at_second),
},
]
service.get_mods = AsyncMock(return_value=mods_data)

request = mod_pb2.GetModsRequest()
response = await GetMods(service, request, context)

assert isinstance(response, mod_pb2.GetModsResponse)
assert len(response.mods) == 2

first_mod = response.mods[0]
assert first_mod.id == mods_data[0]["id"]
assert first_mod.author_id == mods_data[0]["author_id"]
assert first_mod.title == mods_data[0]["title"]
assert first_mod.description == mods_data[0]["description"]
assert first_mod.version == mods_data[0]["version"]
assert first_mod.status == mod_pb2.ModStatus.MOD_STATUS_UPLOADED
assert first_mod.created_at == mods_data[0]["created_at"]

second_mod = response.mods[1]
assert second_mod.status == mod_pb2.ModStatus.MOD_STATUS_BANNED
assert second_mod.created_at == mods_data[1]["created_at"]

service.get_mods.assert_awaited_once_with()
Loading
Loading