add tests forn forms, products, productors
All checks were successful
Deploy Amap / deploy (push) Successful in 3m45s
All checks were successful
Deploy Amap / deploy (push) Successful in 3m45s
This commit is contained in:
17
backend/src/forms/exceptions.py
Normal file
17
backend/src/forms/exceptions.py
Normal file
@@ -0,0 +1,17 @@
|
||||
class FormServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class UserNotFoundError(FormServiceError):
|
||||
pass
|
||||
|
||||
class ProductorNotFoundError(FormServiceError):
|
||||
pass
|
||||
|
||||
class FormNotFoundError(FormServiceError):
|
||||
pass
|
||||
|
||||
class FormCreateError(FormServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -4,6 +4,7 @@ import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.forms.service as service
|
||||
import src.forms.exceptions as exceptions
|
||||
from src.auth.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix='/forms')
|
||||
@@ -40,7 +41,15 @@ async def create_form(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, form)
|
||||
try:
|
||||
form = service.create_one(session, form)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.UserNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.usernotfound)
|
||||
except exceptions.FormCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.forminputinvalid)
|
||||
return form
|
||||
|
||||
@router.put('/{id}', response_model=models.FormPublic)
|
||||
async def update_form(
|
||||
@@ -48,9 +57,14 @@ async def update_form(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, form)
|
||||
if result is None:
|
||||
try:
|
||||
result = service.update_one(session, id, form)
|
||||
except exceptions.FormNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
except exceptions.UserNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.usernotfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.FormPublic)
|
||||
@@ -59,7 +73,8 @@ async def delete_form(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.FormNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
from sqlalchemy import func
|
||||
|
||||
import src.models as models
|
||||
import src.forms.exceptions as exceptions
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
seasons: list[str],
|
||||
@@ -46,6 +48,12 @@ def get_one(session: Session, form_id: int) -> models.FormPublic:
|
||||
return session.get(models.Form, form_id)
|
||||
|
||||
def create_one(session: Session, form: models.FormCreate) -> models.FormPublic:
|
||||
if not form:
|
||||
raise exceptions.FormCreateError('FormCreate input cannot be None')
|
||||
if not session.get(models.Productor, form.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {form.productor_id} not found')
|
||||
if not session.get(models.User, form.referer_id):
|
||||
raise exceptions.UserNotFoundError(f'User {form.referer_id} not found')
|
||||
form_create = form.model_dump(exclude_unset=True)
|
||||
new_form = models.Form(**form_create)
|
||||
session.add(new_form)
|
||||
@@ -58,7 +66,11 @@ def update_one(session: Session, id: int, form: models.FormUpdate) -> models.For
|
||||
result = session.exec(statement)
|
||||
new_form = result.first()
|
||||
if not new_form:
|
||||
return None
|
||||
raise exceptions.FormNotFoundError(f'Form {id} not found')
|
||||
if form.productor_id and not session.get(models.Productor, form.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {form.productor_id} not found')
|
||||
if form.referer_id and not session.get(models.User, form.referer_id):
|
||||
raise exceptions.UserNotFoundError(f'User {form.referer_id} not found')
|
||||
form_updates = form.model_dump(exclude_unset=True)
|
||||
for key, value in form_updates.items():
|
||||
setattr(new_form, key, value)
|
||||
@@ -72,7 +84,7 @@ def delete_one(session: Session, id: int) -> models.FormPublic:
|
||||
result = session.exec(statement)
|
||||
form = result.first()
|
||||
if not form:
|
||||
return None
|
||||
raise exceptions.FormNotFoundError(f'Form {id} not found')
|
||||
result = models.FormPublic.model_validate(form)
|
||||
session.delete(form)
|
||||
session.commit()
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
notfound = "Resource was not found."
|
||||
pdferror = "An error occured during PDF generation please contact administrator"
|
||||
tokenexipired = "Token expired"
|
||||
invalidtoken = "Invalid token"
|
||||
notauthenticated = "Not authenticated"
|
||||
usernotfound = "User not found"
|
||||
userloggedout = "User logged out"
|
||||
failtogettoken = "Failed to get token"
|
||||
unauthorized = "Unauthorized"
|
||||
notallowed = "Not Allowed"
|
||||
pdferror = 'An error occured during PDF generation please contact administrator'
|
||||
|
||||
tokenexipired = 'Token expired'
|
||||
invalidtoken = 'Invalid token'
|
||||
notauthenticated = 'Not authenticated'
|
||||
failtogettoken = 'Failed to get token'
|
||||
unauthorized = 'Unauthorized'
|
||||
notallowed = 'Not Allowed'
|
||||
|
||||
notfound = 'Resource was not found.'
|
||||
usernotfound = 'User not found'
|
||||
userloggedout = 'User logged out'
|
||||
|
||||
productorinputinvalid = 'Invalid productor input'
|
||||
productornotfound = 'Productor not found'
|
||||
|
||||
forminputinvalid = 'Invalid form input'
|
||||
formnotfound = 'Form not found'
|
||||
|
||||
productinputinvalid = 'Invalid product input'
|
||||
productnotfound = 'Product not found'
|
||||
|
||||
11
backend/src/productors/exceptions.py
Normal file
11
backend/src/productors/exceptions.py
Normal file
@@ -0,0 +1,11 @@
|
||||
class ProductorServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class ProductorNotFoundError(ProductorServiceError):
|
||||
pass
|
||||
|
||||
class ProductorCreateError(ProductorServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -4,6 +4,7 @@ import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.productors.service as service
|
||||
import src.productors.exceptions as exceptions
|
||||
from src.auth.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix='/productors')
|
||||
@@ -34,7 +35,11 @@ def create_productor(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, productor)
|
||||
try:
|
||||
result = service.create_one(session, productor)
|
||||
except exceptions.ProductorCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.productorinputinvalid)
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductorPublic)
|
||||
def update_productor(
|
||||
@@ -42,9 +47,10 @@ def update_productor(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, productor)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.update_one(session, id, productor)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductorPublic)
|
||||
@@ -53,7 +59,8 @@ def delete_productor(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.productors.exceptions as exceptions
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -20,7 +21,9 @@ def get_one(session: Session, productor_id: int) -> models.ProductorPublic:
|
||||
return session.get(models.Productor, productor_id)
|
||||
|
||||
def create_one(session: Session, productor: models.ProductorCreate) -> models.ProductorPublic:
|
||||
productor_create = productor.model_dump(exclude_unset=True, exclude="payment_methods")
|
||||
if not productor:
|
||||
raise exceptions.ProductorCreateError('ProductorCreate input cannot be None')
|
||||
productor_create = productor.model_dump(exclude_unset=True, exclude='payment_methods')
|
||||
new_productor = models.Productor(**productor_create)
|
||||
|
||||
new_productor.payment_methods = [
|
||||
@@ -40,21 +43,21 @@ def update_one(session: Session, id: int, productor: models.ProductorUpdate) ->
|
||||
result = session.exec(statement)
|
||||
new_productor = result.first()
|
||||
if not new_productor:
|
||||
return None
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {id} not found')
|
||||
|
||||
productor_updates = productor.model_dump(exclude_unset=True)
|
||||
if "payment_methods" in productor_updates:
|
||||
if 'payment_methods' in productor_updates:
|
||||
new_productor.payment_methods.clear()
|
||||
for pm in productor_updates["payment_methods"]:
|
||||
for pm in productor_updates['payment_methods']:
|
||||
new_productor.payment_methods.append(
|
||||
models.PaymentMethod(
|
||||
name=pm["name"],
|
||||
details=pm["details"],
|
||||
name=pm['name'],
|
||||
details=pm['details'],
|
||||
productor_id=id,
|
||||
max=pm["max"]
|
||||
max=pm['max']
|
||||
)
|
||||
)
|
||||
del productor_updates["payment_methods"]
|
||||
del productor_updates['payment_methods']
|
||||
|
||||
for key, value in productor_updates.items():
|
||||
setattr(new_productor, key, value)
|
||||
@@ -68,7 +71,7 @@ def delete_one(session: Session, id: int) -> models.ProductorPublic:
|
||||
result = session.exec(statement)
|
||||
productor = result.first()
|
||||
if not productor:
|
||||
return None
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {id} not found')
|
||||
result = models.ProductorPublic.model_validate(productor)
|
||||
session.delete(productor)
|
||||
session.commit()
|
||||
|
||||
14
backend/src/products/exceptions.py
Normal file
14
backend/src/products/exceptions.py
Normal file
@@ -0,0 +1,14 @@
|
||||
class ProductServiceError(Exception):
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
class ProductorNotFoundError(ProductServiceError):
|
||||
pass
|
||||
|
||||
class ProductNotFoundError(ProductServiceError):
|
||||
pass
|
||||
|
||||
class ProductCreateError(ProductServiceError):
|
||||
def __init__(self, message: str, field: str | None = None):
|
||||
super().__init__(message)
|
||||
self.field = field
|
||||
@@ -4,7 +4,9 @@ import src.models as models
|
||||
from src.database import get_session
|
||||
from sqlmodel import Session
|
||||
import src.products.service as service
|
||||
import src.products.exceptions as exceptions
|
||||
from src.auth.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix='/products')
|
||||
|
||||
@router.get('', response_model=list[models.ProductPublic], )
|
||||
@@ -40,7 +42,13 @@ def create_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
return service.create_one(session, product)
|
||||
try:
|
||||
result = service.create_one(session, product)
|
||||
except exceptions.ProductCreateError:
|
||||
raise HTTPException(status_code=400, detail=messages.productinputinvalid)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@router.put('/{id}', response_model=models.ProductPublic)
|
||||
def update_product(
|
||||
@@ -48,9 +56,12 @@ def update_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.update_one(session, id, product)
|
||||
if result is None:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
try:
|
||||
result = service.update_one(session, id, product)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productnotfound)
|
||||
except exceptions.ProductorNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.productornotfound)
|
||||
return result
|
||||
|
||||
@router.delete('/{id}', response_model=models.ProductPublic)
|
||||
@@ -59,7 +70,8 @@ def delete_product(
|
||||
user: models.User = Depends(get_current_user),
|
||||
session: Session = Depends(get_session)
|
||||
):
|
||||
result = service.delete_one(session, id)
|
||||
if result is None:
|
||||
try:
|
||||
result = service.delete_one(session, id)
|
||||
except exceptions.ProductNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=messages.notfound)
|
||||
return result
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from sqlmodel import Session, select
|
||||
import src.models as models
|
||||
import src.products.exceptions as exceptions
|
||||
|
||||
def get_all(
|
||||
session: Session,
|
||||
@@ -15,7 +16,7 @@ def get_all(
|
||||
if len(names) > 0:
|
||||
statement = statement.where(models.Product.name.in_(names))
|
||||
if len(productors) > 0:
|
||||
statement = statement.join(models.Productor).where(models.Productor.name.in_(productors))
|
||||
statement = statement.where(models.Productor.name.in_(productors))
|
||||
if len(types) > 0:
|
||||
statement = statement.where(models.Product.type.in_(types))
|
||||
return session.exec(statement.order_by(models.Product.name)).all()
|
||||
@@ -24,6 +25,10 @@ def get_one(session: Session, product_id: int) -> models.ProductPublic:
|
||||
return session.get(models.Product, product_id)
|
||||
|
||||
def create_one(session: Session, product: models.ProductCreate) -> models.ProductPublic:
|
||||
if not product:
|
||||
raise exceptions.ProductCreateError('ProductCreate input cannot be None')
|
||||
if not session.get(models.Productor, product.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
product_create = product.model_dump(exclude_unset=True)
|
||||
new_product = models.Product(**product_create)
|
||||
session.add(new_product)
|
||||
@@ -36,7 +41,10 @@ def update_one(session: Session, id: int, product: models.ProductUpdate) -> mode
|
||||
result = session.exec(statement)
|
||||
new_product = result.first()
|
||||
if not new_product:
|
||||
return None
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
if product.productor_id and not session.get(models.Productor, product.productor_id):
|
||||
raise exceptions.ProductorNotFoundError(f'Productor {product.productor_id} not found')
|
||||
|
||||
product_updates = product.model_dump(exclude_unset=True)
|
||||
for key, value in product_updates.items():
|
||||
setattr(new_product, key, value)
|
||||
@@ -51,7 +59,7 @@ def delete_one(session: Session, id: int) -> models.ProductPublic:
|
||||
result = session.exec(statement)
|
||||
product = result.first()
|
||||
if not product:
|
||||
return None
|
||||
raise exceptions.ProductNotFoundError(f'Product {id} not found')
|
||||
result = models.ProductPublic.model_validate(product)
|
||||
session.delete(product)
|
||||
session.commit()
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from pydantic_settings import BaseSettings
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
class Settings(BaseSettings):
|
||||
origins: str
|
||||
@@ -16,8 +16,9 @@ class Settings(BaseSettings):
|
||||
max_age: int
|
||||
debug: bool
|
||||
|
||||
class Config:
|
||||
env_file = "../.env"
|
||||
model_config = SettingsConfigDict(
|
||||
env_file='../.env'
|
||||
)
|
||||
|
||||
settings = Settings()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user