Тесты
This commit is contained in:
parent
5a9e2c5be5
commit
fc12bc0464
4 changed files with 47 additions and 11 deletions
|
@ -4,11 +4,14 @@ from datetime import datetime
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from sqlalchemy import insert
|
from sqlalchemy import insert
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
from httpx import AsyncClient
|
||||||
|
|
||||||
from app.config import settings
|
from app.config import settings
|
||||||
from app.database import Base, async_session_maker, engine
|
from app.database import Base, async_session_maker, engine
|
||||||
from app.users.models import Users, UsersVerificationCodes
|
from app.users.models import Users, UsersVerificationCodes
|
||||||
from app.users.chat.models import Chats, UsersXChats, Messages
|
from app.users.chat.models import Chats, UsersXChats, Messages
|
||||||
|
from app.main import app as fastapi_app
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True, scope='session')
|
@pytest.fixture(autouse=True, scope='session')
|
||||||
|
@ -20,7 +23,7 @@ async def prepare_database():
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
def open_mock_json(model: str):
|
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)
|
return json.load(file)
|
||||||
|
|
||||||
users = open_mock_json("users")
|
users = open_mock_json("users")
|
||||||
|
@ -50,8 +53,7 @@ async def prepare_database():
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="session")
|
@pytest.fixture(scope="function")
|
||||||
def event_loop(request):
|
async def ac():
|
||||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
async with AsyncClient(app=fastapi_app, base_url="http://test") as ac:
|
||||||
yield loop
|
yield ac
|
||||||
loop.close()
|
|
||||||
|
|
|
@ -1,2 +1,23 @@
|
||||||
def test_abc():
|
import pytest
|
||||||
assert 1 == 1
|
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)
|
||||||
|
|
|
@ -24,7 +24,7 @@ async def get_teleport():
|
||||||
return RedirectResponse(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
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():
|
async def get_all_users():
|
||||||
users = await UserDAO.find_all()
|
users = await UserDAO.find_all()
|
||||||
return users
|
return users
|
||||||
|
|
|
@ -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
|
from fastapi import Query
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,6 +16,18 @@ class SUserRegister(BaseModel):
|
||||||
password: str = Query(None, min_length=8)
|
password: str = Query(None, min_length=8)
|
||||||
date_of_birth: date
|
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):
|
class SUser(BaseModel):
|
||||||
model_config = ConfigDict(from_attributes=True)
|
model_config = ConfigDict(from_attributes=True)
|
||||||
|
|
Loading…
Add table
Reference in a new issue