#!/usr/bin/env python """ Скрипт для заполнения базы данных тестовыми данными """ import asyncio import sys from pathlib import Path from datetime import datetime sys.path.append(str(Path(__file__).parent.parent)) from app.database import AsyncSessionLocal from app.models.form import Form, Field, FormField from app.models.submission import Submission from app.schemas.field import FieldType import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) async def seed_database(): """Заполнение тестовыми данными""" async with AsyncSessionLocal() as db: try: # Создаем тестовые поля fields_data = [ {"name": "full_name", "label": "Full Name", "field_type": FieldType.TEXT}, {"name": "email", "label": "Email Address", "field_type": FieldType.EMAIL}, {"name": "age", "label": "Age", "field_type": FieldType.NUMBER}, {"name": "country", "label": "Country", "field_type": FieldType.SELECT, "options": {"options": ["USA", "UK", "Canada", "Australia"]}}, ] fields = [] for field_data in fields_data: field = Field(**field_data) db.add(field) fields.append(field) await db.flush() # Создаем тестовую форму form = Form( name="Test Registration Form", description="Test form for development", is_active=True, is_published=True ) db.add(form) await db.flush() # Связываем поля с формой for i, field in enumerate(fields): form_field = FormField( form_id=form.id, field_id=field.id, order=i, is_required=True if i < 2 else False ) db.add(form_field) # Создаем тестовые submission'ы test_data = [ {"full_name": "John Doe", "email": "john@example.com", "age": 30, "country": "USA"}, {"full_name": "Jane Smith", "email": "jane@example.com", "age": 25, "country": "UK"}, {"full_name": "Bob Johnson", "email": "bob@example.com", "age": 35, "country": "Canada"}, ] for data in test_data: submission = Submission( form_id=form.id, data=data, status="completed", submitted_at=datetime.utcnow() ) db.add(submission) await db.commit() logger.info( f"Test data seeded successfully! Created form '{form.name}' with {len(fields)} fields and {len(test_data)} submissions") except Exception as e: await db.rollback() logger.error(f"Failed to seed database: {e}") raise if __name__ == "__main__": asyncio.run(seed_database())