Начало тестирования

This commit is contained in:
urec56 2024-02-14 22:30:28 +03:00
parent 4282f48eed
commit d85c9e5065
15 changed files with 169 additions and 16 deletions

View file

@ -1,3 +1,5 @@
MODE=
DB_HOST=
DB_PORT=
DB_USER=

View file

@ -1,13 +1,23 @@
from typing import Literal
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
MODE: Literal["DEV", "TEST", "PROD"]
DB_USER: str
DB_PASS: str
DB_HOST: str
DB_PORT: str
DB_NAME: str
TEST_DB_HOST: str
TEST_DB_PORT: str
TEST_DB_USER: str
TEST_DB_PASS: str
TEST_DB_NAME: str
SECRET_KEY: str
ALGORITHM: str

4
app/conftest.py Normal file
View file

@ -0,0 +1,4 @@
import os
os.environ["MODE"] = "TEST"

View file

@ -1,14 +1,20 @@
from sqlalchemy import NullPool
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import DeclarativeBase, sessionmaker
from app.config import settings
# URL по которому лежит БД
DATABASE_URL = f"""postgresql+asyncpg://{settings.DB_USER}:
{settings.DB_PASS}@{settings.DB_HOST}:
{settings.DB_PORT}/{settings.DB_NAME}"""
DATABASE_PARAMS = {}
if settings.MODE == "TEST":
DATABASE_URL = f"""postgresql+asyncpg://{settings.TEST_DB_USER}:
{settings.TEST_DB_PASS}@{settings.TEST_DB_HOST}:
{settings.TEST_DB_PORT}/{settings.TEST_DB_NAME}"""
DATABASE_PARAMS = {"poolclass": NullPool}
else:
# URL по которому лежит БД
DATABASE_URL = f"""postgresql+asyncpg://{settings.DB_USER}:
{settings.DB_PASS}@{settings.DB_HOST}:
{settings.DB_PORT}/{settings.DB_NAME}"""
DATABASE_PARAMS = {}
engine = create_async_engine(DATABASE_URL, **DATABASE_PARAMS)
# Создание ассинхронной сессии для БД

58
app/tests/conftest.py Normal file
View file

@ -0,0 +1,58 @@
import asyncio
import json
from datetime import datetime
import pytest
from sqlalchemy import insert
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
@pytest.fixture(autouse=True, scope='session')
async def prepare_database():
assert settings.MODE == "TEST"
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
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:
return json.load(file)
users = open_mock_json("users")
users_verification_codes = open_mock_json("verification_codes")
chats = open_mock_json("chats")
users_x_chats = open_mock_json("x_chats")
messages = open_mock_json("messages")
new_users = []
for i in users:
i['date_of_birth'] = datetime.strptime(i['date_of_birth'], '%Y-%m-%d')
# i['date_of_birth'] = i['date_of_birth'].isoformat()
new_users.append(i)
async with async_session_maker() as session:
add_users = insert(Users).values(new_users)
add_users_verification_codes = insert(UsersVerificationCodes).values(users_verification_codes)
add_chats = insert(Chats).values(chats)
add_users_x_chats = insert(UsersXChats).values(users_x_chats)
add_messages = insert(Messages).values(messages)
await session.execute(add_users)
await session.execute(add_users_verification_codes)
await session.execute(add_chats)
await session.execute(add_users_x_chats)
await session.execute(add_messages)
await session.commit()
@pytest.fixture(scope="session")
def event_loop(request):
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()

View file

@ -0,0 +1,2 @@
def test_abc():
assert 1 == 1

14
app/tests/mock_chats.json Normal file
View file

@ -0,0 +1,14 @@
[
{
"chat_name": "lox",
"chat_for": 2,
"visibility": true,
"created_by": 1
},
{
"chat_name": "lox",
"chat_for": 2,
"visibility": true,
"created_by": 1
}
]

View file

@ -0,0 +1,9 @@
[
{
"message": "ВАЫФ",
"image_url": null,
"chat_id": 1,
"user_id": 1,
"visibility": true
}
]

20
app/tests/mock_users.json Normal file
View file

@ -0,0 +1,20 @@
[
{
"email": "user@example.com",
"username": "string",
"hashed_password": "$2b$12$OkYfLiufUGGpxcJIC1TxQeEZNllHpS/jWICF9iRC7E/WTv7uFePbK",
"date_of_birth": "2024-02-13"
},
{
"email": "test@test.com",
"username": "test",
"hashed_password": "$2b$12$Q65fcP54s8gLeIjDo5EPLeyqD7oc8YFXl/mV1CDpnKvFKW8exGBOi",
"date_of_birth": "2024-02-14"
},
{
"email": "urec@urec.com",
"username": "urec",
"hashed_password": "$2b$12$sWscnmmhugSNJECjiz.j4eQK0vVYYA.fsYZD7a00WuJIsj9bDzj3m",
"date_of_birth": "2024-02-14"
}
]

View file

@ -0,0 +1,17 @@
[
{
"user_id": 1,
"code": "2vwQ6k",
"description": "Код подтверждения почты"
},
{
"user_id": 2,
"code": "W6VruA",
"description": "Код подтверждения почты"
},
{
"user_id": 3,
"code": "bWe93v",
"description": "Код подтверждения почты"
}
]

View file

@ -0,0 +1,10 @@
[
{
"user_id": 1,
"chat_id": 2
},
{
"user_id": 3,
"chat_id": 2
}
]

View file

@ -6,6 +6,7 @@ from sqlalchemy.orm import mapped_column, Mapped, relationship
from app.database import Base
class Chats(Base):
__tablename__ = "chats"
@ -20,7 +21,6 @@ class Chats(Base):
user_to_exclude = relationship("Users", primaryjoin='Chats.chat_for == Users.id', back_populates="chat")
user_who_create = relationship("Users", primaryjoin='Chats.created_by == Users.id', back_populates="creator")
def __str__(self):
return f"Чат #{self.id}. Виден: {self.visibility}. Сделан {self.created_by}"

View file

@ -1,10 +1,12 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
class SMessage(BaseModel):
model_config = ConfigDict(from_attributes=True)
message: Optional[str] = None
image_url: Optional[str] = None
chat_id: int
@ -13,9 +15,6 @@ class SMessage(BaseModel):
created_at: datetime
avatar_image: str
class Config:
from_attributes = True
class SLastMessages:
def __init__(

View file

@ -1,6 +1,6 @@
from datetime import date
from pydantic import BaseModel, EmailStr
from pydantic import BaseModel, EmailStr, ConfigDict
from fastapi import Query
@ -17,6 +17,8 @@ class SUserRegister(BaseModel):
class SUser(BaseModel):
model_config = ConfigDict(from_attributes=True)
email: EmailStr
id: int
username: str
@ -25,9 +27,6 @@ class SUser(BaseModel):
date_of_birth: date
date_of_registration: date
class Config:
from_attributes = True
class SUserName(BaseModel):
username: str = Query(None, min_length=2, max_length=30)
@ -37,4 +36,3 @@ class SUserPassword(BaseModel):
password: str = Query(None, min_length=8)
new_password: str = Query(None, min_length=8)
new_password2: str = Query(None, min_length=8)

4
pytest.ini Normal file
View file

@ -0,0 +1,4 @@
[pytest]
pythonpath = . app
asyncio_mode = auto
python_files = *_test.py *_tests.py test_*.py