mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
add set of unit tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from the_app.database import get_db
|
||||
@@ -10,7 +10,7 @@ from the_app.schemas.stuff import StuffResponse, StuffSchema
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/", response_model=StuffResponse)
|
||||
@router.post("/", status_code=status.HTTP_201_CREATED)
|
||||
async def create_stuff(stuff: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
stuff_id = await Stuff.create(db_session, stuff)
|
||||
return {**stuff.dict(), "id": stuff_id}
|
||||
|
||||
@@ -3,6 +3,8 @@ from typing import Any
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.ext.declarative import as_declarative, declared_attr
|
||||
from fastapi import HTTPException, status
|
||||
from icecream import ic
|
||||
|
||||
|
||||
@as_declarative()
|
||||
@@ -19,5 +21,6 @@ class Base:
|
||||
db_session.add(self)
|
||||
return await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
print(f"Have to rollback, save failed: {ex}")
|
||||
raise
|
||||
ic("Have to rollback, save failed:")
|
||||
ic(ex)
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=ex.__str__())
|
||||
|
||||
@@ -12,7 +12,7 @@ class Stuff(Base):
|
||||
__tablename__ = "stuff"
|
||||
id = Column(UUID(as_uuid=True), unique=True, default=uuid.uuid4, autoincrement=True)
|
||||
name = Column(String, nullable=False, primary_key=True, unique=True)
|
||||
description = Column(String, nullable=False, unique=True)
|
||||
description = Column(String, nullable=False)
|
||||
|
||||
def __init__(self, name: str, description: str):
|
||||
self.name = name
|
||||
|
||||
Reference in New Issue
Block a user