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:
154
backend/tests/services/test_forms_service.py
Normal file
154
backend/tests/services/test_forms_service.py
Normal file
@@ -0,0 +1,154 @@
|
||||
import pytest
|
||||
from sqlmodel import Session
|
||||
|
||||
import src.models as models
|
||||
import src.forms.service as forms_service
|
||||
import src.forms.exceptions as forms_exceptions
|
||||
import tests.factories.forms as forms_factory
|
||||
|
||||
class TestFormsService:
|
||||
def test_get_all_forms(self, session: Session, forms: list[models.FormPublic]):
|
||||
result = forms_service.get_all(session, [], [], False)
|
||||
|
||||
assert len(result) == 2
|
||||
assert result == forms
|
||||
|
||||
def test_get_all_forms_filter_productors(self, session: Session, forms: list[models.FormPublic]):
|
||||
result = forms_service.get_all(session, [], ['test productor'], False)
|
||||
|
||||
assert len(result) == 2
|
||||
assert result == forms
|
||||
|
||||
def test_get_all_forms_filter_season(self, session: Session, forms: list[models.FormPublic]):
|
||||
result = forms_service.get_all(session, ['test season 1'], [], False)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_all_forms_all_filters(self, session: Session, forms: list[models.FormPublic]):
|
||||
result = forms_service.get_all(session, ['test season 1'], ['test productor'], True)
|
||||
|
||||
assert result == forms
|
||||
|
||||
def test_get_one_form(self, session: Session, forms: list[models.FormPublic]):
|
||||
result = forms_service.get_one(session, forms[0].id)
|
||||
|
||||
assert result == forms[0]
|
||||
|
||||
def test_get_one_form_notfound(self, session: Session):
|
||||
result = forms_service.get_one(session, 122)
|
||||
|
||||
assert result == None
|
||||
|
||||
def test_create_form(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic
|
||||
):
|
||||
form_create = forms_factory.form_create_factory(
|
||||
name="new test form",
|
||||
productor_id=productor.id,
|
||||
referer=referer.id,
|
||||
season="new test season",
|
||||
)
|
||||
result = forms_service.create_one(session, form_create)
|
||||
|
||||
assert result.id is not None
|
||||
assert result.name == "new test form"
|
||||
assert result.productor.name == "test productor"
|
||||
|
||||
def test_create_form_invalidinput(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.Productor
|
||||
):
|
||||
form_create = None
|
||||
with pytest.raises(forms_exceptions.FormCreateError):
|
||||
result = forms_service.create_one(session, form_create)
|
||||
|
||||
form_create = forms_factory.form_create_factory(productor_id=123)
|
||||
with pytest.raises(forms_exceptions.ProductorNotFoundError):
|
||||
result = forms_service.create_one(session, form_create)
|
||||
|
||||
form_create = forms_factory.form_create_factory(
|
||||
productor_id=productor.id,
|
||||
referer_id=123
|
||||
)
|
||||
with pytest.raises(forms_exceptions.UserNotFoundError):
|
||||
result = forms_service.create_one(session, form_create)
|
||||
|
||||
|
||||
def test_update_form(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic,
|
||||
forms: list[models.FormPublic]
|
||||
):
|
||||
form_update = forms_factory.form_update_factory(
|
||||
name='updated test form',
|
||||
productor_id=productor.id,
|
||||
referer_id=referer.id,
|
||||
season='updated test season'
|
||||
)
|
||||
form_id = forms[0].id
|
||||
result = forms_service.update_one(session, form_id, form_update)
|
||||
|
||||
assert result.id == form_id
|
||||
assert result.name == 'updated test form'
|
||||
assert result.season == 'updated test season'
|
||||
|
||||
def test_update_form_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic,
|
||||
):
|
||||
form_update = forms_factory.form_update_factory(
|
||||
name='updated test form',
|
||||
productor_id=productor.id,
|
||||
referer_id=referer.id,
|
||||
season='updated test season'
|
||||
)
|
||||
form_id = 123
|
||||
with pytest.raises(forms_exceptions.FormNotFoundError):
|
||||
result = forms_service.update_one(session, form_id, form_update)
|
||||
|
||||
def test_update_form_invalidinput(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
forms: list[models.FormPublic]
|
||||
):
|
||||
form_id = forms[0].id
|
||||
form_update = forms_factory.form_update_factory(productor_id=123)
|
||||
with pytest.raises(forms_exceptions.ProductorNotFoundError):
|
||||
result = forms_service.update_one(session, form_id, form_update)
|
||||
|
||||
form_update = forms_factory.form_update_factory(
|
||||
productor_id=productor.id,
|
||||
referer_id=123
|
||||
)
|
||||
with pytest.raises(forms_exceptions.UserNotFoundError):
|
||||
result = forms_service.update_one(session, form_id, form_update)
|
||||
|
||||
def test_delete_form(
|
||||
self,
|
||||
session: Session,
|
||||
forms: list[models.FormPublic]
|
||||
):
|
||||
form_id = forms[0].id
|
||||
result = forms_service.delete_one(session, form_id)
|
||||
|
||||
check = forms_service.get_one(session, form_id)
|
||||
assert check == None
|
||||
|
||||
def test_delete_form_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
forms: list[models.FormPublic]
|
||||
):
|
||||
form_id = 123
|
||||
with pytest.raises(forms_exceptions.FormNotFoundError):
|
||||
result = forms_service.delete_one(session, form_id)
|
||||
|
||||
143
backend/tests/services/test_productors_service.py
Normal file
143
backend/tests/services/test_productors_service.py
Normal file
@@ -0,0 +1,143 @@
|
||||
import pytest
|
||||
from sqlmodel import Session
|
||||
|
||||
import src.models as models
|
||||
import src.productors.service as productors_service
|
||||
import src.productors.exceptions as productors_exceptions
|
||||
import tests.factories.productors as productors_factory
|
||||
|
||||
class TestProductorsService:
|
||||
def test_get_all_productors(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = productors_service.get_all(session, user, [], [])
|
||||
|
||||
assert len(result) == 1
|
||||
assert result == [productors[0]]
|
||||
|
||||
def test_get_all_productors_filter_names(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = productors_service.get_all(
|
||||
session,
|
||||
user,
|
||||
['test productor 1'],
|
||||
[]
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_all_productors_filter_types(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = productors_service.get_all(
|
||||
session,
|
||||
user,
|
||||
[],
|
||||
['Légumineuses'],
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_all_productors_all_filters(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = productors_service.get_all(
|
||||
session,
|
||||
user,
|
||||
['test productor 1'],
|
||||
['Légumineuses'],
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_one_productor(self, session: Session, productors: list[models.ProductorPublic]):
|
||||
result = productors_service.get_one(session, productors[0].id)
|
||||
|
||||
assert result == productors[0]
|
||||
|
||||
def test_get_one_productor_notfound(self, session: Session):
|
||||
result = productors_service.get_one(session, 122)
|
||||
|
||||
assert result == None
|
||||
|
||||
def test_create_productor(
|
||||
self,
|
||||
session: Session,
|
||||
referer: models.ProductorPublic
|
||||
):
|
||||
productor_create = productors_factory.productor_create_factory(
|
||||
name="new test productor",
|
||||
)
|
||||
result = productors_service.create_one(session, productor_create)
|
||||
|
||||
assert result.id is not None
|
||||
assert result.name == "new test productor"
|
||||
|
||||
def test_create_productor_invalidinput(
|
||||
self,
|
||||
session: Session,
|
||||
):
|
||||
productor_create = None
|
||||
with pytest.raises(productors_exceptions.ProductorCreateError):
|
||||
result = productors_service.create_one(session, productor_create)
|
||||
|
||||
def test_update_productor(
|
||||
self,
|
||||
session: Session,
|
||||
referer: models.ProductorPublic,
|
||||
productors: list[models.ProductorPublic]
|
||||
):
|
||||
productor_update = productors_factory.productor_update_factory(
|
||||
name='updated test productor',
|
||||
)
|
||||
productor_id = productors[0].id
|
||||
result = productors_service.update_one(session, productor_id, productor_update)
|
||||
|
||||
assert result.id == productor_id
|
||||
assert result.name == 'updated test productor'
|
||||
|
||||
def test_update_productor_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
referer: models.ProductorPublic,
|
||||
):
|
||||
productor_update = productors_factory.productor_update_factory(
|
||||
name='updated test productor',
|
||||
)
|
||||
productor_id = 123
|
||||
with pytest.raises(productors_exceptions.ProductorNotFoundError):
|
||||
result = productors_service.update_one(session, productor_id, productor_update)
|
||||
|
||||
def test_delete_productor(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic]
|
||||
):
|
||||
productor_id = productors[0].id
|
||||
result = productors_service.delete_one(session, productor_id)
|
||||
|
||||
check = productors_service.get_one(session, productor_id)
|
||||
assert check == None
|
||||
|
||||
def test_delete_productor_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
productors: list[models.ProductorPublic]
|
||||
):
|
||||
productor_id = 123
|
||||
with pytest.raises(productors_exceptions.ProductorNotFoundError):
|
||||
result = productors_service.delete_one(session, productor_id)
|
||||
|
||||
191
backend/tests/services/test_products_service.py
Normal file
191
backend/tests/services/test_products_service.py
Normal file
@@ -0,0 +1,191 @@
|
||||
import pytest
|
||||
from sqlmodel import Session
|
||||
|
||||
import src.models as models
|
||||
import src.products.service as products_service
|
||||
import src.products.exceptions as products_exceptions
|
||||
import tests.factories.products as products_factory
|
||||
|
||||
class TestProductsService:
|
||||
def test_get_all_products(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = products_service.get_all(session, user, [], [], [])
|
||||
|
||||
assert len(result) == 2
|
||||
assert result == products
|
||||
|
||||
def test_get_all_products_filter_productors(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = products_service.get_all(
|
||||
session,
|
||||
user,
|
||||
[],
|
||||
['test productor'],
|
||||
[]
|
||||
)
|
||||
|
||||
assert len(result) == 2
|
||||
assert result == products
|
||||
|
||||
def test_get_all_products_filter_names(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = products_service.get_all(
|
||||
session,
|
||||
user,
|
||||
['product 1 occasionnal'],
|
||||
[],
|
||||
[]
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_all_products_filter_types(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = products_service.get_all(
|
||||
session,
|
||||
user,
|
||||
[],
|
||||
[],
|
||||
['1']
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_all_products_all_filters(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic],
|
||||
user: models.UserPublic
|
||||
):
|
||||
result = products_service.get_all(
|
||||
session,
|
||||
user,
|
||||
['product 1 occasionnal'],
|
||||
['test productor'],
|
||||
['1']
|
||||
)
|
||||
|
||||
assert len(result) == 1
|
||||
|
||||
def test_get_one_product(self, session: Session, products: list[models.ProductPublic]):
|
||||
result = products_service.get_one(session, products[0].id)
|
||||
|
||||
assert result == products[0]
|
||||
|
||||
def test_get_one_product_notfound(self, session: Session):
|
||||
result = products_service.get_one(session, 122)
|
||||
|
||||
assert result == None
|
||||
|
||||
def test_create_product(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic
|
||||
):
|
||||
product_create = products_factory.product_create_factory(
|
||||
name="new test product",
|
||||
productor_id=productor.id,
|
||||
)
|
||||
result = products_service.create_one(session, product_create)
|
||||
|
||||
assert result.id is not None
|
||||
assert result.name == "new test product"
|
||||
assert result.productor.name == "test productor"
|
||||
|
||||
def test_create_product_invalidinput(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.Productor
|
||||
):
|
||||
product_create = None
|
||||
with pytest.raises(products_exceptions.ProductCreateError):
|
||||
result = products_service.create_one(session, product_create)
|
||||
|
||||
product_create = products_factory.product_create_factory(productor_id=123)
|
||||
with pytest.raises(products_exceptions.ProductorNotFoundError):
|
||||
result = products_service.create_one(session, product_create)
|
||||
|
||||
def test_update_product(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic,
|
||||
products: list[models.ProductPublic]
|
||||
):
|
||||
product_update = products_factory.product_update_factory(
|
||||
name='updated test product',
|
||||
productor_id=productor.id,
|
||||
)
|
||||
product_id = products[0].id
|
||||
result = products_service.update_one(session, product_id, product_update)
|
||||
|
||||
assert result.id == product_id
|
||||
assert result.name == 'updated test product'
|
||||
|
||||
def test_update_product_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
referer: models.ProductorPublic,
|
||||
):
|
||||
product_update = products_factory.product_update_factory(
|
||||
name='updated test product',
|
||||
productor_id=productor.id,
|
||||
)
|
||||
product_id = 123
|
||||
with pytest.raises(products_exceptions.ProductNotFoundError):
|
||||
result = products_service.update_one(session, product_id, product_update)
|
||||
|
||||
def test_update_product_invalidinput(
|
||||
self,
|
||||
session: Session,
|
||||
productor: models.ProductorPublic,
|
||||
products: list[models.ProductPublic]
|
||||
):
|
||||
product_id = products[0].id
|
||||
product_update = products_factory.product_update_factory(productor_id=123)
|
||||
with pytest.raises(products_exceptions.ProductorNotFoundError):
|
||||
result = products_service.update_one(session, product_id, product_update)
|
||||
|
||||
product_update = products_factory.product_update_factory(
|
||||
productor_id=productor.id,
|
||||
referer_id=123
|
||||
)
|
||||
|
||||
def test_delete_product(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic]
|
||||
):
|
||||
product_id = products[0].id
|
||||
result = products_service.delete_one(session, product_id)
|
||||
|
||||
check = products_service.get_one(session, product_id)
|
||||
assert check == None
|
||||
|
||||
def test_delete_product_notfound(
|
||||
self,
|
||||
session: Session,
|
||||
products: list[models.ProductPublic]
|
||||
):
|
||||
product_id = 123
|
||||
with pytest.raises(products_exceptions.ProductNotFoundError):
|
||||
result = products_service.delete_one(session, product_id)
|
||||
|
||||
Reference in New Issue
Block a user