Skip to content

Commit

Permalink
Add route to remove achievment (#7)
Browse files Browse the repository at this point in the history
* Add delet route to achievements

* Add tests for delete route

* Change scope for delete route

* Test fix (with potential bug)

* Test fix for delete route

* Fix test delete route

* Fix doc in delete route

* Fix test format

* Fix response for delete route

* Minor syntax fix
  • Loading branch information
DaymasS authored Jul 21, 2024
1 parent 94de3cc commit e3d25f3
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions achievement_api/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from pydantic import BaseModel, ConfigDict


class Base(BaseModel):
def __repr__(self) -> str:
attrs = []
for k, v in self.__class__.model_json_schema().items():
attrs.append(f"{k}={v}")
return "{}({})".format(self.__class__.__name__, ', '.join(attrs))

model_config = ConfigDict(from_attributes=True)


class StatusResponseModel(Base):
status: str
message: str
ru: str
13 changes: 13 additions & 0 deletions achievement_api/routes/achievement.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from fastapi_sqlalchemy import db
from pydantic import BaseModel, ConfigDict

from achievement_api.base import StatusResponseModel
from achievement_api.models import Achievement
from achievement_api.settings import get_settings
from achievement_api.utils.image import get_image_dimensions
Expand Down Expand Up @@ -92,3 +93,15 @@ async def upload_picture(
achievement.picture = f'static/{id}.png'
db.session.commit()
return achievement


@router.delete("/{id}", response_model=StatusResponseModel)
def delete_achievement(id: int, user=Depends(UnionAuth(['achievements.achievement.delete']))) -> StatusResponseModel:
"""Нужны права на: `achievements.achievement.delete`"""
achievement: Achievement | None = db.session.get(Achievement, id)
if achievement is None:
raise HTTPException(404, f"Achievement {id=} not found")
logger.info(f"User id={user['id']} has deleted achievement {achievement.name}")
db.session.delete(achievement)
db.session.commit()
return StatusResponseModel(status="Success", message=f"Achievement {id=} has been deleted", ru="Ачивка удалена")
17 changes: 17 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from achievement_api.routes.base import app
from achievement_api.settings import get_settings


@pytest.fixture
def client():
yield TestClient(app)


@pytest.fixture(scope='session')
def dbsession():
settings = get_settings()
engine = create_engine(str(settings.DB_DSN))
TestingSessionLocal = sessionmaker(bind=engine)
yield TestingSessionLocal()


@pytest.fixture
def achievement_id(client):
post_response = client.post("/achievement", json={"name": "test name", "description": "test description"})
yield post_response.json()["id"]
18 changes: 18 additions & 0 deletions tests/test_routes/test_achievement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from achievement_api.models.achievement import Achievement


@pytest.mark.authenticated("achievements.achievement.create", "achievements.achievement.delete")
def test_delete_existed(client, dbsession, achievement_id):
delete_response = client.delete(f"/achievement/{achievement_id}")
assert delete_response.status_code == 200
query = dbsession.get(Achievement, achievement_id)
assert query is None


@pytest.mark.authenticated("achievements.achievement.delete")
def test_delete_unexisted(client):
unexisted_id = -1
response = client.delete(f"/achievement/{unexisted_id}")
assert response.status_code == 404

0 comments on commit e3d25f3

Please sign in to comment.