Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Фельдман Эмилий Олегович committed Nov 13, 2022
1 parent a39fffa commit adab5e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion service/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
super().__init__()


class UserNotFoundException(AppException):
class UserNotFoundError(AppException):
def __init__(
self,
status_code: int = HTTPStatus.NOT_FOUND,
Expand Down
9 changes: 8 additions & 1 deletion service/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from fastapi import APIRouter, FastAPI, Request
from pydantic import BaseModel

from service.api.exceptions import UserNotFoundError
from service.log import app_logger


Expand Down Expand Up @@ -30,9 +31,15 @@ async def health() -> str:
async def get_reco(
request: Request,
model_name: str,
user_id: str,
user_id: int,
) -> RecoResponse:
app_logger.info(f"Request for model: {model_name}, user_id: {user_id}")

# Write your code here

if user_id > 10**9:
raise UserNotFoundError(error_message=f"User {user_id} not found")

k_recs = request.app.state.k_recs
reco = list(range(k_recs))
return RecoResponse(user_id=user_id, items=reco)
Expand Down
20 changes: 13 additions & 7 deletions tests/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from starlette.testclient import TestClient

GET_RECO_PATH = "/reco/{user_id}"
from service.settings import ServiceConfig

GET_RECO_PATH = "/reco/{model_name}/{user_id}"


def test_health(
Expand All @@ -15,20 +17,24 @@ def test_health(

def test_get_reco_success(
client: TestClient,
service_config: ServiceConfig,
) -> None:
user_id = "123"
path = GET_RECO_PATH.format(user_id=user_id)
user_id = 123
path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id)
with client:
response = client.get(path)
assert response.status_code == HTTPStatus.OK
assert response.json() == {"items": ["123a", "123b"]}
response_json = response.json()
assert response_json["user_id"] == user_id
assert len(response_json["items"]) == service_config.k_recs
assert all(isinstance(item_id, int) for item_id in response_json["items"])


def test_get_reco_404(
def test_get_reco_for_unknown_user(
client: TestClient,
) -> None:
user_id = "1234"
path = GET_RECO_PATH.format(user_id=user_id)
user_id = 10**10
path = GET_RECO_PATH.format(model_name="some_model", user_id=user_id)
with client:
response = client.get(path)
assert response.status_code == HTTPStatus.NOT_FOUND
Expand Down

0 comments on commit adab5e3

Please sign in to comment.