From 7242fe138b03fed991e2e1f5b9cd4054c608f080 Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Thu, 22 Sep 2016 11:04:52 -0400 Subject: [PATCH 1/3] Declare additional requirements for installing the capa module --- common/lib/capa/setup.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/setup.py b/common/lib/capa/setup.py index b9466ca101..6edde294eb 100644 --- a/common/lib/capa/setup.py +++ b/common/lib/capa/setup.py @@ -4,5 +4,9 @@ setup( name="capa", version="0.1", packages=find_packages(exclude=["tests"]), - install_requires=["setuptools"], + install_requires=[ + "setuptools", + "lxml", + "pytz" + ], ) From 24ae284f34d2243227621f4f37f2e309290bfa9f Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Fri, 23 Sep 2016 13:21:49 -0400 Subject: [PATCH 2/3] Move helper methods from the capa tests module __init__ file --- common/lib/capa/capa/tests/__init__.py | 103 ------------------ common/lib/capa/capa/tests/helpers.py | 103 ++++++++++++++++++ .../lib/capa/capa/tests/test_answer_pool.py | 2 +- .../lib/capa/capa/tests/test_capa_problem.py | 2 +- .../lib/capa/capa/tests/test_customrender.py | 2 +- .../capa/tests/test_hint_functionality.py | 2 +- .../lib/capa/capa/tests/test_html_render.py | 2 +- common/lib/capa/capa/tests/test_inputtypes.py | 2 +- .../lib/capa/capa/tests/test_responsetypes.py | 2 +- common/lib/capa/capa/tests/test_shuffle.py | 2 +- .../capa/capa/tests/test_targeted_feedback.py | 2 +- common/lib/capa/capa/tests/test_util.py | 2 +- 12 files changed, 113 insertions(+), 113 deletions(-) create mode 100644 common/lib/capa/capa/tests/helpers.py diff --git a/common/lib/capa/capa/tests/__init__.py b/common/lib/capa/capa/tests/__init__.py index 7bd12bb65f..e69de29bb2 100644 --- a/common/lib/capa/capa/tests/__init__.py +++ b/common/lib/capa/capa/tests/__init__.py @@ -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
- """ - return '
{0}
'.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') diff --git a/common/lib/capa/capa/tests/helpers.py b/common/lib/capa/capa/tests/helpers.py new file mode 100644 index 0000000000..7bd12bb65f --- /dev/null +++ b/common/lib/capa/capa/tests/helpers.py @@ -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
+ """ + return '
{0}
'.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') diff --git a/common/lib/capa/capa/tests/test_answer_pool.py b/common/lib/capa/capa/tests/test_answer_pool.py index 7ca4923be2..8d1d3b800f 100644 --- a/common/lib/capa/capa/tests/test_answer_pool.py +++ b/common/lib/capa/capa/tests/test_answer_pool.py @@ -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 diff --git a/common/lib/capa/capa/tests/test_capa_problem.py b/common/lib/capa/capa/tests/test_capa_problem.py index edd02cd3c7..8a65b0f1d5 100644 --- a/common/lib/capa/capa/tests/test_capa_problem.py +++ b/common/lib/capa/capa/tests/test_capa_problem.py @@ -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 diff --git a/common/lib/capa/capa/tests/test_customrender.py b/common/lib/capa/capa/tests/test_customrender.py index 9cb1de7d9b..16bba785fc 100644 --- a/common/lib/capa/capa/tests/test_customrender.py +++ b/common/lib/capa/capa/tests/test_customrender.py @@ -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 diff --git a/common/lib/capa/capa/tests/test_hint_functionality.py b/common/lib/capa/capa/tests/test_hint_functionality.py index 76333ad22e..b96f6cadbd 100644 --- a/common/lib/capa/capa/tests/test_hint_functionality.py +++ b/common/lib/capa/capa/tests/test_hint_functionality.py @@ -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): diff --git a/common/lib/capa/capa/tests/test_html_render.py b/common/lib/capa/capa/tests/test_html_render.py index 85c88de8ac..3b9b2db62c 100644 --- a/common/lib/capa/capa/tests/test_html_render.py +++ b/common/lib/capa/capa/tests/test_html_render.py @@ -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): diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 7d0b7ed860..e6dfc88a01 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -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 diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index e3f486ccbf..a73da83d89 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -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, \ diff --git a/common/lib/capa/capa/tests/test_shuffle.py b/common/lib/capa/capa/tests/test_shuffle.py index 958c0c2398..ff5c99c10e 100644 --- a/common/lib/capa/capa/tests/test_shuffle.py +++ b/common/lib/capa/capa/tests/test_shuffle.py @@ -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 diff --git a/common/lib/capa/capa/tests/test_targeted_feedback.py b/common/lib/capa/capa/tests/test_targeted_feedback.py index d8592c642c..41f70d33b2 100644 --- a/common/lib/capa/capa/tests/test_targeted_feedback.py +++ b/common/lib/capa/capa/tests/test_targeted_feedback.py @@ -5,7 +5,7 @@ i.e. those with the 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): diff --git a/common/lib/capa/capa/tests/test_util.py b/common/lib/capa/capa/tests/test_util.py index 1f470fb7d2..eee0cfcf3b 100644 --- a/common/lib/capa/capa/tests/test_util.py +++ b/common/lib/capa/capa/tests/test_util.py @@ -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 From 3de366dcf94d6ab0998156ec61664054f1477116 Mon Sep 17 00:00:00 2001 From: Jesse Zoldak Date: Fri, 23 Sep 2016 17:09:13 -0400 Subject: [PATCH 3/3] Fix pylint errors --- common/lib/capa/capa/tests/helpers.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/common/lib/capa/capa/tests/helpers.py b/common/lib/capa/capa/tests/helpers.py index 7bd12bb65f..7ae7a3020d 100644 --- a/common/lib/capa/capa/tests/helpers.py +++ b/common/lib/capa/capa/tests/helpers.py @@ -33,7 +33,7 @@ def capa_render_template(template, context): return get_template(template).render_unicode(**context) -def tst_render_template(template, context): +def tst_render_template(template, context): # pylint: disable=unused-argument """ 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
@@ -42,9 +42,10 @@ def tst_render_template(template, context): def calledback_url(dispatch='score_update'): + """A callback url method to use in tests.""" return dispatch -xqueue_interface = MagicMock() +xqueue_interface = MagicMock() # pylint: disable=invalid-name xqueue_interface.send_to_queue.return_value = (0, 'Success!') @@ -68,7 +69,12 @@ def test_capa_system(render_template=None): seed=0, STATIC_URL='/dummy-static/', STATUS_CLASS=Status, - xqueue={'interface': xqueue_interface, 'construct_callback': calledback_url, 'default_queuename': 'testqueue', 'waittime': 10}, + xqueue={ + 'interface': xqueue_interface, + 'construct_callback': calledback_url, + 'default_queuename': 'testqueue', + 'waittime': 10 + }, ) return the_system