Commit 969830ce by Will Daly

Merge pull request #62 from edx/will/test-cleanup

Refactor test setup for XBlock rendering and handlers.
parents e44b501c a33ad617
"""
Base class for handler-level testing of the XBlock.
"""
import os.path
import json
from functools import wraps
from django.test import TestCase
from workbench.runtime import WorkbenchRuntime
import webob
def scenario(scenario_path, user_id=None):
"""
Method decorator to load a scenario for a test case.
Must be called on an `XBlockHandlerTestCase` subclass, or
else it will have no effect.
Args:
scenario_path (str): Path to the scenario XML file.
Kwargs:
user_id (str or None): User ID to log in as, or None.
Returns:
The decorated method
Example:
@scenario('data/test_scenario.xml')
def test_submit(self, xblock):
response = self.request(xblock, 'submit', 'Test submission')
self.assertTrue('Success' in response)
"""
def _decorator(func):
@wraps(func)
def _wrapped(*args, **kwargs):
# Retrieve the object (self)
# if this is a function, not a method, then do nothing.
xblock = None
if args:
self = args[0]
if isinstance(self, XBlockHandlerTestCase):
# Configure the runtime with our user id
self.set_user(user_id)
# Load the scenario
xblock = self.load_scenario(scenario_path)
# Pass the XBlock as the first argument to the decorated method (after `self`)
args = list(args)
args.insert(1, xblock)
return func(*args, **kwargs)
return _wrapped
return _decorator
class XBlockHandlerTestCase(TestCase):
"""
Load the XBlock in the workbench runtime to test its handler.
"""
def setUp(self):
"""
Create the runtime.
"""
self.runtime = WorkbenchRuntime()
def set_user(self, user_id):
"""
Provide a user ID to the runtime.
Args:
user_id (str): a user ID.
Returns:
None
"""
self.runtime.user_id = user_id
def load_scenario(self, xml_path):
"""
Load an XML definition of an XBlock and return the XBlock instance.
Args:
xml (string): Path to an XML definition of the XBlock, relative
to the test module.
Returns:
XBlock
"""
base_dir = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(base_dir, xml_path)) as xml_file:
block_id = self.runtime.parse_xml_string(
xml_file.read(), self.runtime.id_generator
)
return self.runtime.get_block(block_id)
def request(self, xblock, handler_name, content, response_format=None):
"""
Make a request to an XBlock handler.
Args:
xblock (XBlock): The XBlock instance that should handle the request.
handler_name (str): The name of the handler.
content (unicode): Content of the request.
Kwargs:
response_format (None or str): Expected format of the response string.
If `None`, return the raw response content; if 'json', parse the
response as JSON and return the result.
Raises:
NotImplementedError: Response format not supported.
Returns:
Content of the response (mixed).
"""
# Create a fake request
request = webob.Request(dict())
request.body = content
# Send the request to the XBlock handler
response = self.runtime.handle(xblock, handler_name, request)
# Parse the response (if a format is specified)
if response_format is None:
return response.body
elif response_format == 'json':
return json.loads(response.body)
else:
raise NotImplementedError("Response format '{format}' not supported".format(response_format))
<openassessment start="2014-12-19T23:00:00" due="2014-12-21T23:00:00">
<prompt>
Given the state of the world today, what do you think should be done to
combat poverty? Please answer in a short essay of 200-300 words.
</prompt>
<rubric>
Read for conciseness, clarity of thought, and form.
<criterion name="concise">
How concise is it?
<option val="0">Neal Stephenson (late)</option>
<option val="1">HP Lovecraft</option>
<option val="3">Robert Heinlein</option>
<option val="4">Neal Stephenson (early)</option>
<option val="5">Earnest Hemingway</option>
</criterion>
<criterion name="clearheaded">
How clear is the thinking?
<option val="0">Yogi Berra</option>
<option val="1">Hunter S. Thompson</option>
<option val="2">Robert Heinlein</option>
<option val="3">Isaac Asimov</option>
<option val="10">Spock</option>
</criterion>
<criterion name="form">
Lastly, how is it's form? Punctuation, grammar, and spelling all count.
<option val="0">lolcats</option>
<option val="1">Facebook</option>
<option val="2">Reddit</option>
<option val="3">metafilter</option>
<option val="4">Usenet, 1996</option>
<option val="5">The Elements of Style</option>
</criterion>
</rubric>
<assessments>
<peer-assessment name="peer-assessment"
start="2014-12-20T19:00"
due="2014-12-21T22:22"
must_grade="5"
must_be_graded_by="3" />
<self-assessment name="self-assessment"/>
</assessments>
</openassessment>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment