unit & interga tests

This commit is contained in:
2026-08-19 00:22:59 +03:00
parent b3083b0e82
commit 8bc3ff7b54
7 changed files with 84 additions and 2 deletions
+7
View File
@@ -1,2 +1,9 @@
import pytest_asyncio
from src.service.users_crud.users_crud import CrudService
@pytest_asyncio.fixture
async def crud_service()->CrudService:
crud_service=CrudService()
return crud_service
+48
View File
@@ -0,0 +1,48 @@
from types import SimpleNamespace
from unittest.mock import AsyncMock
import allure
import pytest
from fastapi import HTTPException
from src.service.users_crud.users_crud import CrudService
@pytest.mark.unit
class TestCrud:
@pytest.mark.parametrize("user_data",[
pytest.param(SimpleNamespace(first_name="test",last_name="test",middle_name="test",email="d@d.d",direct_permissions=[],group=[],status=True),id="Get_user_by_email_positive")
])
async def test_get_user_by_email_positive(self, monkeypatch, user_data:SimpleNamespace, crud_service:CrudService)->None:
with allure.step("Patching functions"):
monkeypatch.setattr(crud_service.crud_db_actions, "get_user_by_email",AsyncMock(return_value=user_data))
with allure.step("Test get_by_email"):
test_result = await crud_service.get_user_by_email(user_data.email)
assert test_result.email==user_data.email
assert test_result.first_name==user_data.first_name
assert test_result.last_name==user_data.last_name
assert test_result.middle_name==user_data.middle_name
assert test_result.email==user_data.email
assert test_result.email==user_data.email
assert not hasattr(test_result, "password") or not hasattr(test_result, "plain_password") or not hasattr(test_result, "hashed_password")
assert test_result.direct_permissions==user_data.direct_permissions
assert test_result.group==user_data.group
assert not hasattr(test_result, "status")
@pytest.mark.parametrize("email, expected_exception, expected_status",[
pytest.param("Wrong_email", HTTPException, 404,id="Wrong_email"),
pytest.param("",HTTPException, 404,id="Empty_email"),
])
async def test_get_user_by_email_negative(self, email, crud_service:CrudService, expected_exception, expected_status:int)->None:
with allure.step("Test get_by_email"), pytest.raises(expected_exception) as exc_info:
await crud_service.get_user_by_email(email)
if expected_exception is HTTPException:
assert exc_info.value.status_code==expected_status