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
ec06b366
Commit
ec06b366
authored
Apr 01, 2013
by
Calen Pennington
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1787 from MITx/fix/will/random_seed_not_saved
Fix/will/random seed not saved
parents
98a717b6
fb976024
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
104 additions
and
0 deletions
+104
-0
common/lib/xmodule/xmodule/capa_module.py
+10
-0
common/lib/xmodule/xmodule/tests/test_capa_module.py
+94
-0
No files found.
common/lib/xmodule/xmodule/capa_module.py
View file @
ec06b366
...
...
@@ -151,6 +151,16 @@ class CapaModule(CapaFields, XModule):
# TODO (vshnayder): move as much as possible of this work and error
# checking to descriptor load time
self
.
lcp
=
self
.
new_lcp
(
self
.
get_state_for_lcp
())
# At this point, we need to persist the randomization seed
# so that when the problem is re-loaded (to check/view/save)
# it stays the same.
# However, we do not want to write to the database
# every time the module is loaded.
# So we set the seed ONLY when there is not one set already
if
self
.
seed
is
None
:
self
.
seed
=
self
.
lcp
.
seed
except
Exception
as
err
:
msg
=
'cannot create LoncapaProblem {loc}: {err}'
.
format
(
loc
=
self
.
location
.
url
(),
err
=
err
)
...
...
common/lib/xmodule/xmodule/tests/test_capa_module.py
View file @
ec06b366
...
...
@@ -901,3 +901,97 @@ class CapaModuleTest(unittest.TestCase):
# Expect that the module has created a new dummy problem with the error
self
.
assertNotEqual
(
original_problem
,
module
.
lcp
)
def
test_random_seed_no_change
(
self
):
# Run the test for each possible rerandomize value
for
rerandomize
in
[
'never'
,
'per_student'
,
'always'
,
'onreset'
]:
module
=
CapaFactory
.
create
(
rerandomize
=
rerandomize
)
# Get the seed
# By this point, the module should have persisted the seed
seed
=
module
.
seed
self
.
assertTrue
(
seed
is
not
None
)
# If we're not rerandomizing, the seed is always set
# to the same value (1)
if
rerandomize
==
'never'
:
self
.
assertEqual
(
seed
,
1
)
# Check the problem
get_request_dict
=
{
CapaFactory
.
input_key
():
'3.14'
}
module
.
check_problem
(
get_request_dict
)
# Expect that the seed is the same
self
.
assertEqual
(
seed
,
module
.
seed
)
# Save the problem
module
.
save_problem
(
get_request_dict
)
# Expect that the seed is the same
self
.
assertEqual
(
seed
,
module
.
seed
)
def
test_random_seed_with_reset
(
self
):
def
_reset_and_get_seed
(
module
):
'''
Reset the XModule and return the module's seed
'''
# Simulate submitting an attempt
# We need to do this, or reset_problem() will
# fail with a complaint that we haven't submitted
# the problem yet.
module
.
done
=
True
# Reset the problem
module
.
reset_problem
({})
# Return the seed
return
module
.
seed
def
_retry_and_check
(
num_tries
,
test_func
):
'''
Returns True if *test_func* was successful
(returned True) within *num_tries* attempts
*test_func* must be a function
of the form test_func() -> bool
'''
success
=
False
for
i
in
range
(
num_tries
):
if
test_func
()
is
True
:
success
=
True
break
return
success
# Run the test for each possible rerandomize value
for
rerandomize
in
[
'never'
,
'per_student'
,
'always'
,
'onreset'
]:
module
=
CapaFactory
.
create
(
rerandomize
=
rerandomize
)
# Get the seed
# By this point, the module should have persisted the seed
seed
=
module
.
seed
self
.
assertTrue
(
seed
is
not
None
)
# We do NOT want the seed to reset if rerandomize
# is set to 'never' -- it should still be 1
# The seed also stays the same if we're randomizing
# 'per_student': the same student should see the same problem
if
rerandomize
in
[
'never'
,
'per_student'
]:
self
.
assertEqual
(
seed
,
_reset_and_get_seed
(
module
))
# Otherwise, we expect the seed to change
# to another valid seed
else
:
# Since there's a small chance we might get the
# same seed again, give it 5 chances
# to generate a different seed
success
=
_retry_and_check
(
5
,
lambda
:
_reset_and_get_seed
(
module
)
!=
seed
)
self
.
assertTrue
(
module
.
seed
!=
None
)
msg
=
'Could not get a new seed from reset after 5 tries'
self
.
assertTrue
(
success
,
msg
)
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