284 lines
12 KiB
Python
284 lines
12 KiB
Python
"""
|
|
Скрипт для создания примеров данных для системы тестирования
|
|
Создает тестовые категории, тесты, вопросы и варианты ответов
|
|
"""
|
|
|
|
import sys
|
|
import os
|
|
from datetime import datetime
|
|
|
|
# Добавляем путь к корневой директории проекта
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from database.database import db, init_database
|
|
from database.models import TestCategory, Test, Question, AnswerOption, Image
|
|
from database.repositories import TestRepository, QuestionRepository, ImageRepository
|
|
from sqlalchemy.orm import Session
|
|
|
|
def create_sample_data():
|
|
"""Создание примеров данных для тестирования"""
|
|
|
|
print("Создание примеров данных для системы тестирования...")
|
|
|
|
# Инициализируем базу данных
|
|
init_database()
|
|
|
|
with db.get_session() as session:
|
|
# Создаем категории тестов
|
|
print("\n1. Создание категорий тестов...")
|
|
|
|
categories_data = [
|
|
{"name": "Программирование", "description": "Тесты по программированию и разработке"},
|
|
{"name": "Математика", "description": "Математические тесты и задачи"},
|
|
{"name": "Общие знания", "description": "Тесты на общую эрудицию"},
|
|
{"name": "История", "description": "Исторические факты и события"},
|
|
]
|
|
|
|
categories = {}
|
|
for cat_data in categories_data:
|
|
# Проверяем, есть ли уже такая категория
|
|
existing = session.query(TestCategory).filter(
|
|
TestCategory.name == cat_data["name"]
|
|
).first()
|
|
|
|
if not existing:
|
|
category = TestCategory(
|
|
name=cat_data["name"],
|
|
description=cat_data["description"]
|
|
)
|
|
session.add(category)
|
|
session.flush() # Получаем ID
|
|
categories[cat_data["name"]] = category
|
|
print(f" ✓ Создана категория: {cat_data['name']}")
|
|
else:
|
|
categories[cat_data["name"]] = existing
|
|
print(f" - Категория уже существует: {cat_data['name']}")
|
|
|
|
session.commit()
|
|
|
|
# Создаем тесты
|
|
print("\n2. Создание тестов...")
|
|
|
|
test_repo = TestRepository(session)
|
|
tests_data = [
|
|
{
|
|
"title": "Python для начинающих",
|
|
"description": "Основы программирования на Python",
|
|
"category": "Программирование",
|
|
"time_limit": 30
|
|
},
|
|
{
|
|
"title": "Базовая алгебра",
|
|
"description": "Простые алгебраические задачи",
|
|
"category": "Математика",
|
|
"time_limit": 45
|
|
},
|
|
{
|
|
"title": "Столицы мира",
|
|
"description": "Знание столиц различных стран",
|
|
"category": "Общие знания",
|
|
"time_limit": 15
|
|
},
|
|
]
|
|
|
|
tests = {}
|
|
for test_data in tests_data:
|
|
# Проверяем, есть ли уже такой тест
|
|
existing = session.query(Test).filter(Test.title == test_data["title"]).first()
|
|
|
|
if not existing:
|
|
test = test_repo.create_test(
|
|
title=test_data["title"],
|
|
description=test_data["description"],
|
|
category_id=categories[test_data["category"]].id,
|
|
time_limit_minutes=test_data["time_limit"]
|
|
)
|
|
tests[test_data["title"]] = test
|
|
print(f" ✓ Создан тест: {test_data['title']}")
|
|
else:
|
|
tests[test_data["title"]] = existing
|
|
print(f" - Тест уже существует: {test_data['title']}")
|
|
|
|
# Создаем вопросы и варианты ответов
|
|
print("\n3. Создание вопросов и вариантов ответов...")
|
|
|
|
question_repo = QuestionRepository(session)
|
|
|
|
# Вопросы для теста "Python для начинающих"
|
|
python_test = tests["Python для начинающих"]
|
|
python_questions = [
|
|
{
|
|
"text": "Какой оператор используется для присваивания в Python?",
|
|
"type": "single_choice",
|
|
"order": 1,
|
|
"points": 1,
|
|
"answers": [
|
|
{"text": "=", "correct": True, "order": 1},
|
|
{"text": "==", "correct": False, "order": 2},
|
|
{"text": ":=", "correct": False, "order": 3},
|
|
{"text": "->", "correct": False, "order": 4},
|
|
]
|
|
},
|
|
{
|
|
"text": "Как создать список в Python?",
|
|
"type": "single_choice",
|
|
"order": 2,
|
|
"points": 1,
|
|
"answers": [
|
|
{"text": "list = {}", "correct": False, "order": 1},
|
|
{"text": "list = []", "correct": True, "order": 2},
|
|
{"text": "list = ()", "correct": False, "order": 3},
|
|
{"text": "list = <>", "correct": False, "order": 4},
|
|
]
|
|
},
|
|
{
|
|
"text": "Какая функция используется для вывода текста в Python?",
|
|
"type": "single_choice",
|
|
"order": 3,
|
|
"points": 1,
|
|
"answers": [
|
|
{"text": "echo()", "correct": False, "order": 1},
|
|
{"text": "printf()", "correct": False, "order": 2},
|
|
{"text": "print()", "correct": True, "order": 3},
|
|
{"text": "write()", "correct": False, "order": 4},
|
|
]
|
|
}
|
|
]
|
|
|
|
_create_questions_for_test(session, question_repo, python_test.id, python_questions)
|
|
|
|
# Вопросы для теста "Базовая алгебра"
|
|
math_test = tests["Базовая алгебра"]
|
|
math_questions = [
|
|
{
|
|
"text": "Чему равно 2 + 2 * 3?",
|
|
"type": "single_choice",
|
|
"order": 1,
|
|
"points": 2,
|
|
"answers": [
|
|
{"text": "12", "correct": False, "order": 1},
|
|
{"text": "8", "correct": True, "order": 2},
|
|
{"text": "10", "correct": False, "order": 3},
|
|
{"text": "6", "correct": False, "order": 4},
|
|
]
|
|
},
|
|
{
|
|
"text": "Решите уравнение: x + 5 = 12",
|
|
"type": "single_choice",
|
|
"order": 2,
|
|
"points": 2,
|
|
"answers": [
|
|
{"text": "x = 5", "correct": False, "order": 1},
|
|
{"text": "x = 7", "correct": True, "order": 2},
|
|
{"text": "x = 17", "correct": False, "order": 3},
|
|
{"text": "x = 12", "correct": False, "order": 4},
|
|
]
|
|
}
|
|
]
|
|
|
|
_create_questions_for_test(session, question_repo, math_test.id, math_questions)
|
|
|
|
# Вопросы для теста "Столицы мира"
|
|
capitals_test = tests["Столицы мира"]
|
|
capitals_questions = [
|
|
{
|
|
"text": "Какая столица Франции?",
|
|
"type": "single_choice",
|
|
"order": 1,
|
|
"points": 1,
|
|
"answers": [
|
|
{"text": "Лондон", "correct": False, "order": 1},
|
|
{"text": "Париж", "correct": True, "order": 2},
|
|
{"text": "Берлин", "correct": False, "order": 3},
|
|
{"text": "Рим", "correct": False, "order": 4},
|
|
]
|
|
},
|
|
{
|
|
"text": "Столица Японии?",
|
|
"type": "single_choice",
|
|
"order": 2,
|
|
"points": 1,
|
|
"answers": [
|
|
{"text": "Осака", "correct": False, "order": 1},
|
|
{"text": "Киото", "correct": False, "order": 2},
|
|
{"text": "Токио", "correct": True, "order": 3},
|
|
{"text": "Хиросима", "correct": False, "order": 4},
|
|
]
|
|
}
|
|
]
|
|
|
|
_create_questions_for_test(session, question_repo, capitals_test.id, capitals_questions)
|
|
|
|
print(f"\n✅ Создание примеров данных завершено!")
|
|
print(f"Создано категорий: {len(categories)}")
|
|
print(f"Создано тестов: {len(tests)}")
|
|
|
|
# Выводим статистику
|
|
print("\n📊 Статистика:")
|
|
for test_name, test in tests.items():
|
|
question_count = session.query(Question).filter(Question.test_id == test.id).count()
|
|
print(f" - {test_name}: {question_count} вопросов")
|
|
|
|
|
|
def _create_questions_for_test(session: Session, question_repo: QuestionRepository, test_id: int, questions_data: list):
|
|
"""Вспомогательная функция для создания вопросов теста"""
|
|
|
|
for q_data in questions_data:
|
|
# Проверяем, есть ли уже такой вопрос
|
|
existing_question = session.query(Question).filter(
|
|
Question.test_id == test_id,
|
|
Question.order_number == q_data["order"]
|
|
).first()
|
|
|
|
if not existing_question:
|
|
question = question_repo.create_question(
|
|
test_id=test_id,
|
|
question_text=q_data["text"],
|
|
question_type=q_data["type"],
|
|
order_number=q_data["order"],
|
|
points=q_data["points"]
|
|
)
|
|
|
|
# Создаем варианты ответов
|
|
for answer_data in q_data["answers"]:
|
|
answer_option = AnswerOption(
|
|
question_id=question.id,
|
|
option_text=answer_data["text"],
|
|
is_correct=answer_data["correct"],
|
|
order_number=answer_data["order"]
|
|
)
|
|
session.add(answer_option)
|
|
|
|
session.commit()
|
|
print(f" ✓ Создан вопрос: {q_data['text'][:50]}...")
|
|
else:
|
|
print(f" - Вопрос уже существует: {q_data['text'][:50]}...")
|
|
|
|
|
|
def clear_test_data():
|
|
"""Очистка тестовых данных"""
|
|
print("\n🗑️ Очистка тестовых данных...")
|
|
|
|
with db.get_session() as session:
|
|
# Удаляем в правильном порядке (от дочерних к родительским)
|
|
session.query(AnswerOption).delete()
|
|
session.query(Question).delete()
|
|
session.query(Test).delete()
|
|
session.query(TestCategory).delete()
|
|
session.commit()
|
|
|
|
print("✅ Тестовые данные очищены")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(description="Управление примерами данных для системы тестирования")
|
|
parser.add_argument("--clear", action="store_true", help="Очистить все тестовые данные")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if args.clear:
|
|
clear_test_data()
|
|
else:
|
|
create_sample_data() |