سوالها از قبل فلهای وارد میشدند ولی دو شکاف مانده بود: افزودنِ تکی عملاً غیرقابلِ استفاده بود، و خودِ برنامهی درسی هیچ راهی جز ترمینالِ سرور نداشت. افزودنِ تکیِ سوال: انتخابِ درس یک دراپداونِ ۱۳۰۷تایی بود. حالا autocomplete است و بر اساسِ عنوانِ درس، فصل و پایه جستجو میشود. برنامهی درسی (Grade → Unit → Node → Lesson) حالا از /admin/content/grade/ وارد و خارج میشود. تا امروز رساندنش به سرور یعنی اجرای rag_import_collection و بعد seed_curriculum_from_textbook روی همان سرور — یعنی وابستگی به Qdrant و به آدمی با دسترسیِ SSH. حالا یکبار لوکال ساخته میشود و فایلش بالا میرود. همسانسازی با عنوان است نه با id: idها بینِ دو دیتابیس یکی نیستند و تکیه بر آنها یعنی وارد کردن روی سرور محتوای بیربط را بازنویسی میکند. وارد کردنِ برنامهی درسی عمداً چیزی پاک نمیکند و idempotent است — یک فایلِ ناقص نباید محتوای سرور را از بین ببرد، و اجرای دوباره نباید چیزی را دو بار بسازد. (وارد کردنِ سوالها اما replace دارد، چون جایگزینیِ سوالهای یک درس کارِ رایجی است.) Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
183 lines
8.0 KiB
Python
183 lines
8.0 KiB
Python
"""
|
||
تستهای وارد/خارج کردنِ سوالها.
|
||
|
||
نکتهی محوری: فایلِ بد نباید چیزی بنویسد. وارد کردنِ نیمهکاره بدترین حالت است
|
||
چون کسی که فایل را داده فکر میکند کار شده، ولی درس نصفه پر است.
|
||
"""
|
||
import json
|
||
|
||
from django.test import TestCase
|
||
|
||
from content.models import Choice, Grade, Lesson, Node, Question, Unit
|
||
from content.question_io import ImportError_, export_questions, import_questions, parse
|
||
|
||
|
||
class QuestionImportTests(TestCase):
|
||
def setUp(self):
|
||
grade = Grade.objects.create(title='پایه نهم', icon='', color='')
|
||
unit = Unit.objects.create(grade=grade, title='فصل ۱', order=1)
|
||
node = Node.objects.create(unit=unit, title='درس ۱', order=1)
|
||
self.lesson = Lesson.objects.create(node=node, title='نوع ۱')
|
||
|
||
def _payload(self, **over):
|
||
row = {
|
||
'lessonId': self.lesson.pk,
|
||
'prompt': 'حاصل ۲+۳ چند است؟',
|
||
'type': 'multiple_choice',
|
||
'choices': ['۴', '۵', '۶', '۷'],
|
||
'correct': 1,
|
||
}
|
||
row.update(over)
|
||
return json.dumps([row], ensure_ascii=False).encode()
|
||
|
||
def test_a_valid_file_creates_the_question_and_marks_one_choice_correct(self):
|
||
result = import_questions(parse(self._payload()))
|
||
self.assertEqual(result['created'], 1)
|
||
question = Question.objects.get()
|
||
self.assertEqual(question.choices.count(), 4)
|
||
self.assertEqual(question.choices.filter(is_correct=True).get().value, '۵')
|
||
|
||
def test_an_unknown_lesson_is_rejected_before_anything_is_written(self):
|
||
with self.assertRaises(ImportError_):
|
||
import_questions(parse(self._payload(lessonId=999999)))
|
||
self.assertEqual(Question.objects.count(), 0)
|
||
|
||
def test_a_bad_row_in_the_middle_rolls_the_whole_file_back(self):
|
||
good = json.loads(self._payload())[0]
|
||
bad = dict(good, lessonId=999999)
|
||
raw = json.dumps([good, bad], ensure_ascii=False).encode()
|
||
with self.assertRaises(ImportError_):
|
||
import_questions(parse(raw))
|
||
self.assertEqual(Question.objects.count(), 0, 'نباید نیمهکاره بنویسد')
|
||
|
||
def test_correct_out_of_range_is_rejected(self):
|
||
with self.assertRaises(ImportError_):
|
||
parse(self._payload(correct=9))
|
||
|
||
def test_duplicate_choices_are_rejected(self):
|
||
with self.assertRaises(ImportError_):
|
||
parse(self._payload(choices=['۵', '۵', '۶', '۷']))
|
||
|
||
def test_broken_json_says_where_it_broke(self):
|
||
with self.assertRaises(ImportError_) as ctx:
|
||
parse(b'[{"lessonId": 1,,}]')
|
||
self.assertIn('JSON', str(ctx.exception))
|
||
|
||
def test_replace_clears_the_previous_questions_of_that_lesson(self):
|
||
import_questions(parse(self._payload()))
|
||
import_questions(parse(self._payload(prompt='سوال تازه')), replace=True)
|
||
self.assertEqual(Question.objects.count(), 1)
|
||
self.assertEqual(Question.objects.get().prompt, 'سوال تازه')
|
||
|
||
def test_export_round_trips_back_through_import(self):
|
||
import_questions(parse(self._payload()))
|
||
exported = export_questions(Question.objects.all())
|
||
Question.objects.all().delete()
|
||
import_questions(parse(json.dumps(exported, ensure_ascii=False).encode()))
|
||
self.assertEqual(Question.objects.get().choices.filter(is_correct=True).get().value, '۵')
|
||
|
||
|
||
class HealthEndpointTests(TestCase):
|
||
def test_reports_ok_and_counts_content(self):
|
||
response = self.client.get('/api/health')
|
||
self.assertEqual(response.status_code, 200)
|
||
body = response.json()
|
||
self.assertEqual(body['status'], 'ok')
|
||
self.assertTrue(body['checks']['database']['ok'])
|
||
self.assertTrue(body['checks']['migrations']['ok'])
|
||
self.assertIn('questions', body['checks']['content'])
|
||
|
||
def test_never_leaks_a_secret(self):
|
||
raw = self.client.get('/api/health').content.decode()
|
||
for needle in ('sk-', 'SECRET', 'API_KEY', 'password'):
|
||
self.assertNotIn(needle, raw)
|
||
|
||
def test_needs_no_authentication(self):
|
||
self.assertEqual(self.client.get('/api/health').status_code, 200)
|
||
|
||
|
||
class CurriculumImportTests(TestCase):
|
||
"""
|
||
برنامهی درسی باید بدونِ Qdrant و بدونِ ترمینال به سرور برسد.
|
||
|
||
مهمترین خاصیتش این است که چیزی پاک نمیکند: یک فایلِ ناقص نباید محتوای
|
||
سرور را از بین ببرد.
|
||
"""
|
||
|
||
SAMPLE = {
|
||
'grades': [{
|
||
'title': 'پایه نهم', 'icon': 'grade-9', 'color': '#FF9800',
|
||
'units': [{
|
||
'title': 'فصل ۴: توان و ریشه', 'order': 4,
|
||
'nodes': [{
|
||
'title': 'توان صحیح', 'order': 1,
|
||
'lessons': [{'title': 'ضرب توانها', 'introText': 'پایهها برابرند.'}],
|
||
}],
|
||
}],
|
||
}],
|
||
}
|
||
|
||
def _raw(self, payload=None):
|
||
return json.dumps(payload or self.SAMPLE, ensure_ascii=False).encode()
|
||
|
||
def test_builds_the_whole_tree(self):
|
||
from content.curriculum_io import import_curriculum
|
||
from content.curriculum_io import parse as parse_curriculum
|
||
|
||
counts = import_curriculum(parse_curriculum(self._raw()))
|
||
self.assertEqual((counts['grades'], counts['units'], counts['nodes'], counts['lessons']),
|
||
(1, 1, 1, 1))
|
||
self.assertEqual(Lesson.objects.get().intro_text, 'پایهها برابرند.')
|
||
|
||
def test_running_it_twice_creates_nothing_new(self):
|
||
from content.curriculum_io import import_curriculum
|
||
from content.curriculum_io import parse as parse_curriculum
|
||
|
||
import_curriculum(parse_curriculum(self._raw()))
|
||
counts = import_curriculum(parse_curriculum(self._raw()))
|
||
self.assertEqual(counts['lessons'], 0)
|
||
self.assertEqual(Lesson.objects.count(), 1)
|
||
|
||
def test_a_partial_file_never_deletes_what_is_already_there(self):
|
||
from content.curriculum_io import import_curriculum
|
||
from content.curriculum_io import parse as parse_curriculum
|
||
|
||
import_curriculum(parse_curriculum(self._raw()))
|
||
smaller = json.loads(json.dumps(self.SAMPLE))
|
||
smaller['grades'][0]['units'] = []
|
||
import_curriculum(parse_curriculum(self._raw(smaller)))
|
||
self.assertEqual(Lesson.objects.count(), 1, 'محتوای موجود نباید پاک شود')
|
||
|
||
def test_a_missing_title_is_rejected_with_a_readable_message(self):
|
||
from content.curriculum_io import CurriculumError
|
||
from content.curriculum_io import parse as parse_curriculum
|
||
|
||
broken = json.loads(json.dumps(self.SAMPLE))
|
||
broken['grades'][0]['units'][0]['nodes'][0]['title'] = ''
|
||
with self.assertRaises(CurriculumError) as ctx:
|
||
parse_curriculum(self._raw(broken))
|
||
self.assertIn('درس', str(ctx.exception))
|
||
|
||
def test_export_round_trips_back_through_import(self):
|
||
from content.curriculum_io import export_curriculum, import_curriculum
|
||
from content.curriculum_io import parse as parse_curriculum
|
||
|
||
import_curriculum(parse_curriculum(self._raw()))
|
||
exported = export_curriculum(Grade.objects.all())
|
||
Grade.objects.all().delete()
|
||
import_curriculum(parse_curriculum(json.dumps(exported, ensure_ascii=False).encode()))
|
||
self.assertEqual(Lesson.objects.get().title, 'ضرب توانها')
|
||
|
||
|
||
class HealthWarningTests(TestCase):
|
||
def test_debug_mode_is_reported_as_a_warning(self):
|
||
with self.settings(DEBUG=True, DEV_MASTER_OTP='0000'):
|
||
body = self.client.get('/api/health').json()
|
||
joined = ' '.join(body['warnings'])
|
||
self.assertIn('DEBUG', joined)
|
||
self.assertIn('0000', joined)
|
||
|
||
def test_allowed_origins_include_the_deployed_frontend(self):
|
||
body = self.client.get('/api/health').json()
|
||
self.assertIn('https://math.tadrisino2.ir', body['allowedOrigins'])
|