Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
24ae284f
Commit
24ae284f
authored
Sep 23, 2016
by
Jesse Zoldak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move helper methods from the capa tests module __init__ file
parent
7242fe13
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
113 additions
and
113 deletions
+113
-113
common/lib/capa/capa/tests/__init__.py
+0
-103
common/lib/capa/capa/tests/helpers.py
+103
-0
common/lib/capa/capa/tests/test_answer_pool.py
+1
-1
common/lib/capa/capa/tests/test_capa_problem.py
+1
-1
common/lib/capa/capa/tests/test_customrender.py
+1
-1
common/lib/capa/capa/tests/test_hint_functionality.py
+1
-1
common/lib/capa/capa/tests/test_html_render.py
+1
-1
common/lib/capa/capa/tests/test_inputtypes.py
+1
-1
common/lib/capa/capa/tests/test_responsetypes.py
+1
-1
common/lib/capa/capa/tests/test_shuffle.py
+1
-1
common/lib/capa/capa/tests/test_targeted_feedback.py
+1
-1
common/lib/capa/capa/tests/test_util.py
+1
-1
No files found.
common/lib/capa/capa/tests/__init__.py
View file @
24ae284f
"""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'
)
common/lib/capa/capa/tests/helpers.py
0 → 100644
View file @
24ae284f
"""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'
)
common/lib/capa/capa/tests/test_answer_pool.py
View file @
24ae284f
...
@@ -5,7 +5,7 @@ Tests the logic of the "answer-pool" attribute, e.g.
...
@@ -5,7 +5,7 @@ Tests the logic of the "answer-pool" attribute, e.g.
import
unittest
import
unittest
import
textwrap
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
from
capa.responsetypes
import
LoncapaProblemError
...
...
common/lib/capa/capa/tests/test_capa_problem.py
View file @
24ae284f
...
@@ -6,7 +6,7 @@ import textwrap
...
@@ -6,7 +6,7 @@ import textwrap
from
lxml
import
etree
from
lxml
import
etree
import
unittest
import
unittest
from
.
import
new_loncapa_problem
from
capa.tests.helpers
import
new_loncapa_problem
@ddt.ddt
@ddt.ddt
...
...
common/lib/capa/capa/tests/test_customrender.py
View file @
24ae284f
...
@@ -2,7 +2,7 @@ from lxml import etree
...
@@ -2,7 +2,7 @@ from lxml import etree
import
unittest
import
unittest
import
xml.sax.saxutils
as
saxutils
import
xml.sax.saxutils
as
saxutils
from
.
import
test_capa_system
from
capa.tests.helpers
import
test_capa_system
from
capa
import
customrender
from
capa
import
customrender
# just a handy shortcut
# just a handy shortcut
...
...
common/lib/capa/capa/tests/test_hint_functionality.py
View file @
24ae284f
...
@@ -14,7 +14,7 @@ from ddt import ddt, data, unpack
...
@@ -14,7 +14,7 @@ from ddt import ddt, data, unpack
# pylint: disable=line-too-long
# pylint: disable=line-too-long
# For out many ddt data cases, prefer a compact form of { .. }
# 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
):
class
HintTest
(
unittest
.
TestCase
):
...
...
common/lib/capa/capa/tests/test_html_render.py
View file @
24ae284f
...
@@ -6,7 +6,7 @@ import textwrap
...
@@ -6,7 +6,7 @@ import textwrap
import
mock
import
mock
from
.response_xml_factory
import
StringResponseXMLFactory
,
CustomResponseXMLFactory
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
):
class
CapaHtmlRenderTest
(
unittest
.
TestCase
):
...
...
common/lib/capa/capa/tests/test_inputtypes.py
View file @
24ae284f
...
@@ -24,7 +24,7 @@ import unittest
...
@@ -24,7 +24,7 @@ import unittest
import
textwrap
import
textwrap
import
xml.sax.saxutils
as
saxutils
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
import
inputtypes
from
capa.checker
import
DemoSystem
from
capa.checker
import
DemoSystem
from
mock
import
ANY
,
patch
from
mock
import
ANY
,
patch
...
...
common/lib/capa/capa/tests/test_responsetypes.py
View file @
24ae284f
...
@@ -17,7 +17,7 @@ import mock
...
@@ -17,7 +17,7 @@ import mock
from
pytz
import
UTC
from
pytz
import
UTC
import
requests
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
import
calc
from
capa.responsetypes
import
LoncapaProblemError
,
\
from
capa.responsetypes
import
LoncapaProblemError
,
\
...
...
common/lib/capa/capa/tests/test_shuffle.py
View file @
24ae284f
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
import
unittest
import
unittest
import
textwrap
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
from
capa.responsetypes
import
LoncapaProblemError
...
...
common/lib/capa/capa/tests/test_targeted_feedback.py
View file @
24ae284f
...
@@ -5,7 +5,7 @@ i.e. those with the <multiplechoiceresponse> element
...
@@ -5,7 +5,7 @@ i.e. those with the <multiplechoiceresponse> element
import
unittest
import
unittest
import
textwrap
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
):
class
CapaTargetedFeedbackTest
(
unittest
.
TestCase
):
...
...
common/lib/capa/capa/tests/test_util.py
View file @
24ae284f
...
@@ -4,7 +4,7 @@ Tests capa util
...
@@ -4,7 +4,7 @@ Tests capa util
import
unittest
import
unittest
from
lxml
import
etree
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
capa.util
import
compare_with_tolerance
,
sanitize_html
,
get_inner_html_from_xpath
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment