Move helper methods from the capa tests module __init__ file
This commit is contained in:
@@ -1,103 +0,0 @@
|
||||
"""Tools for helping with testing capa."""
|
||||
|
||||
import gettext
|
||||
from path import path # pylint: disable=no-name-in-module
|
||||
import os
|
||||
import os.path
|
||||
|
||||
import fs.osfs
|
||||
|
||||
from capa.capa_problem import LoncapaProblem, LoncapaSystem
|
||||
from capa.inputtypes import Status
|
||||
from mock import Mock, MagicMock
|
||||
from mako.lookup import TemplateLookup
|
||||
|
||||
import xml.sax.saxutils as saxutils
|
||||
|
||||
TEST_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
def get_template(template_name):
|
||||
"""
|
||||
Return template for a capa inputtype.
|
||||
"""
|
||||
return TemplateLookup(
|
||||
directories=[path(__file__).dirname().dirname() / 'templates']
|
||||
).get_template(template_name)
|
||||
|
||||
|
||||
def capa_render_template(template, context):
|
||||
"""
|
||||
Render template for a capa inputtype.
|
||||
"""
|
||||
return get_template(template).render_unicode(**context)
|
||||
|
||||
|
||||
def tst_render_template(template, context):
|
||||
"""
|
||||
A test version of render to template. Renders to the repr of the context, completely ignoring
|
||||
the template name. To make the output valid xml, quotes the content, and wraps it in a <div>
|
||||
"""
|
||||
return '<div>{0}</div>'.format(saxutils.escape(repr(context)))
|
||||
|
||||
|
||||
def calledback_url(dispatch='score_update'):
|
||||
return dispatch
|
||||
|
||||
xqueue_interface = MagicMock()
|
||||
xqueue_interface.send_to_queue.return_value = (0, 'Success!')
|
||||
|
||||
|
||||
def test_capa_system(render_template=None):
|
||||
"""
|
||||
Construct a mock LoncapaSystem instance.
|
||||
|
||||
"""
|
||||
the_system = Mock(
|
||||
spec=LoncapaSystem,
|
||||
ajax_url='/dummy-ajax-url',
|
||||
anonymous_student_id='student',
|
||||
cache=None,
|
||||
can_execute_unsafe_code=lambda: False,
|
||||
get_python_lib_zip=lambda: None,
|
||||
DEBUG=True,
|
||||
filestore=fs.osfs.OSFS(os.path.join(TEST_DIR, "test_files")),
|
||||
i18n=gettext.NullTranslations(),
|
||||
node_path=os.environ.get("NODE_PATH", "/usr/local/lib/node_modules"),
|
||||
render_template=render_template or tst_render_template,
|
||||
seed=0,
|
||||
STATIC_URL='/dummy-static/',
|
||||
STATUS_CLASS=Status,
|
||||
xqueue={'interface': xqueue_interface, 'construct_callback': calledback_url, 'default_queuename': 'testqueue', 'waittime': 10},
|
||||
)
|
||||
return the_system
|
||||
|
||||
|
||||
def mock_capa_module():
|
||||
"""
|
||||
capa response types needs just two things from the capa_module: location and track_function.
|
||||
"""
|
||||
capa_module = Mock()
|
||||
capa_module.location.to_deprecated_string.return_value = 'i4x://Foo/bar/mock/abc'
|
||||
# The following comes into existence by virtue of being called
|
||||
# capa_module.runtime.track_function
|
||||
return capa_module
|
||||
|
||||
|
||||
def new_loncapa_problem(xml, capa_system=None, seed=723, use_capa_render_template=False):
|
||||
"""Construct a `LoncapaProblem` suitable for unit tests."""
|
||||
render_template = capa_render_template if use_capa_render_template else None
|
||||
return LoncapaProblem(xml, id='1', seed=seed, capa_system=capa_system or test_capa_system(render_template),
|
||||
capa_module=mock_capa_module())
|
||||
|
||||
|
||||
def load_fixture(relpath):
|
||||
"""
|
||||
Return a `unicode` object representing the contents
|
||||
of the fixture file at the given path within a test_files directory
|
||||
in the same directory as the test file.
|
||||
"""
|
||||
abspath = os.path.join(os.path.dirname(__file__), 'test_files', relpath)
|
||||
with open(abspath) as fixture_file:
|
||||
contents = fixture_file.read()
|
||||
return contents.decode('utf8')
|
||||
|
||||
103
common/lib/capa/capa/tests/helpers.py
Normal file
103
common/lib/capa/capa/tests/helpers.py
Normal file
@@ -0,0 +1,103 @@
|
||||
"""Tools for helping with testing capa."""
|
||||
|
||||
import gettext
|
||||
from path import path # pylint: disable=no-name-in-module
|
||||
import os
|
||||
import os.path
|
||||
|
||||
import fs.osfs
|
||||
|
||||
from capa.capa_problem import LoncapaProblem, LoncapaSystem
|
||||
from capa.inputtypes import Status
|
||||
from mock import Mock, MagicMock
|
||||
from mako.lookup import TemplateLookup
|
||||
|
||||
import xml.sax.saxutils as saxutils
|
||||
|
||||
TEST_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
|
||||
|
||||
def get_template(template_name):
|
||||
"""
|
||||
Return template for a capa inputtype.
|
||||
"""
|
||||
return TemplateLookup(
|
||||
directories=[path(__file__).dirname().dirname() / 'templates']
|
||||
).get_template(template_name)
|
||||
|
||||
|
||||
def capa_render_template(template, context):
|
||||
"""
|
||||
Render template for a capa inputtype.
|
||||
"""
|
||||
return get_template(template).render_unicode(**context)
|
||||
|
||||
|
||||
def tst_render_template(template, context):
|
||||
"""
|
||||
A test version of render to template. Renders to the repr of the context, completely ignoring
|
||||
the template name. To make the output valid xml, quotes the content, and wraps it in a <div>
|
||||
"""
|
||||
return '<div>{0}</div>'.format(saxutils.escape(repr(context)))
|
||||
|
||||
|
||||
def calledback_url(dispatch='score_update'):
|
||||
return dispatch
|
||||
|
||||
xqueue_interface = MagicMock()
|
||||
xqueue_interface.send_to_queue.return_value = (0, 'Success!')
|
||||
|
||||
|
||||
def test_capa_system(render_template=None):
|
||||
"""
|
||||
Construct a mock LoncapaSystem instance.
|
||||
|
||||
"""
|
||||
the_system = Mock(
|
||||
spec=LoncapaSystem,
|
||||
ajax_url='/dummy-ajax-url',
|
||||
anonymous_student_id='student',
|
||||
cache=None,
|
||||
can_execute_unsafe_code=lambda: False,
|
||||
get_python_lib_zip=lambda: None,
|
||||
DEBUG=True,
|
||||
filestore=fs.osfs.OSFS(os.path.join(TEST_DIR, "test_files")),
|
||||
i18n=gettext.NullTranslations(),
|
||||
node_path=os.environ.get("NODE_PATH", "/usr/local/lib/node_modules"),
|
||||
render_template=render_template or tst_render_template,
|
||||
seed=0,
|
||||
STATIC_URL='/dummy-static/',
|
||||
STATUS_CLASS=Status,
|
||||
xqueue={'interface': xqueue_interface, 'construct_callback': calledback_url, 'default_queuename': 'testqueue', 'waittime': 10},
|
||||
)
|
||||
return the_system
|
||||
|
||||
|
||||
def mock_capa_module():
|
||||
"""
|
||||
capa response types needs just two things from the capa_module: location and track_function.
|
||||
"""
|
||||
capa_module = Mock()
|
||||
capa_module.location.to_deprecated_string.return_value = 'i4x://Foo/bar/mock/abc'
|
||||
# The following comes into existence by virtue of being called
|
||||
# capa_module.runtime.track_function
|
||||
return capa_module
|
||||
|
||||
|
||||
def new_loncapa_problem(xml, capa_system=None, seed=723, use_capa_render_template=False):
|
||||
"""Construct a `LoncapaProblem` suitable for unit tests."""
|
||||
render_template = capa_render_template if use_capa_render_template else None
|
||||
return LoncapaProblem(xml, id='1', seed=seed, capa_system=capa_system or test_capa_system(render_template),
|
||||
capa_module=mock_capa_module())
|
||||
|
||||
|
||||
def load_fixture(relpath):
|
||||
"""
|
||||
Return a `unicode` object representing the contents
|
||||
of the fixture file at the given path within a test_files directory
|
||||
in the same directory as the test file.
|
||||
"""
|
||||
abspath = os.path.join(os.path.dirname(__file__), 'test_files', relpath)
|
||||
with open(abspath) as fixture_file:
|
||||
contents = fixture_file.read()
|
||||
return contents.decode('utf8')
|
||||
@@ -5,7 +5,7 @@ Tests the logic of the "answer-pool" attribute, e.g.
|
||||
|
||||
import unittest
|
||||
import textwrap
|
||||
from . import test_capa_system, new_loncapa_problem
|
||||
from capa.tests.helpers import test_capa_system, new_loncapa_problem
|
||||
from capa.responsetypes import LoncapaProblemError
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import textwrap
|
||||
from lxml import etree
|
||||
import unittest
|
||||
|
||||
from . import new_loncapa_problem
|
||||
from capa.tests.helpers import new_loncapa_problem
|
||||
|
||||
|
||||
@ddt.ddt
|
||||
|
||||
@@ -2,7 +2,7 @@ from lxml import etree
|
||||
import unittest
|
||||
import xml.sax.saxutils as saxutils
|
||||
|
||||
from . import test_capa_system
|
||||
from capa.tests.helpers import test_capa_system
|
||||
from capa import customrender
|
||||
|
||||
# just a handy shortcut
|
||||
|
||||
@@ -14,7 +14,7 @@ from ddt import ddt, data, unpack
|
||||
# pylint: disable=line-too-long
|
||||
# For out many ddt data cases, prefer a compact form of { .. }
|
||||
|
||||
from . import new_loncapa_problem, load_fixture
|
||||
from capa.tests.helpers import new_loncapa_problem, load_fixture
|
||||
|
||||
|
||||
class HintTest(unittest.TestCase):
|
||||
|
||||
@@ -6,7 +6,7 @@ import textwrap
|
||||
import mock
|
||||
|
||||
from .response_xml_factory import StringResponseXMLFactory, CustomResponseXMLFactory
|
||||
from . import test_capa_system, new_loncapa_problem
|
||||
from capa.tests.helpers import test_capa_system, new_loncapa_problem
|
||||
|
||||
|
||||
class CapaHtmlRenderTest(unittest.TestCase):
|
||||
|
||||
@@ -24,7 +24,7 @@ import unittest
|
||||
import textwrap
|
||||
import xml.sax.saxutils as saxutils
|
||||
|
||||
from . import test_capa_system
|
||||
from capa.tests.helpers import test_capa_system
|
||||
from capa import inputtypes
|
||||
from capa.checker import DemoSystem
|
||||
from mock import ANY, patch
|
||||
|
||||
@@ -17,7 +17,7 @@ import mock
|
||||
from pytz import UTC
|
||||
import requests
|
||||
|
||||
from . import new_loncapa_problem, test_capa_system, load_fixture
|
||||
from capa.tests.helpers import new_loncapa_problem, test_capa_system, load_fixture
|
||||
import calc
|
||||
|
||||
from capa.responsetypes import LoncapaProblemError, \
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import unittest
|
||||
import textwrap
|
||||
|
||||
from . import test_capa_system, new_loncapa_problem
|
||||
from capa.tests.helpers import test_capa_system, new_loncapa_problem
|
||||
from capa.responsetypes import LoncapaProblemError
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ i.e. those with the <multiplechoiceresponse> element
|
||||
|
||||
import unittest
|
||||
import textwrap
|
||||
from . import test_capa_system, new_loncapa_problem, load_fixture
|
||||
from capa.tests.helpers import test_capa_system, new_loncapa_problem, load_fixture
|
||||
|
||||
|
||||
class CapaTargetedFeedbackTest(unittest.TestCase):
|
||||
|
||||
@@ -4,7 +4,7 @@ Tests capa util
|
||||
import unittest
|
||||
from lxml import etree
|
||||
|
||||
from . import test_capa_system
|
||||
from capa.tests.helpers import test_capa_system
|
||||
from capa.util import compare_with_tolerance, sanitize_html, get_inner_html_from_xpath
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user