fix: Restructuring to send course_id and score to edx submission fix: Refactoring of sending information to sent_to_submission fix: Elimination of unnecessary functions fix: Added usage comment to ProblemBlock in XqueueInterface constructor fix: update doc ADR fix: setting for Quality Others test (ubuntu-24.04, 3.11, 20) fix: Deprecation for django-trans-escape-filter-parse-error test: Add @pytest.mark.django_db decorator to test functions test: Fix for pylint being disabled fix: updated changes for pylint disable fix: update error from docs ADR-0005 update: xmodule/docs/decisions/0005-send-data-to-edx-submission.rst Co-authored-by: Sarina Canelake <sarina@axim.org> update: xmodule/docs/decisions/0005-send-data-to-edx-submission.rst Co-authored-by: Sarina Canelake <sarina@axim.org> fix: Adjusted correction fix: update date for docs ADR Revert "fix: update date for docs ADR" This reverts commit 0b4229c51c4937f95cb407872645dd448df45418. fix: replace call created_submission to create_external_grader_detail fix: update test xqueue_submission fix: add docstring in test_xqueue_submission fix: update date doc ADR fix: update version edx-submission 3.8.6 fix: add @pytest.mark.xfail fix: add 20 chances in test_capa_block: fix: increase retry attempts for seed generation in ProblemBlockTest fix: change version to edx-submission lib fix: new version edx-submission in testings fix: replace parameter file to files fix: update variable grader_file_name and points_possible fix: Adjustment in the is_flag_active function to always take the last record edited in the waffle fix: wrap large line of code fix: update function is_flag_active fix: code style adjustment fix: changes for 60 retry feat: use CourseWaffleFlag to determine xqueue callback path fix: Code style adjustment fix: remove deprecated xqueue callback route and simplify callback type logic fix: Deleting a comment in the ADR document fix: add log in self.block is None fix: Code style adjustment in log
170 lines
6.6 KiB
Python
170 lines
6.6 KiB
Python
# coding=utf-8
|
||
"""
|
||
Tests capa util
|
||
"""
|
||
|
||
|
||
import unittest
|
||
|
||
import ddt
|
||
from lxml import etree
|
||
|
||
from xmodule.capa.tests.helpers import test_capa_system
|
||
from xmodule.capa.util import (
|
||
compare_with_tolerance,
|
||
contextualize_text,
|
||
get_inner_html_from_xpath,
|
||
remove_markup,
|
||
sanitize_html
|
||
)
|
||
|
||
|
||
@ddt.ddt
|
||
class UtilTest(unittest.TestCase):
|
||
"""Tests for util"""
|
||
|
||
def setUp(self):
|
||
super(UtilTest, self).setUp() # lint-amnesty, pylint: disable=super-with-arguments
|
||
self.system = test_capa_system()
|
||
|
||
def test_compare_with_tolerance(self): # lint-amnesty, pylint: disable=too-many-statements
|
||
# Test default tolerance '0.001%' (it is relative)
|
||
result = compare_with_tolerance(100.0, 100.0)
|
||
assert result
|
||
result = compare_with_tolerance(100.001, 100.0)
|
||
assert result
|
||
result = compare_with_tolerance(101.0, 100.0)
|
||
assert not result
|
||
# Test absolute percentage tolerance
|
||
result = compare_with_tolerance(109.9, 100.0, '10%', False)
|
||
assert result
|
||
result = compare_with_tolerance(110.1, 100.0, '10%', False)
|
||
assert not result
|
||
# Test relative percentage tolerance
|
||
result = compare_with_tolerance(111.0, 100.0, '10%', True)
|
||
assert result
|
||
result = compare_with_tolerance(112.0, 100.0, '10%', True)
|
||
assert not result
|
||
# Test absolute tolerance (string)
|
||
result = compare_with_tolerance(109.9, 100.0, '10.0', False)
|
||
assert result
|
||
result = compare_with_tolerance(110.1, 100.0, '10.0', False)
|
||
assert not result
|
||
# Test relative tolerance (string)
|
||
result = compare_with_tolerance(111.0, 100.0, '0.1', True)
|
||
assert result
|
||
result = compare_with_tolerance(112.0, 100.0, '0.1', True)
|
||
assert not result
|
||
# Test absolute tolerance (float)
|
||
result = compare_with_tolerance(109.9, 100.0, 10.0, False)
|
||
assert result
|
||
result = compare_with_tolerance(110.1, 100.0, 10.0, False)
|
||
assert not result
|
||
# Test relative tolerance (float)
|
||
result = compare_with_tolerance(111.0, 100.0, 0.1, True)
|
||
assert result
|
||
result = compare_with_tolerance(112.0, 100.0, 0.1, True)
|
||
assert not result
|
||
##### Infinite values #####
|
||
infinity = float('Inf')
|
||
# Test relative tolerance (float)
|
||
result = compare_with_tolerance(infinity, 100.0, 1.0, True)
|
||
assert not result
|
||
result = compare_with_tolerance(100.0, infinity, 1.0, True)
|
||
assert not result
|
||
result = compare_with_tolerance(infinity, infinity, 1.0, True)
|
||
assert result
|
||
# Test absolute tolerance (float)
|
||
result = compare_with_tolerance(infinity, 100.0, 1.0, False)
|
||
assert not result
|
||
result = compare_with_tolerance(100.0, infinity, 1.0, False)
|
||
assert not result
|
||
result = compare_with_tolerance(infinity, infinity, 1.0, False)
|
||
assert result
|
||
# Test relative tolerance (string)
|
||
result = compare_with_tolerance(infinity, 100.0, '1.0', True)
|
||
assert not result
|
||
result = compare_with_tolerance(100.0, infinity, '1.0', True)
|
||
assert not result
|
||
result = compare_with_tolerance(infinity, infinity, '1.0', True)
|
||
assert result
|
||
# Test absolute tolerance (string)
|
||
result = compare_with_tolerance(infinity, 100.0, '1.0', False)
|
||
assert not result
|
||
result = compare_with_tolerance(100.0, infinity, '1.0', False)
|
||
assert not result
|
||
result = compare_with_tolerance(infinity, infinity, '1.0', False)
|
||
assert result
|
||
# Test absolute tolerance for smaller values
|
||
result = compare_with_tolerance(100.01, 100.0, 0.01, False)
|
||
assert result
|
||
result = compare_with_tolerance(100.001, 100.0, 0.001, False)
|
||
assert result
|
||
result = compare_with_tolerance(100.01, 100.0, '0.01%', False)
|
||
assert result
|
||
result = compare_with_tolerance(100.002, 100.0, 0.001, False)
|
||
assert not result
|
||
result = compare_with_tolerance(0.4, 0.44, 0.01, False)
|
||
assert not result
|
||
result = compare_with_tolerance(100.01, 100.0, 0.010, False)
|
||
assert result
|
||
|
||
# Test complex_number instructor_complex
|
||
result = compare_with_tolerance(0.4, complex(0.44, 0), 0.01, False)
|
||
assert not result
|
||
result = compare_with_tolerance(100.01, complex(100.0, 0), 0.010, False)
|
||
assert result
|
||
result = compare_with_tolerance(110.1, complex(100.0, 0), '10.0', False)
|
||
assert not result
|
||
result = compare_with_tolerance(111.0, complex(100.0, 0), '10%', True)
|
||
assert result
|
||
|
||
def test_sanitize_html(self):
|
||
"""
|
||
Test for html sanitization with nh3.
|
||
"""
|
||
allowed_tags = ['div', 'p', 'audio', 'pre', 'span']
|
||
for tag in allowed_tags:
|
||
queue_msg = "<{0}>Test message</{0}>".format(tag)
|
||
assert sanitize_html(queue_msg) == queue_msg
|
||
|
||
not_allowed_tag = 'script'
|
||
queue_msg = "<{0}>Test message</{0}>".format(not_allowed_tag)
|
||
expected = ""
|
||
assert sanitize_html(queue_msg) == expected
|
||
|
||
def test_get_inner_html_from_xpath(self):
|
||
"""
|
||
Test for getting inner html as string from xpath node.
|
||
"""
|
||
xpath_node = etree.XML('<hint style="smtng">aa<a href="#">bb</a>cc</hint>')
|
||
assert get_inner_html_from_xpath(xpath_node) == 'aa<a href="#">bb</a>cc'
|
||
|
||
def test_remove_markup(self):
|
||
"""
|
||
Test for markup removal with nh3.
|
||
"""
|
||
assert remove_markup('The <mark>Truth</mark> is <em>Out There</em> & you need to <strong>find</strong> it') ==\
|
||
'The Truth is Out There & you need to find it'
|
||
|
||
@ddt.data(
|
||
'When the root level failš the whole hierarchy won’t work anymore.',
|
||
'あなたあなたあなた'
|
||
)
|
||
def test_contextualize_text(self, context_value):
|
||
"""Verify that variable substitution works as intended with non-ascii characters."""
|
||
key = 'answer0'
|
||
text = '$answer0'
|
||
context = {key: context_value}
|
||
contextual_text = contextualize_text(text, context)
|
||
assert context_value == contextual_text
|
||
|
||
def test_contextualize_text_with_non_ascii_context(self):
|
||
"""Verify that variable substitution works as intended with non-ascii characters."""
|
||
key = 'あなた$a $b'
|
||
text = '$' + key
|
||
context = {'a': 'あなたあなたあなた', 'b': 'あなたhi'}
|
||
expected_text = '$あなたあなたあなたあなた あなたhi'
|
||
contextual_text = contextualize_text(text, context)
|
||
assert expected_text == contextual_text
|