Тесты

This commit is contained in:
urec56 2024-02-15 22:24:26 +03:00
parent 5a9e2c5be5
commit fc12bc0464
4 changed files with 47 additions and 11 deletions

View file

@ -4,11 +4,14 @@ from datetime import datetime
import pytest
from sqlalchemy import insert
from fastapi.testclient import TestClient
from httpx import AsyncClient
from app.config import settings
from app.database import Base, async_session_maker, engine
from app.users.models import Users, UsersVerificationCodes
from app.users.chat.models import Chats, UsersXChats, Messages
from app.main import app as fastapi_app
@pytest.fixture(autouse=True, scope='session')
@ -20,7 +23,7 @@ async def prepare_database():
await conn.run_sync(Base.metadata.create_all)
def open_mock_json(model: str):
with open(f"app/tests/mock_{model}.json", 'r') as file:
with open(f"app/tests/mock_{model}.json", 'r', encoding='utf8') as file:
return json.load(file)
users = open_mock_json("users")
@ -50,8 +53,7 @@ async def prepare_database():
await session.commit()
@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture(scope="function")
async def ac():
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
yield ac

View file

@ -1,2 +1,23 @@
def test_abc():
assert 1 == 1
import pytest
from httpx import AsyncClient
@pytest.mark.parametrize("email,username,password,date_of_birth,status_code", [
("sosi@lox.com", "sobakasutulaya", "12311231", "1990-01-01", 200),
("sosi@lox.com", "sobakasutulaya", "12311231", "1990-01-01", 409),
("lox@sosi.com", "sobakasutulaya", "12311231", "1990-01-01", 409),
("lox@sosi.com", "sobakastroinaya", "3228", "1990-01-01", 422),
("lox@sosi.com", "sobakastroinaya", "asdwdawdasd", "2030-01-01", 422),
("lox@sosi.com", "sobakastroinaya", "asdwdawdasd", "2000-01-01", 200),
])
async def test_register_user(email, username, password, date_of_birth, status_code, ac: AsyncClient):
response = await ac.post("/users/register", json={
"email": email,
"username": username,
"password": password,
"date_of_birth": date_of_birth
})
assert response.status_code == status_code
print(response)

View file

@ -24,7 +24,7 @@ async def get_teleport():
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
@router.get("", response_model=list[SUser])
@router.get("/", response_model=list[SUser])
async def get_all_users():
users = await UserDAO.find_all()
return users

View file

@ -1,6 +1,7 @@
from datetime import date
from datetime import date, timedelta
from pydantic import BaseModel, EmailStr, ConfigDict
from pydantic_core import PydanticCustomError
from pydantic import BaseModel, EmailStr, ConfigDict, field_validator
from fastapi import Query
@ -15,6 +16,18 @@ class SUserRegister(BaseModel):
password: str = Query(None, min_length=8)
date_of_birth: date
@field_validator("date_of_birth")
@classmethod
def validate_date_of_birth(cls, input_date):
if date.today() - input_date < timedelta(days=365 * 16):
date_of_birth = date.today() - timedelta(days=365*16)
raise PydanticCustomError(
"date_input_error",
"date of birth might be earlier than {date_of_birth}",
{"date_of_birth": date_of_birth}
)
return input_date
class SUser(BaseModel):
model_config = ConfigDict(from_attributes=True)