Skip to content

Commit

Permalink
Merge pull request #29 from profcomff/26-добавить-ограничение-на-поля…
Browse files Browse the repository at this point in the history
…-оценок

Ограничения на поля оценок
  • Loading branch information
Temmmmmo authored Nov 7, 2024
2 parents 0f67d7f + 6cdeaef commit cd8370b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
7 changes: 7 additions & 0 deletions rating_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ def __init__(self, dtime: datetime.timedelta):
class ForbiddenAction(RatingAPIError):
def __init__(self, type: Type):
super().__init__(f"Forbidden action with {type.__name__}", f"Запрещенное действие с объектом {type.__name__}")


class WrongMark(RatingAPIError):
def __init__(self):
super().__init__(
f"Ratings can only take values: -2, -1, 0, 1, 2", f"Оценки могут принимать только значения: -2, -1, 0, 1, 2"
)
9 changes: 8 additions & 1 deletion rating_api/routes/exc_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import starlette.requests
from starlette.responses import JSONResponse

from rating_api.exceptions import AlreadyExists, ForbiddenAction, ObjectNotFound, TooManyCommentRequests
from rating_api.exceptions import AlreadyExists, ForbiddenAction, ObjectNotFound, TooManyCommentRequests, WrongMark
from rating_api.schemas.base import StatusResponseModel

from .base import app
Expand Down Expand Up @@ -33,3 +33,10 @@ async def forbidden_action_handler(req: starlette.requests.Request, exc: Already
return JSONResponse(
content=StatusResponseModel(status="Error", message=exc.eng, ru=exc.ru).model_dump(), status_code=403
)


@app.exception_handler(WrongMark)
async def wrong_mark_handler(req: starlette.requests.Request, exc: WrongMark):
return JSONResponse(
content=StatusResponseModel(status="Error", message=exc.eng, ru=exc.ru).model_dump(), status_code=400
)
10 changes: 10 additions & 0 deletions rating_api/schemas/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
from uuid import UUID

from pydantic import field_validator

from rating_api.exceptions import WrongMark
from rating_api.schemas.base import Base


Expand All @@ -23,6 +26,13 @@ class CommentPost(Base):
mark_freebie: int
mark_clarity: int

@field_validator('mark_kindness', 'mark_freebie', 'mark_clarity')
@classmethod
def validate_mark(cls, value):
if value not in [-2, -1, 0, 1, 2]:
raise WrongMark()
return value


class CommentGetAll(Base):
comments: list[CommentGet] = []
Expand Down
20 changes: 20 additions & 0 deletions tests/test_routes/test_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ def test_create_comment(client, dbsession):
assert post_response.status_code == status.HTTP_404_NOT_FOUND


def test_post_bad_mark(client, dbsession):
body = {"first_name": 'Иван', "last_name": 'Иванов', "middle_name": 'Иванович', "timetable_id": 0}
lecturer: Lecturer = Lecturer(**body)
dbsession.add(lecturer)
dbsession.commit()

body = {
"subject": "Физика",
"text": "Хороший препод",
"mark_kindness": 4,
"mark_freebie": -2,
"mark_clarity": 0,
}
params = {"lecturer_id": lecturer.id}
post_response = client.post(url, json=body, params=params)
assert post_response.status_code == status.HTTP_400_BAD_REQUEST
dbsession.delete(lecturer)
dbsession.commit()


def test_get_comment(client, dbsession):
body = {
"first_name": 'Иван',
Expand Down

0 comments on commit cd8370b

Please sign in to comment.