Modernize bot for Python 3.12
This commit is contained in:
@@ -3,12 +3,10 @@
|
||||
Скрипт для очистки и инициализации БД с тестовыми данными
|
||||
"""
|
||||
import asyncio
|
||||
import random
|
||||
from database import async_session_maker, Base, engine
|
||||
from models import User, Lottery, Participation, Winner
|
||||
from services import LotteryService, UserService, ParticipationService
|
||||
from datetime import datetime, timedelta
|
||||
from sqlalchemy import delete, update, select, func
|
||||
from src.core.database import async_session_maker
|
||||
from src.core.models import User, Lottery, Participation, Winner, Account
|
||||
from src.core.services import LotteryService, UserService, ParticipationService
|
||||
from sqlalchemy import delete, select, func
|
||||
|
||||
async def clear_database():
|
||||
"""Очистка всех таблиц"""
|
||||
@@ -18,6 +16,7 @@ async def clear_database():
|
||||
# Удаляем все данные в правильном порядке (учитывая внешние ключи)
|
||||
await session.execute(delete(Winner))
|
||||
await session.execute(delete(Participation))
|
||||
await session.execute(delete(Account))
|
||||
await session.execute(delete(Lottery))
|
||||
await session.execute(delete(User))
|
||||
await session.commit()
|
||||
@@ -26,8 +25,10 @@ async def clear_database():
|
||||
|
||||
def generate_account_numbers(user_id: int, count: int = 10) -> list:
|
||||
"""Генерация номеров счетов для пользователя"""
|
||||
base = user_id * 1000
|
||||
return [f"{base + i:06d}" for i in range(1, count + 1)]
|
||||
return [
|
||||
"-".join(f"{part:02d}" for part in [user_id, i, 11, 22, 33, 44, 55])
|
||||
for i in range(1, count + 1)
|
||||
]
|
||||
|
||||
async def create_test_users():
|
||||
"""Создание тестовых пользователей"""
|
||||
@@ -62,12 +63,8 @@ async def create_test_users():
|
||||
# Генерируем и присваиваем номера счетов
|
||||
account_numbers = generate_account_numbers(i, 10)
|
||||
|
||||
# Обновляем счета
|
||||
await session.execute(
|
||||
update(User)
|
||||
.where(User.id == user.id)
|
||||
.values(account_number=",".join(account_numbers))
|
||||
)
|
||||
for account_number in account_numbers:
|
||||
await UserService.set_account_number(session, user.telegram_id, account_number)
|
||||
|
||||
await session.commit()
|
||||
created_users.append((user, account_numbers))
|
||||
@@ -194,4 +191,4 @@ async def main():
|
||||
print("\n🎉 Инициализация завершена!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
asyncio.run(main())
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
Примеры использования API сервисов для тестирования
|
||||
"""
|
||||
import asyncio
|
||||
from database import async_session_maker, init_db
|
||||
from services import UserService, LotteryService, ParticipationService
|
||||
from src.core.database import async_session_maker, init_db
|
||||
from src.core.services import UserService, LotteryService, ParticipationService
|
||||
|
||||
|
||||
async def example_usage():
|
||||
@@ -61,7 +61,7 @@ async def example_usage():
|
||||
|
||||
# Добавляем участников
|
||||
for user in users:
|
||||
success = await LotteryService.add_participant(session, lottery.id, user.id)
|
||||
success = await ParticipationService.add_participant(session, lottery.id, user.id)
|
||||
if success:
|
||||
print(f"✅ {user.first_name} присоединился к розыгрышу")
|
||||
|
||||
@@ -93,6 +93,7 @@ async def example_usage():
|
||||
|
||||
# Проводим розыгрыш
|
||||
results = await LotteryService.conduct_draw(session, lottery.id)
|
||||
await session.commit()
|
||||
|
||||
print(f"\n🏆 Результаты розыгрыша:")
|
||||
print(f"🎯 Розыгрыш: {lottery.title}")
|
||||
@@ -134,7 +135,7 @@ async def test_edge_cases():
|
||||
user = await UserService.get_user_by_telegram_id(session, 100000001)
|
||||
lottery = (await LotteryService.get_active_lotteries(session))[0]
|
||||
|
||||
success = await LotteryService.add_participant(session, lottery.id, user.id)
|
||||
success = await ParticipationService.add_participant(session, lottery.id, user.id)
|
||||
print(f"Повторное добавление участника: {'❌ Заблокировано' if not success else '⚠️ Разрешено'}")
|
||||
|
||||
# Попытка установить ручного победителя для несуществующего пользователя
|
||||
|
||||
Reference in New Issue
Block a user