Commit 8323cc7c by Vik Paruchuri

Refactor tests

parent 7b8b168f
from . import test_system
import unittest
from xmodule.modulestore import Location
from xmodule.modulestore.xml import ImportSystem, XMLModuleStore
from xmodule.tests.test_export import DATA_DIR
from fs.memoryfs import MemoryFS
from mock import patch, Mock
class DummySystem(ImportSystem):
@patch('xmodule.modulestore.xml.OSFS', lambda dir: MemoryFS())
def __init__(self, load_error_modules, org, course):
xmlstore = XMLModuleStore("data_dir", course_dirs=[], load_error_modules=load_error_modules)
course_id = "/".join([org, course, 'test_run'])
course_dir = "test_dir"
policy = {}
error_tracker = Mock()
parent_tracker = Mock()
super(DummySystem, self).__init__(
xmlstore,
course_id,
course_dir,
policy,
error_tracker,
parent_tracker,
load_error_modules=load_error_modules,
)
def render_template(self, template, context):
raise Exception("Shouldn't be called")
class DummySystemUser(object):
test_system = test_system()
@staticmethod
def get_import_system(org, course, load_error_modules=True):
'''Get a dummy system'''
return DummySystem(load_error_modules, org, course)
def get_course(self, name):
"""Get a test course by directory name. If there's more than one, error."""
modulestore = XMLModuleStore(DATA_DIR, course_dirs=[name])
courses = modulestore.get_courses()
self.modulestore = modulestore
return courses[0]
def get_module_from_location(self, location, course):
course = self.get_course(course)
if not isinstance(location, Location):
location = Location(location)
descriptor = self.modulestore.get_instance(course.id, location, depth=None)
return descriptor.xmodule(self.test_system)
\ No newline at end of file
...@@ -2,8 +2,7 @@ import json ...@@ -2,8 +2,7 @@ import json
from mock import Mock, MagicMock, ANY from mock import Mock, MagicMock, ANY
import unittest import unittest
from dummy_system import DummySystemUser from test_util_open_ended import MockQueryDict, DummyModulestore
from test_util_open_ended import MockQueryDict
from xmodule.open_ended_grading_classes.openendedchild import OpenEndedChild from xmodule.open_ended_grading_classes.openendedchild import OpenEndedChild
from xmodule.open_ended_grading_classes.open_ended_module import OpenEndedModule from xmodule.open_ended_grading_classes.open_ended_module import OpenEndedModule
...@@ -19,7 +18,7 @@ import logging ...@@ -19,7 +18,7 @@ import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
from . import test_system from .import test_system
ORG = 'edX' ORG = 'edX'
COURSE = 'open_ended' # name of directory with course data COURSE = 'open_ended' # name of directory with course data
...@@ -473,34 +472,36 @@ class CombinedOpenEndedModuleTest(unittest.TestCase): ...@@ -473,34 +472,36 @@ class CombinedOpenEndedModuleTest(unittest.TestCase):
self.assertEqual(score_dict['score'], 15.0) self.assertEqual(score_dict['score'], 15.0)
self.assertEqual(score_dict['total'], 15.0) self.assertEqual(score_dict['total'], 15.0)
class OpenEndedModuleXmlTest(unittest.TestCase, DummySystemUser):
class OpenEndedModuleXmlTest(unittest.TestCase, DummyModulestore):
problem_location = Location(["i4x", "edX", "open_ended", "combinedopenended", "SampleQuestion"]) problem_location = Location(["i4x", "edX", "open_ended", "combinedopenended", "SampleQuestion"])
answer = "blah blah" answer = "blah blah"
assessment = [0,1] assessment = [0, 1]
hint = "blah" hint = "blah"
def setUp(self): def setUp(self):
self.test_system = test_system() self.test_system = test_system()
self.test_system.xqueue['interface'] = Mock( self.test_system.xqueue['interface'] = Mock(
send_to_queue = Mock(side_effect=[1,"queued"]) send_to_queue=Mock(side_effect=[1, "queued"])
) )
def test_open_ended_load_and_save(self): def test_open_ended_load_and_save(self):
module = self.get_module_from_location(self.problem_location, COURSE) module = self.get_module_from_location(self.problem_location, COURSE)
module.handle_ajax("save_answer", {"student_answer" : self.answer}) module.handle_ajax("save_answer", {"student_answer": self.answer})
task_one_json = json.loads(module.task_states[0]) task_one_json = json.loads(module.task_states[0])
self.assertEqual(task_one_json['child_history'][0]['answer'], self.answer) self.assertEqual(task_one_json['child_history'][0]['answer'], self.answer)
def test_open_ended_flow_reset(self): def test_open_ended_flow_reset(self):
assessment = [0,1] assessment = [0, 1]
module = self.get_module_from_location(self.problem_location, COURSE) module = self.get_module_from_location(self.problem_location, COURSE)
#Simulate a student saving an answer #Simulate a student saving an answer
module.handle_ajax("save_answer", {"student_answer" : self.answer}) module.handle_ajax("save_answer", {"student_answer": self.answer})
status = module.handle_ajax("get_status", {}) status = module.handle_ajax("get_status", {})
#Mock a student submitting an assessment #Mock a student submitting an assessment
assessment_dict = MockQueryDict() assessment_dict = MockQueryDict()
assessment_dict.update({'assessment' : sum(assessment), 'score_list[]' : assessment}) assessment_dict.update({'assessment': sum(assessment), 'score_list[]': assessment})
module.handle_ajax("save_assessment", assessment_dict) module.handle_ajax("save_assessment", assessment_dict)
task_one_json = json.loads(module.task_states[0]) task_one_json = json.loads(module.task_states[0])
self.assertEqual(json.loads(task_one_json['child_history'][0]['post_assessment']), assessment) self.assertEqual(json.loads(task_one_json['child_history'][0]['post_assessment']), assessment)
...@@ -514,16 +515,16 @@ class OpenEndedModuleXmlTest(unittest.TestCase, DummySystemUser): ...@@ -514,16 +515,16 @@ class OpenEndedModuleXmlTest(unittest.TestCase, DummySystemUser):
module.handle_ajax("reset", {}) module.handle_ajax("reset", {})
def test_open_ended_flow_correct(self): def test_open_ended_flow_correct(self):
assessment = [1,1] assessment = [1, 1]
module = self.get_module_from_location(self.problem_location, COURSE) module = self.get_module_from_location(self.problem_location, COURSE)
#Simulate a student saving an answer #Simulate a student saving an answer
module.handle_ajax("save_answer", {"student_answer" : self.answer}) module.handle_ajax("save_answer", {"student_answer": self.answer})
status = module.handle_ajax("get_status", {}) status = module.handle_ajax("get_status", {})
#Mock a student submitting an assessment #Mock a student submitting an assessment
assessment_dict = MockQueryDict() assessment_dict = MockQueryDict()
assessment_dict.update({'assessment' : sum(assessment), 'score_list[]' : assessment}) assessment_dict.update({'assessment': sum(assessment), 'score_list[]': assessment})
module.handle_ajax("save_assessment", assessment_dict) module.handle_ajax("save_assessment", assessment_dict)
task_one_json = json.loads(module.task_states[0]) task_one_json = json.loads(module.task_states[0])
self.assertEqual(json.loads(task_one_json['child_history'][0]['post_assessment']), assessment) self.assertEqual(json.loads(task_one_json['child_history'][0]['post_assessment']), assessment)
...@@ -546,16 +547,17 @@ class OpenEndedModuleXmlTest(unittest.TestCase, DummySystemUser): ...@@ -546,16 +547,17 @@ class OpenEndedModuleXmlTest(unittest.TestCase, DummySystemUser):
#Make a fake reply from the queue #Make a fake reply from the queue
queue_reply = { queue_reply = {
'queuekey' : "", 'queuekey': "",
'xqueue_body' : json.dumps({ 'xqueue_body': json.dumps({
'score' : 0, 'score': 0,
'feedback' : json.dumps({"spelling": "Spelling: Ok.", "grammar": "Grammar: Ok.", "markup-text": " all of us can think of a book that we hope none of our children or any other children have taken off the shelf . but if i have the right to remove that book from the shelf that work i abhor then you also have exactly the same right and so does everyone else . and then we <bg>have no books left</bg> on the shelf for any of us . <bs>katherine</bs> <bs>paterson</bs> , author write a persuasive essay to a newspaper reflecting your vies on censorship <bg>in libraries . do</bg> you believe that certain materials , such as books , music , movies , magazines , <bg>etc . , should be</bg> removed from the shelves if they are found <bg>offensive ? support your</bg> position with convincing arguments from your own experience , observations <bg>, and or reading .</bg> "}), 'feedback': json.dumps({"spelling": "Spelling: Ok.", "grammar": "Grammar: Ok.",
'grader_type' : "ML", "markup-text": " all of us can think of a book that we hope none of our children or any other children have taken off the shelf . but if i have the right to remove that book from the shelf that work i abhor then you also have exactly the same right and so does everyone else . and then we <bg>have no books left</bg> on the shelf for any of us . <bs>katherine</bs> <bs>paterson</bs> , author write a persuasive essay to a newspaper reflecting your vies on censorship <bg>in libraries . do</bg> you believe that certain materials , such as books , music , movies , magazines , <bg>etc . , should be</bg> removed from the shelves if they are found <bg>offensive ? support your</bg> position with convincing arguments from your own experience , observations <bg>, and or reading .</bg> "}),
'success' : True, 'grader_type': "ML",
'grader_id' : 1, 'success': True,
'submission_id' : 1, 'grader_id': 1,
'rubric_xml' : "<rubric><category><description>Writing Applications</description><score>0</score><option points='0'> The essay loses focus, has little information or supporting details, and the organization makes it difficult to follow.</option><option points='1'> The essay presents a mostly unified theme, includes sufficient information to convey the theme, and is generally organized well.</option></category><category><description> Language Conventions </description><score>0</score><option points='0'> The essay demonstrates a reasonable command of proper spelling and grammar. </option><option points='1'> The essay demonstrates superior command of proper spelling and grammar.</option></category></rubric>", 'submission_id': 1,
'rubric_scores_complete' : True, 'rubric_xml': "<rubric><category><description>Writing Applications</description><score>0</score><option points='0'> The essay loses focus, has little information or supporting details, and the organization makes it difficult to follow.</option><option points='1'> The essay presents a mostly unified theme, includes sufficient information to convey the theme, and is generally organized well.</option></category><category><description> Language Conventions </description><score>0</score><option points='0'> The essay demonstrates a reasonable command of proper spelling and grammar. </option><option points='1'> The essay demonstrates superior command of proper spelling and grammar.</option></category></rubric>",
'rubric_scores_complete': True,
}) })
} }
......
import unittest import unittest
from xmodule.modulestore import Location from xmodule.modulestore import Location
import json from .import test_system
from lxml import etree from test_util_open_ended import MockQueryDict, DummyModulestore
from mock import Mock
from . import test_system
from dummy_system import DummySystem, DummySystemUser
from test_util_open_ended import MockQueryDict
from xmodule.peer_grading_module import PeerGradingModule, PeerGradingDescriptor from xmodule.peer_grading_module import PeerGradingModule, PeerGradingDescriptor
from xmodule.open_ended_grading_classes.grading_service_module import GradingServiceError from xmodule.open_ended_grading_classes.grading_service_module import GradingServiceError
import logging
log = logging.getLogger(__name__)
ORG = "edX" ORG = "edX"
COURSE="open_ended" COURSE = "open_ended"
class PeerGradingModuleTest(unittest.TestCase, DummySystemUser): class PeerGradingModuleTest(unittest.TestCase, DummyModulestore):
problem_location = Location(["i4x", "edX", "open_ended", "peergrading", problem_location = Location(["i4x", "edX", "open_ended", "peergrading",
"PeerGradingSample"]) "PeerGradingSample"])
calibrated_dict = {'location' : "blah"} calibrated_dict = {'location': "blah"}
save_dict = MockQueryDict() save_dict = MockQueryDict()
save_dict.update({ save_dict.update({
'location' : "blah", 'location': "blah",
'submission_id' : 1, 'submission_id': 1,
'submission_key' : "", 'submission_key': "",
'score': 1, 'score': 1,
'feedback' : "", 'feedback': "",
'rubric_scores[]' : [0,1], 'rubric_scores[]': [0, 1],
'submission_flagged': False, 'submission_flagged': False,
}) })
...@@ -54,17 +54,16 @@ class PeerGradingModuleTest(unittest.TestCase, DummySystemUser): ...@@ -54,17 +54,16 @@ class PeerGradingModuleTest(unittest.TestCase, DummySystemUser):
max_score = self.peer_grading.max_score() max_score = self.peer_grading.max_score()
def get_next_submission(self): def get_next_submission(self):
success, next_submission = self.peer_grading.get_next_submission({'location' : 'blah'}) success, next_submission = self.peer_grading.get_next_submission({'location': 'blah'})
def test_save_grade(self): def test_save_grade(self):
self.peer_grading.save_grade(self.save_dict) self.peer_grading.save_grade(self.save_dict)
def test_is_student_calibrated(self): def test_is_student_calibrated(self):
calibrated_dict = {'location' : "blah"} calibrated_dict = {'location': "blah"}
self.peer_grading.is_student_calibrated(self.calibrated_dict) self.peer_grading.is_student_calibrated(self.calibrated_dict)
def test_show_calibration_essay(self): def test_show_calibration_essay(self):
self.peer_grading.show_calibration_essay(self.calibrated_dict) self.peer_grading.show_calibration_essay(self.calibrated_dict)
def test_save_calibration_essay(self): def test_save_calibration_essay(self):
......
from .import test_system
from xmodule.modulestore import Location
from xmodule.modulestore.xml import ImportSystem, XMLModuleStore
from xmodule.tests.test_export import DATA_DIR
OPEN_ENDED_GRADING_INTERFACE = { OPEN_ENDED_GRADING_INTERFACE = {
'url': 'blah/', 'url': 'blah/',
'username': 'incorrect', 'username': 'incorrect',
...@@ -17,6 +22,7 @@ class MockQueryDict(dict): ...@@ -17,6 +22,7 @@ class MockQueryDict(dict):
""" """
Mock a query set so that it can be used with default authorization Mock a query set so that it can be used with default authorization
""" """
def getlist(self, key, default=None): def getlist(self, key, default=None):
try: try:
return super(MockQueryDict, self).__getitem__(key) return super(MockQueryDict, self).__getitem__(key)
...@@ -24,3 +30,22 @@ class MockQueryDict(dict): ...@@ -24,3 +30,22 @@ class MockQueryDict(dict):
if default is None: if default is None:
return [] return []
return default return default
class DummyModulestore(object):
test_system = test_system()
def get_course(self, name):
"""Get a test course by directory name. If there's more than one, error."""
modulestore = XMLModuleStore(DATA_DIR, course_dirs=[name])
courses = modulestore.get_courses()
self.modulestore = modulestore
return courses[0]
def get_module_from_location(self, location, course):
course = self.get_course(course)
if not isinstance(location, Location):
location = Location(location)
descriptor = self.modulestore.get_instance(course.id, location, depth=None)
return descriptor.xmodule(self.test_system)
\ No newline at end of file
...@@ -2,5 +2,6 @@ ...@@ -2,5 +2,6 @@
<chapter url_name="Overview"> <chapter url_name="Overview">
<combinedopenended url_name="SampleQuestion"/> <combinedopenended url_name="SampleQuestion"/>
<peergrading url_name="PeerGradingSample"/> <peergrading url_name="PeerGradingSample"/>
<peergrading url_name="PeerGradingScored"/>
</chapter> </chapter>
</course> </course>
<peergrading is_graded="True" max_grade="1" use_for_single_location="True" link_to_location="i4x://edX/open_ended/combinedopenended/SampleQuestion"/>
\ No newline at end of file
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