Skip to content

Commit

Permalink
test: simplify mathjax eqn representation and added some more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Jan 2, 2025
1 parent 8d8319e commit a335a9d
Showing 1 changed file with 79 additions and 66 deletions.
145 changes: 79 additions & 66 deletions openedx/core/djangoapps/content/search/tests/test_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,79 +483,92 @@ def test_mathjax_plain_text_conversion_for_search(self):
Test how an HTML block with mathjax equations gets converted to plain text in search description.
"""
# pylint: disable=line-too-long
eqns = [
# (input, expected output)
('Simple addition: \\( 2 + 3 \\)', 'Simple addition: 2 + 3'),
('Simple subtraction: \\( 5 - 2 \\)', 'Simple subtraction: 5 − 2'),
('Simple multiplication: \\( 4 * 6 \\)', 'Simple multiplication: 4 * 6'),
('Simple division: \\( 8 / 2 \\)', 'Simple division: 8 / 2'),
('Mixed arithmetic: \\( 2 + 3 4 \\)', 'Mixed arithmetic: 2 + 3 4'),
('Simple exponentiation: \\[ 2^3 \\]', 'Simple exponentiation: 2³'),
('Root extraction: \\[ 16^{1/2} \\]', 'Root extraction: 16¹^/²'),
('Exponent with multiple terms: \\[ (2 + 3)^2 \\]', 'Exponent with multiple terms: (2 + 3)²'),
('Nested exponents: \\[ 2^(3^2) \\]', 'Nested exponents: 2⁽3²)'),
('Mixed roots: \\[ 8^{1/2} 3^2 \\]', 'Mixed roots: 8¹^/² 3²'),
('Simple fraction: [mathjaxinline] 3/4 [/mathjaxinline]', 'Simple fraction: 3/4'),
(
'Decimal to fraction conversion: [mathjaxinline] 0.75 = 3/4 [/mathjaxinline]',
'Decimal to fraction conversion: 0.75 = 3/4',
),
('Mixed fractions: [mathjaxinline] 1 1/2 = 3/2 [/mathjaxinline]', 'Mixed fractions: 1 1/2 = 3/2'),
(
'Converting decimals to mixed fractions: [mathjaxinline] 2.5 = 5/2 [/mathjaxinline]',
'Converting decimals to mixed fractions: 2.5 = 5/2',
),
(
'Trig identities: [mathjaxinline] \\sin(x + y) = \\sin(x) \\cos(y) + \\cos(x) \\sin(y) [/mathjaxinline]',
'Trig identities: sin(x + y) = sin(x) cos(y) + cos(x) sin(y)',
),
(
'Sine, cosine, and tangent: [mathjaxinline] \\sin(x) [/mathjaxinline] [mathjaxinline] \\cos(x) [/mathjaxinline] [mathjaxinline] \\tan(x) [/mathjaxinline]',
'Sine, cosine, and tangent: sin(x) cos(x) tan(x)',
),
(
'Hyperbolic trig functions: [mathjaxinline] \\sinh(x) [/mathjaxinline] [mathjaxinline] \\cosh(x) [/mathjaxinline]',
'Hyperbolic trig functions: sinh(x) cosh(x)',
),
(
"Simple derivative: [mathjax] f(x) = x^2, f'(x) = 2x [/mathjax]",
"Simple derivative: f(x) = x², f'(x) = 2x",
),
('Double integral: [mathjax] int\\int (x + y) dxdy [/mathjax]', 'Double integral: int∫ (x + y) dxdy'),
(
'Partial derivatives: [mathjax] f(x,y) = xy, \\frac{\\partial f}{\\partial x} = y [/mathjax] [mathjax] \\frac{\\partial f}{\\partial y} = x [/mathjax]',
'Partial derivatives: f(x,y) = xy, (∂ f/∂ x) = y (∂ f/∂ y) = x',
),
(
'Mean and standard deviation: [mathjax] mu = 2, \\sigma = 1 [/mathjax]',
'Mean and standard deviation: mu = 2, σ = 1',
),
(
'Binomial probability: [mathjax] P(X = k) = (\\binom{n}{k} p^k (1-p)^{n-k}) [/mathjax]',
'Binomial probability: P(X = k) = (\\binom{n}{k} pᵏ (1−p)ⁿ⁻ᵏ)',
),
('Gaussian distribution: [mathjax] N(\\mu, \\sigma^2) [/mathjax]', 'Gaussian distribution: N(μ, σ²)'),
(
'Greek letters: [mathjaxinline] \\alpha [/mathjaxinline] [mathjaxinline] \\beta [/mathjaxinline] [mathjaxinline] \\gamma [/mathjaxinline]',
'Greek letters: α β γ',
),
(
'Subscripted variables: [mathjaxinline] x_i [/mathjaxinline] [mathjaxinline] y_j [/mathjaxinline]',
'Subscripted variables: xᵢ yⱼ',
),
('Superscripted variables: [mathjaxinline] x^{i} [/mathjaxinline]', 'Superscripted variables: xⁱ'),
(
'Not supported: \\( \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I \\)',
'Not supported: \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I',
),
(
'Bold text: \\( {\\bf a} \\cdot {\\bf b} = |{\\bf a}| |{\\bf b}| \\cos(\\theta) \\)',
'Bold text: a ⋅ b = |a| |b| cos(θ)',
),
('Bold text: \\( \\frac{\\sqrt{\\mathbf{2}+3}}{\\sqrt{4}} \\)', 'Bold text: (√{2+3}/√{4})'),
('Sqrt test 1: \\(\\sqrt\\)', 'Sqrt test 1: √'),
('Sqrt test 2: \\(x^2 + \\sqrt(y)\\)', 'Sqrt test 2: x² + √(y)'),
('Sqrt test 3: [mathjaxinline]x^2 + \\sqrt(y)[/mathjaxinline]', 'Sqrt test 3: x² + √(y)'),
]
# pylint: enable=line-too-long
block = BlockFactory.create(
parent_location=self.toy_course.location,
category="html",
display_name="Non-default HTML Block",
editor="raw",
use_latex_compiler=True,
data=(
"Simple addition: \\( 2 + 3 \\) |||"
" Simple subtraction: \\( 5 - 2 \\) |||"
" Simple multiplication: \\( 4 * 6 \\) |||"
" Simple division: \\( 8 / 2 \\) |||"
" Mixed arithmetic: \\( 2 + 3 4 \\) |||"
" Simple exponentiation: \\[ 2^3 \\] |||"
" Root extraction: \\[ 16^{1/2} \\] |||"
" Exponent with multiple terms: \\[ (2 + 3)^2 \\] |||"
" Nested exponents: \\[ 2^(3^2) \\] |||"
" Mixed roots: \\[ 8^{1/2} 3^2 \\] |||"
" Simple fraction: [mathjaxinline] 3/4 [/mathjaxinline] |||"
" Decimal to fraction conversion: [mathjaxinline] 0.75 = 3/4 [/mathjaxinline] |||"
" Mixed fractions: [mathjaxinline] 1 1/2 = 3/2 [/mathjaxinline] |||"
" Converting decimals to mixed fractions: [mathjaxinline] 2.5 = 5/2 [/mathjaxinline] |||"
" Sine, cosine, and tangent: [mathjaxinline] \\sin(x) [/mathjaxinline] [mathjaxinline] \\cos(x) [/mathjaxinline] [mathjaxinline] \\tan(x) [/mathjaxinline] |||"
" Trig identities: [mathjaxinline] \\sin(x + y) = \\sin(x) \\cos(y) + \\cos(x) \\sin(y) [/mathjaxinline] |||"
" Hyperbolic trig functions: [mathjaxinline] \\sinh(x) [/mathjaxinline] [mathjaxinline] \\cosh(x) [/mathjaxinline] |||"
" Simple derivative: [mathjax] f(x) = x^2, f'(x) = 2x [/mathjax] |||"
" Double integral: [mathjax] int\\int (x + y) dxdy [/mathjax] |||"
" Partial derivatives: [mathjax] f(x,y) = xy, \\frac{\\partial f}{\\partial x} = y [/mathjax] [mathjax] \\frac{\\partial f}{\\partial y} = x [/mathjax] |||"
" Mean and standard deviation: [mathjax] mu = 2, \\sigma = 1 [/mathjax] |||"
" Binomial probability: [mathjax] P(X = k) = (\\binom{n}{k} p^k (1-p)^{n-k}) [/mathjax] |||"
" Gaussian distribution: [mathjax] N(\\mu, \\sigma^2) [/mathjax] |||"
" Greek letters: [mathjaxinline] \\alpha [/mathjaxinline] [mathjaxinline] \\beta [/mathjaxinline] [mathjaxinline] \\gamma [/mathjaxinline] |||"
" Subscripted variables: [mathjaxinline] x_i [/mathjaxinline] [mathjaxinline] y_j [/mathjaxinline] |||"
" Superscripted variables: [mathjaxinline] x^{i} [/mathjaxinline] |||"
" Not supported: \\( \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I \\) |||"
" Bold text: \\( {\\bf a} \\cdot {\\bf b} = |{\\bf a}| |{\\bf b}| \\cos(\\theta) \\) |||"
" Bold text: \\( \\frac{\\sqrt{\\mathbf{2}+3}}{\\sqrt{4}} \\)"
),
data="|||".join(e[0] for e in eqns),
)
# pylint: enable=line-too-long
doc = {}
doc.update(searchable_doc_for_course_block(block))
doc.update(searchable_doc_tags(block.usage_key))
expected_equations = [
'Simple addition: 2 + 3',
'Simple subtraction: 5 − 2',
'Simple multiplication: 4 * 6',
'Simple division: 8 / 2',
'Mixed arithmetic: 2 + 3 4',
'Simple exponentiation: 2³',
'Root extraction: 16¹^/²',
'Exponent with multiple terms: (2 + 3)²',
'Nested exponents: 2⁽3²)',
'Mixed roots: 8¹^/² 3²',
'Simple fraction: 3/4',
'Decimal to fraction conversion: 0.75 = 3/4',
'Mixed fractions: 1 1/2 = 3/2',
'Converting decimals to mixed fractions: 2.5 = 5/2',
'Sine, cosine, and tangent: sin(x) cos(x) tan(x)',
'Trig identities: sin(x + y) = sin(x) cos(y) + cos(x) sin(y)',
'Hyperbolic trig functions: sinh(x) cosh(x)',
"Simple derivative: f(x) = x², f'(x) = 2x",
'Double integral: int∫ (x + y) dxdy',
'Partial derivatives: f(x,y) = xy, (∂ f/∂ x) = y (∂ f/∂ y) = x',
'Mean and standard deviation: mu = 2, σ = 1',
'Binomial probability: P(X = k) = (\\binom{n}{k} pᵏ (1−p)ⁿ⁻ᵏ)',
'Gaussian distribution: N(μ, σ²)',
'Greek letters: α β γ',
'Subscripted variables: xᵢ yⱼ',
'Superscripted variables: xⁱ',
'Not supported: \\begin{bmatrix} 1 & 0 \\ 0 & 1 \\end{bmatrix} = I',
'Bold text: a ⋅ b = |a| |b| cos(θ)',
'Bold text: (√{2+3}/√{4})',
]
eqns = doc['description'].split('|||')
for i, eqn in enumerate(eqns):
assert eqn.strip() == expected_equations[i]
result = doc['description'].split('|||')
for i, eqn in enumerate(result):
assert eqn.strip() == eqns[i][1]

0 comments on commit a335a9d

Please sign in to comment.