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
4c269c10
Commit
4c269c10
authored
Jun 25, 2013
by
Peter Baratta
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #203 from edx/pbaratta/fix/capa-unicode
Fix unicode for errors on capa problems
parents
08b994b8
3a8f591f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
130 additions
and
11 deletions
+130
-11
CHANGELOG.rst
+2
-0
common/lib/calc/calc.py
+1
-1
common/lib/xmodule/xmodule/capa_module.py
+0
-0
common/lib/xmodule/xmodule/tests/test_capa_module.py
+127
-10
No files found.
CHANGELOG.rst
View file @
4c269c10
...
@@ -51,6 +51,8 @@ setting now run entirely outside the Python sandbox.
...
@@ -51,6 +51,8 @@ setting now run entirely outside the Python sandbox.
Blades: Added tests for Video Alpha player.
Blades: Added tests for Video Alpha player.
Common: Have the capa module handle unicode better (especially errors)
Blades: Video Alpha bug fix for speed changing to 1.0 in Firefox.
Blades: Video Alpha bug fix for speed changing to 1.0 in Firefox.
Blades: Additional event tracking added to Video Alpha: fullscreen switch, show/hide
Blades: Additional event tracking added to Video Alpha: fullscreen switch, show/hide
...
...
common/lib/calc/calc.py
View file @
4c269c10
...
@@ -93,7 +93,7 @@ def check_variables(string, variables):
...
@@ -93,7 +93,7 @@ def check_variables(string, variables):
Pyparsing uses a left-to-right parser, which makes a more
Pyparsing uses a left-to-right parser, which makes a more
elegant approach pretty hopeless.
elegant approach pretty hopeless.
"""
"""
general_whitespace
=
re
.
compile
(
'[^
\\
w]+'
)
general_whitespace
=
re
.
compile
(
'[^
\\
w]+'
)
# TODO consider non-ascii
# List of all alnums in string
# List of all alnums in string
possible_variables
=
re
.
split
(
general_whitespace
,
string
)
possible_variables
=
re
.
split
(
general_whitespace
,
string
)
bad_variables
=
[]
bad_variables
=
[]
...
...
common/lib/xmodule/xmodule/capa_module.py
View file @
4c269c10
This diff is collapsed.
Click to expand it.
common/lib/xmodule/xmodule/tests/test_capa_module.py
View file @
4c269c10
"""Tests of the Capa XModule"""
# -*- coding: utf-8 -*-
"""
Tests of the Capa XModule
"""
#pylint: disable=C0111
#pylint: disable=C0111
#pylint: disable=R0904
#pylint: disable=R0904
#pylint: disable=C0103
#pylint: disable=C0103
...
@@ -8,11 +11,12 @@ import datetime
...
@@ -8,11 +11,12 @@ import datetime
from
mock
import
Mock
,
patch
from
mock
import
Mock
,
patch
import
unittest
import
unittest
import
random
import
random
import
json
import
xmodule
import
xmodule
from
capa.responsetypes
import
StudentInputError
,
\
from
capa.responsetypes
import
(
StudentInputError
,
LoncapaProblemError
,
LoncapaProblemError
,
ResponseError
ResponseError
)
from
xmodule.capa_module
import
CapaModule
from
xmodule.capa_module
import
CapaModule
,
ComplexEncoder
from
xmodule.modulestore
import
Location
from
xmodule.modulestore
import
Location
from
django.http
import
QueryDict
from
django.http
import
QueryDict
...
@@ -47,12 +51,16 @@ class CapaFactory(object):
...
@@ -47,12 +51,16 @@ class CapaFactory(object):
@staticmethod
@staticmethod
def
input_key
():
def
input_key
():
""" Return the input key to use when passing GET parameters """
"""
Return the input key to use when passing GET parameters
"""
return
(
"input_"
+
CapaFactory
.
answer_key
())
return
(
"input_"
+
CapaFactory
.
answer_key
())
@staticmethod
@staticmethod
def
answer_key
():
def
answer_key
():
""" Return the key stored in the capa problem answer dict """
"""
Return the key stored in the capa problem answer dict
"""
return
(
"-"
.
join
([
'i4x'
,
'edX'
,
'capa_test'
,
'problem'
,
return
(
"-"
.
join
([
'i4x'
,
'edX'
,
'capa_test'
,
'problem'
,
'SampleProblem
%
d'
%
CapaFactory
.
num
])
+
'SampleProblem
%
d'
%
CapaFactory
.
num
])
+
"_2_1"
)
"_2_1"
)
...
@@ -361,7 +369,9 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -361,7 +369,9 @@ class CapaModuleTest(unittest.TestCase):
result
=
CapaModule
.
make_dict_of_responses
(
invalid_get_dict
)
result
=
CapaModule
.
make_dict_of_responses
(
invalid_get_dict
)
def
_querydict_from_dict
(
self
,
param_dict
):
def
_querydict_from_dict
(
self
,
param_dict
):
""" Create a Django QueryDict from a Python dictionary """
"""
Create a Django QueryDict from a Python dictionary
"""
# QueryDict objects are immutable by default, so we make
# QueryDict objects are immutable by default, so we make
# a copy that we can update.
# a copy that we can update.
...
@@ -496,9 +506,10 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -496,9 +506,10 @@ class CapaModuleTest(unittest.TestCase):
def
test_check_problem_error
(
self
):
def
test_check_problem_error
(
self
):
# Try each exception that capa_module should handle
# Try each exception that capa_module should handle
for
exception_class
in
[
StudentInputError
,
exception_classes
=
[
StudentInputError
,
LoncapaProblemError
,
LoncapaProblemError
,
ResponseError
]:
ResponseError
]
for
exception_class
in
exception_classes
:
# Create the module
# Create the module
module
=
CapaFactory
.
create
(
attempts
=
1
)
module
=
CapaFactory
.
create
(
attempts
=
1
)
...
@@ -520,6 +531,60 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -520,6 +531,60 @@ class CapaModuleTest(unittest.TestCase):
# Expect that the number of attempts is NOT incremented
# Expect that the number of attempts is NOT incremented
self
.
assertEqual
(
module
.
attempts
,
1
)
self
.
assertEqual
(
module
.
attempts
,
1
)
def
test_check_problem_other_errors
(
self
):
"""
Test that errors other than the expected kinds give an appropriate message.
See also `test_check_problem_error` for the "expected kinds" or errors.
"""
# Create the module
module
=
CapaFactory
.
create
(
attempts
=
1
)
# Ensure that the user is NOT staff
module
.
system
.
user_is_staff
=
False
# Ensure that DEBUG is on
module
.
system
.
DEBUG
=
True
# Simulate answering a problem that raises the exception
with
patch
(
'capa.capa_problem.LoncapaProblem.grade_answers'
)
as
mock_grade
:
error_msg
=
u"Superterrible error happened: ☠"
mock_grade
.
side_effect
=
Exception
(
error_msg
)
get_request_dict
=
{
CapaFactory
.
input_key
():
'3.14'
}
result
=
module
.
check_problem
(
get_request_dict
)
# Expect an AJAX alert message in 'success'
self
.
assertTrue
(
error_msg
in
result
[
'success'
])
def
test_check_problem_error_nonascii
(
self
):
# Try each exception that capa_module should handle
exception_classes
=
[
StudentInputError
,
LoncapaProblemError
,
ResponseError
]
for
exception_class
in
exception_classes
:
# Create the module
module
=
CapaFactory
.
create
(
attempts
=
1
)
# Ensure that the user is NOT staff
module
.
system
.
user_is_staff
=
False
# Simulate answering a problem that raises the exception
with
patch
(
'capa.capa_problem.LoncapaProblem.grade_answers'
)
as
mock_grade
:
mock_grade
.
side_effect
=
exception_class
(
u"ȧƈƈḗƞŧḗḓ ŧḗẋŧ ƒǿř ŧḗşŧīƞɠ"
)
get_request_dict
=
{
CapaFactory
.
input_key
():
'3.14'
}
result
=
module
.
check_problem
(
get_request_dict
)
# Expect an AJAX alert message in 'success'
expected_msg
=
u'Error: ȧƈƈḗƞŧḗḓ ŧḗẋŧ ƒǿř ŧḗşŧīƞɠ'
self
.
assertEqual
(
expected_msg
,
result
[
'success'
])
# Expect that the number of attempts is NOT incremented
self
.
assertEqual
(
module
.
attempts
,
1
)
def
test_check_problem_error_with_staff_user
(
self
):
def
test_check_problem_error_with_staff_user
(
self
):
# Try each exception that capa module should handle
# Try each exception that capa module should handle
...
@@ -1021,6 +1086,33 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -1021,6 +1086,33 @@ class CapaModuleTest(unittest.TestCase):
# Expect that the module has created a new dummy problem with the error
# Expect that the module has created a new dummy problem with the error
self
.
assertNotEqual
(
original_problem
,
module
.
lcp
)
self
.
assertNotEqual
(
original_problem
,
module
.
lcp
)
def
test_get_problem_html_error_w_debug
(
self
):
"""
Test the html response when an error occurs with DEBUG on
"""
module
=
CapaFactory
.
create
()
# Simulate throwing an exception when the capa problem
# is asked to render itself as HTML
error_msg
=
u"Superterrible error happened: ☠"
module
.
lcp
.
get_html
=
Mock
(
side_effect
=
Exception
(
error_msg
))
# Stub out the get_test_system rendering function
module
.
system
.
render_template
=
Mock
(
return_value
=
"<div>Test Template HTML</div>"
)
# Make sure DEBUG is on
module
.
system
.
DEBUG
=
True
# Try to render the module with DEBUG turned on
html
=
module
.
get_problem_html
()
self
.
assertTrue
(
html
is
not
None
)
# Check the rendering context
render_args
,
_
=
module
.
system
.
render_template
.
call_args
context
=
render_args
[
1
]
self
.
assertTrue
(
error_msg
in
context
[
'problem'
][
'html'
])
def
test_random_seed_no_change
(
self
):
def
test_random_seed_no_change
(
self
):
# Run the test for each possible rerandomize value
# Run the test for each possible rerandomize value
...
@@ -1126,3 +1218,28 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -1126,3 +1218,28 @@ class CapaModuleTest(unittest.TestCase):
for
i
in
range
(
200
):
for
i
in
range
(
200
):
module
=
CapaFactory
.
create
(
rerandomize
=
rerandomize
)
module
=
CapaFactory
.
create
(
rerandomize
=
rerandomize
)
assert
0
<=
module
.
seed
<
1000
assert
0
<=
module
.
seed
<
1000
@patch
(
'xmodule.capa_module.log'
)
@patch
(
'xmodule.capa_module.Progress'
)
def
test_get_progress_error
(
self
,
mock_progress
,
mock_log
):
"""
Check that an exception given in `Progress` produces a `log.exception` call.
"""
error_types
=
[
TypeError
,
ValueError
]
for
error_type
in
error_types
:
mock_progress
.
side_effect
=
error_type
module
=
CapaFactory
.
create
()
self
.
assertIsNone
(
module
.
get_progress
())
mock_log
.
exception
.
assert_called_once_with
(
'Got bad progress'
)
mock_log
.
reset_mock
()
class
ComplexEncoderTest
(
unittest
.
TestCase
):
def
test_default
(
self
):
"""
Check that complex numbers can be encoded into JSON.
"""
complex_num
=
1
-
1
j
expected_str
=
'1-1*j'
json_str
=
json
.
dumps
(
complex_num
,
cls
=
ComplexEncoder
)
self
.
assertEqual
(
expected_str
,
json_str
[
1
:
-
1
])
# ignore quotes
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