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
15b42b68
Commit
15b42b68
authored
Jul 17, 2012
by
kimth
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Unit tests for CodeResponse update_score
parent
631517cc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
146 additions
and
0 deletions
+146
-0
common/lib/xmodule/tests/__init__.py
+45
-0
common/lib/xmodule/tests/test_files/coderesponse.xml
+101
-0
No files found.
common/lib/xmodule/tests/__init__.py
View file @
15b42b68
...
...
@@ -13,6 +13,7 @@ import numpy
import
xmodule
import
capa.calc
as
calc
import
capa.capa_problem
as
lcp
from
capa.correctmap
import
CorrectMap
from
xmodule
import
graders
,
x_module
from
xmodule.graders
import
Score
,
aggregate_scores
from
xmodule.progress
import
Progress
...
...
@@ -271,6 +272,50 @@ class StringResponseWithHintTest(unittest.TestCase):
self
.
assertEquals
(
cmap
.
get_correctness
(
'1_2_1'
),
'incorrect'
)
self
.
assertTrue
(
'St. Paul'
in
cmap
.
get_hint
(
'1_2_1'
))
class
CodeResponseTest
(
unittest
.
TestCase
):
'''
Test CodeResponse
'''
def
test_update_score
(
self
):
problem_file
=
os
.
path
.
dirname
(
__file__
)
+
"/test_files/coderesponse.xml"
test_lcp
=
lcp
.
LoncapaProblem
(
open
(
problem_file
)
.
read
(),
'1'
,
system
=
i4xs
)
# CodeResponse requires internal CorrectMap state. Build it now
old_cmap
=
CorrectMap
()
answer_ids
=
sorted
(
test_lcp
.
get_question_answers
()
.
keys
())
numAnswers
=
len
(
answer_ids
)
for
i
in
range
(
numAnswers
):
old_cmap
.
update
(
CorrectMap
(
answer_id
=
answer_ids
[
i
],
queuekey
=
1000
+
i
))
# Message format inherited from ExternalResponse
correct_score_msg
=
"<edxgrade><awarddetail>EXACT_ANS</awarddetail><message>MESSAGE</message></edxgrade>"
incorrect_score_msg
=
"<edxgrade><awarddetail>WRONG_FORMAT</awarddetail><message>MESSAGE</message></edxgrade>"
xserver_msgs
=
{
'correct'
:
correct_score_msg
,
'incorrect'
:
incorrect_score_msg
,
}
# Incorrect queuekey, state should not be updated
for
correctness
in
[
'correct'
,
'incorrect'
]:
test_lcp
.
correct_map
=
CorrectMap
()
test_lcp
.
correct_map
.
update
(
old_cmap
)
# Deep copy
test_lcp
.
update_score
(
xserver_msgs
[
correctness
],
queuekey
=
0
)
self
.
assertEquals
(
test_lcp
.
correct_map
.
get_dict
(),
old_cmap
.
get_dict
())
# Deep comparison
# Correct queuekey, state should be updated
for
correctness
in
[
'correct'
,
'incorrect'
]:
for
i
in
range
(
numAnswers
):
# Target specific answer_id's
test_lcp
.
correct_map
=
CorrectMap
()
test_lcp
.
correct_map
.
update
(
old_cmap
)
new_cmap
=
CorrectMap
()
new_cmap
.
update
(
old_cmap
)
new_cmap
.
set
(
answer_id
=
answer_ids
[
i
],
correctness
=
correctness
,
msg
=
'MESSAGE'
,
queuekey
=
None
)
test_lcp
.
update_score
(
xserver_msgs
[
correctness
],
queuekey
=
1000
+
i
)
self
.
assertEquals
(
test_lcp
.
correct_map
.
get_dict
(),
new_cmap
.
get_dict
())
#-----------------------------------------------------------------------------
# Grading tests
...
...
common/lib/xmodule/tests/test_files/coderesponse.xml
0 → 100644
View file @
15b42b68
<problem>
<text>
<h2>
Code response
</h2>
<p>
</p>
<text>
Write a program to compute the square of a number
<coderesponse
tests=
"repeat:2,generate"
>
<textbox
rows=
"10"
cols=
"70"
mode=
"python"
/>
<answer>
<![CDATA[
initial_display = """
def square(n):
"""
answer = """
def square(n):
return n**2
"""
preamble = """
import sys, time
"""
test_program = """
import random
import operator
def testSquare(n = None):
if n is None:
n = random.randint(2, 20)
print 'Test is: square(%d)'%n
return str(square(n))
def main():
f = os.fdopen(3,'w')
test = int(sys.argv[1])
rndlist = map(int,os.getenv('rndlist').split(','))
random.seed(rndlist[0])
if test == 1: f.write(testSquare(0))
elif test == 2: f.write(testSquare(1))
else: f.write(testSquare())
f.close()
main()
sys.exit(0)
"""
]]>
</answer>
</coderesponse>
</text>
<text>
Write a program to compute the cube of a number
<coderesponse
tests=
"repeat:2,generate"
>
<textbox
rows=
"10"
cols=
"70"
mode=
"python"
/>
<answer>
<![CDATA[
initial_display = """
def cube(n):
"""
answer = """
def cube(n):
return n**3
"""
preamble = """
import sys, time
"""
test_program = """
import random
import operator
def testCube(n = None):
if n is None:
n = random.randint(2, 20)
print 'Test is: cube(%d)'%n
return str(cube(n))
def main():
f = os.fdopen(3,'w')
test = int(sys.argv[1])
rndlist = map(int,os.getenv('rndlist').split(','))
random.seed(rndlist[0])
if test == 1: f.write(testCube(0))
elif test == 2: f.write(testCube(1))
else: f.write(testCube())
f.close()
main()
sys.exit(0)
"""
]]>
</answer>
</coderesponse>
</text>
</text>
</problem>
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