Files
MathApp/content/tests_plain_math.py

69 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import json
from pathlib import Path
from django.test import SimpleTestCase
from content.plain_math import convert, convert_figures, leftover
SEED = Path(__file__).parent / 'seed' / 'curriculum.json'
class ConvertTests(SimpleTestCase):
def test_vector_and_sets(self):
self.assertEqual(convert(r'بردار \( \vec{a} \) و \( \vec{AB} \)'), 'بردار a و AB')
self.assertEqual(convert(r'از \( \mathbb{Q} \) و \( \mathbb{R} \)'), 'از ℚ و ℝ')
def test_norm_sqrt_powers(self):
self.assertEqual(convert(r'\( \|\vec{a}\| = \sqrt{x^2 + y^2} \)'), '|a| = √(x² + y²)')
self.assertEqual(convert('(−۱)^{۲+۲}'), '(−۱)²⁺²')
def test_unmappable_script_is_kept_readable_not_dropped(self):
self.assertEqual(convert('D_{g}'), 'Dg')
self.assertEqual(convert('a^b'), 'aᵇ')
def test_blank_placeholder_untouched(self):
self.assertEqual(convert('3، 6، ___، 12'), '3، 6، ___، 12')
def test_idempotent(self):
s = r'\( \frac{1}{2} \vec{a}^{n} \)'
self.assertEqual(convert(convert(s)), convert(s))
def test_figure_expr_is_never_rewritten(self):
fig = [{'diagram': {'items': [{'type': 'function', 'expr': 'x^2', 'label': 'f(x)=x^2'}]}}]
out = convert_figures(fig)[0]['diagram']['items'][0]
self.assertEqual(out['expr'], 'x^2')
self.assertEqual(out['label'], 'f(x)=x²')
class MarkupTests(SimpleTestCase):
def test_caret_markdown_and_integral(self):
self.assertEqual(convert('(۲/۳)^(−۴)'), '(۲/۳)⁻⁴')
self.assertEqual(convert('**تمرین:** حل کنید'), 'تمرین: حل کنید')
self.assertEqual(convert(r'\int_0^1 x\,dx'), '∫₀¹ x dx')
self.assertEqual(convert(r'$\vec{a}\cdot\vec{b}$'), 'a·b')
self.assertEqual(leftover(convert('x^2y^3 و log_a b')), [])
class SeedHasNoLatexTests(SimpleTestCase):
"""گزارشِ مالک ۲۰۲۶-۱۰-۱۲: LaTeX خام در درس‌های بردار. فرانت LaTeX رندر نمی‌کند."""
def test_no_latex_in_lesson_text(self):
data = json.loads(SEED.read_text(encoding='utf-8'))
bad = []
for g in data['grades']:
for u in g['units']:
for n in u['nodes']:
for l in n['lessons']:
texts = [l.get('introText'), l.get('example'), *(l.get('steps') or [])]
for t in texts:
if leftover(t):
bad.append((l['title'], leftover(t)))
self.assertEqual(bad, [])
class SeedQuestionsHaveNoMarkupTests(SimpleTestCase):
def test_no_markup_in_questions(self):
rows = json.loads((SEED.parent / 'questions.json').read_text(encoding='utf-8'))['questions']
bad = [t for r in rows for t in [r['prompt'], *r['choices']] if leftover(t)]
self.assertEqual(bad[:5], [])