Skip to content

Commit

Permalink
Use uuid type for the new modules (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
julien4215 authored May 23, 2024
1 parent 79b2b86 commit f42bab6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 29 deletions.
9 changes: 2 additions & 7 deletions app/modules/flappybird/models_flappybird.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from sqlalchemy import ForeignKey, String
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from app.core.models_core import CoreUser
Expand All @@ -10,13 +10,8 @@
class FlappyBirdScore(Base):
__tablename__ = "flappy-bird_score"

# id: Mapped[str] = mapped_column(String, primary_key=True, index=True)
id: Mapped[PrimaryKey]
user_id: Mapped[str] = mapped_column(
String,
ForeignKey("core_user.id"),
nullable=False,
)
user_id: Mapped[str] = mapped_column(ForeignKey("core_user.id"))
user: Mapped[CoreUser] = relationship("CoreUser")
value: Mapped[int]
creation_time: Mapped[datetime]
7 changes: 4 additions & 3 deletions app/modules/ph/cruds_ph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from collections.abc import Sequence
from datetime import date

Expand All @@ -23,7 +24,7 @@ async def get_papers(

async def get_paper_by_id(
db: AsyncSession,
paper_id: str,
paper_id: uuid.UUID,
) -> Sequence[models_ph.Paper]:
result = await db.execute(
select(models_ph.Paper).where(models_ph.Paper.id == paper_id),
Expand All @@ -47,7 +48,7 @@ async def create_paper(


async def update_paper(
paper_id: str,
paper_id: uuid.UUID,
paper_update: schemas_ph.PaperUpdate,
db: AsyncSession,
):
Expand All @@ -60,7 +61,7 @@ async def update_paper(


async def delete_paper(
paper_id: str,
paper_id: uuid.UUID,
db: AsyncSession,
):
await db.execute(
Expand Down
12 changes: 6 additions & 6 deletions app/modules/ph/endpoints_ph.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
status_code=200,
)
async def get_paper_pdf(
paper_id: str,
paper_id: uuid.UUID,
db: AsyncSession = Depends(get_db),
user: models_core.CoreUser = Depends(is_user_a_member),
):
Expand Down Expand Up @@ -109,7 +109,7 @@ async def create_paper(
"""Create a new paper."""

paper_complete = schemas_ph.PaperComplete(
id=str(uuid.uuid4()),
id=uuid.uuid4(),
**paper.model_dump(),
)
try:
Expand Down Expand Up @@ -153,7 +153,7 @@ async def create_paper(
status_code=201,
)
async def create_paper_pdf_and_cover(
paper_id: str,
paper_id: uuid.UUID,
pdf: UploadFile = File(...),
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.ph)),
request_id: str = Depends(get_request_id),
Expand Down Expand Up @@ -190,7 +190,7 @@ async def create_paper_pdf_and_cover(
status_code=200,
)
async def get_cover(
paper_id: str,
paper_id: uuid.UUID,
user: models_core.CoreUser = Depends(is_user_a_member),
db: AsyncSession = Depends(get_db),
):
Expand All @@ -213,7 +213,7 @@ async def get_cover(
status_code=204,
)
async def update_paper(
paper_id: str,
paper_id: uuid.UUID,
paper_update: schemas_ph.PaperUpdate,
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.ph)),
db: AsyncSession = Depends(get_db),
Expand All @@ -237,7 +237,7 @@ async def update_paper(
status_code=204,
)
async def delete_paper(
paper_id: str,
paper_id: uuid.UUID,
user: models_core.CoreUser = Depends(is_user_a_member_of(GroupType.ph)),
db: AsyncSession = Depends(get_db),
):
Expand Down
11 changes: 5 additions & 6 deletions app/modules/ph/models_ph.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from datetime import date

from sqlalchemy import Date, String
from sqlalchemy.orm import Mapped, mapped_column
from sqlalchemy.orm import Mapped

from app.types.sqlalchemy import Base
from app.types.sqlalchemy import Base, PrimaryKey


class Paper(Base):
__tablename__ = "ph_papers"

id: Mapped[str] = mapped_column(String, primary_key=True, index=True)
name: Mapped[str] = mapped_column(String, nullable=False)
release_date: Mapped[date] = mapped_column(Date, nullable=False)
id: Mapped[PrimaryKey]
name: Mapped[str]
release_date: Mapped[date]
3 changes: 2 additions & 1 deletion app/modules/ph/schemas_ph.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import uuid
from datetime import date

from pydantic import BaseModel
Expand All @@ -11,7 +12,7 @@ class PaperBase(BaseModel):


class PaperComplete(PaperBase):
id: str
id: uuid.UUID


class PaperUpdate(BaseModel):
Expand Down
51 changes: 51 additions & 0 deletions migrations/versions/14-fix_uuid_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""fix uuid type
Create Date: 2024-05-22 23:18:24.002804
"""

from collections.abc import Sequence
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pytest_alembic import MigrationContext

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "7b078dd0e7e4"
down_revision: str | None = "fce1716123e2"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
with op.batch_alter_table("ph_papers") as batch_op:
batch_op.alter_column(
"id",
type_=sa.Uuid(),
)
op.drop_index("ix_ph_papers_id", table_name="ph_papers")


def downgrade() -> None:
op.create_index("ix_ph_papers_id", "ph_papers", ["id"], unique=False)
with op.batch_alter_table("ph_papers") as batch_op:
batch_op.alter_column(
"id",
type_=sa.String(),
)


def pre_test_upgrade(
alembic_runner: "MigrationContext",
alembic_connection: sa.Connection,
) -> None:
pass


def test_upgrade(
alembic_runner: "MigrationContext",
alembic_connection: sa.Connection,
) -> None:
pass
14 changes: 8 additions & 6 deletions tests/test_PH.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ async def init_objects():

global paper
paper = models_ph.Paper(
id=str(uuid.uuid4()),
id=uuid.uuid4(),
name="OnlyPhans",
release_date=datetime.date(2023, 10, 21),
)
await add_object_to_db(paper)

global paper2
paper2 = models_ph.Paper(
id=str(uuid.uuid4()),
id=uuid.uuid4(),
name="OnlyPhans du futur",
release_date=datetime.date(2090, 10, 21),
)
Expand All @@ -75,8 +75,10 @@ def test_get_papers():
)
response_json = response.json()
assert response.status_code == 200
assert paper.id in [response_paper["id"] for response_paper in response_json]
assert paper2.id not in [response_paper["id"] for response_paper in response_json]
assert str(paper.id) in [response_paper["id"] for response_paper in response_json]
assert str(paper2.id) not in [
response_paper["id"] for response_paper in response_json
]


def test_get_papers_admin():
Expand All @@ -86,8 +88,8 @@ def test_get_papers_admin():
)
response_json = response.json()
assert response.status_code == 200
assert paper.id in [response_paper["id"] for response_paper in response_json]
assert paper2.id in [response_paper["id"] for response_paper in response_json]
assert str(paper.id) in [response_paper["id"] for response_paper in response_json]
assert str(paper2.id) in [response_paper["id"] for response_paper in response_json]


def test_create_paper_pdf_and_cover():
Expand Down

0 comments on commit f42bab6

Please sign in to comment.