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
08221639
Commit
08221639
authored
Mar 04, 2013
by
Will Daly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added unit test for conversion of GET parameters for capa module.
Added error checking for possible error cases
parent
999ed17e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
8 deletions
+77
-8
common/lib/xmodule/xmodule/capa_module.py
+27
-8
common/lib/xmodule/xmodule/tests/test_capa_module.py
+50
-0
No files found.
common/lib/xmodule/xmodule/capa_module.py
View file @
08221639
...
@@ -534,15 +534,34 @@ class CapaModule(XModule):
...
@@ -534,15 +534,34 @@ class CapaModule(XModule):
# e.g. input_resistor_1 ==> resistor_1
# e.g. input_resistor_1 ==> resistor_1
_
,
_
,
name
=
key
.
partition
(
'_'
)
_
,
_
,
name
=
key
.
partition
(
'_'
)
#
This allows for answers which require more than one value for
#
If key has no underscores, then partition
#
the same form input (e.g. checkbox inputs). The convention is that
#
will return (key, '', '')
#
if the name ends with '[]' (which looks like an array), then the
#
We detect this and raise an error
# answer will be an array.
if
name
is
''
:
if
not
name
.
endswith
(
'[]'
):
raise
ValueError
(
"
%
s must contain at least one underscore"
%
str
(
key
))
answers
[
name
]
=
get
[
key
]
else
:
else
:
name
=
name
[:
-
2
]
# This allows for answers which require more than one value for
answers
[
name
]
=
get
.
getlist
(
key
)
# the same form input (e.g. checkbox inputs). The convention is that
# if the name ends with '[]' (which looks like an array), then the
# answer will be an array.
is_list_key
=
name
.
endswith
(
'[]'
)
name
=
name
[:
-
2
]
if
is_list_key
else
name
if
is_list_key
:
if
type
(
get
[
key
])
is
list
:
val
=
get
[
key
]
else
:
val
=
[
get
[
key
]]
else
:
val
=
get
[
key
]
# If the name already exists, then we don't want
# to override it. Raise an error instead
if
name
in
answers
:
raise
ValueError
(
"Key
%
s already exists in answers dict"
%
str
(
name
))
else
:
answers
[
name
]
=
val
return
answers
return
answers
...
...
common/lib/xmodule/xmodule/tests/test_capa_module.py
View file @
08221639
...
@@ -282,3 +282,53 @@ class CapaModuleTest(unittest.TestCase):
...
@@ -282,3 +282,53 @@ class CapaModuleTest(unittest.TestCase):
due
=
self
.
yesterday_str
,
due
=
self
.
yesterday_str
,
graceperiod
=
self
.
two_day_delta_str
)
graceperiod
=
self
.
two_day_delta_str
)
self
.
assertTrue
(
still_in_grace
.
answer_available
())
self
.
assertTrue
(
still_in_grace
.
answer_available
())
def
test_parse_get_params
(
self
):
# Valid GET param dict
valid_get_dict
=
{
'input_1'
:
'test'
,
'input_1_2'
:
'test'
,
'input_1_2_3'
:
'test'
,
'input_[]_3'
:
'test'
,
'input_4'
:
None
,
'input_5'
:
[],
'input_6'
:
5
}
result
=
CapaModule
.
make_dict_of_responses
(
valid_get_dict
)
# Expect that we get a dict with "input" stripped from key names
# and that we get the same values back
for
key
in
result
.
keys
():
original_key
=
"input_"
+
key
self
.
assertTrue
(
original_key
in
valid_get_dict
,
"Output dict should have key
%
s"
%
original_key
)
self
.
assertEqual
(
valid_get_dict
[
original_key
],
result
[
key
])
# Valid GET param dict with list keys
valid_get_dict
=
{
'input_2[]'
:
[
'test1'
,
'test2'
]}
result
=
CapaModule
.
make_dict_of_responses
(
valid_get_dict
)
self
.
assertTrue
(
'2'
in
result
)
self
.
assertEqual
(
valid_get_dict
[
'input_2[]'
],
result
[
'2'
])
# If we use [] at the end of a key name, we should always
# get a list, even if there's just one value
valid_get_dict
=
{
'input_1[]'
:
'test'
}
result
=
CapaModule
.
make_dict_of_responses
(
valid_get_dict
)
self
.
assertEqual
(
result
[
'1'
],
[
'test'
])
# If we have no underscores in the name, then the key is invalid
invalid_get_dict
=
{
'input'
:
'test'
}
with
self
.
assertRaises
(
ValueError
):
result
=
CapaModule
.
make_dict_of_responses
(
invalid_get_dict
)
# Two equivalent names (one list, one non-list)
# One of the values would overwrite the other, so detect this
# and raise an exception
invalid_get_dict
=
{
'input_1[]'
:
'test 1'
,
'input_1'
:
'test 2'
}
with
self
.
assertRaises
(
ValueError
):
result
=
CapaModule
.
make_dict_of_responses
(
invalid_get_dict
)
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