Commit 38752a77 by solashirai Committed by solashirai

modified to resemble test_recommender

parent fdae162b
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
Test scenarios for the crowdsource hinter xblock. Test scenarios for the crowdsource hinter xblock.
""" """
import json import json
import unittest
from nose.plugins.attrib import attr
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
...@@ -9,11 +12,13 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase ...@@ -9,11 +12,13 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory
from courseware.tests.factories import GlobalStaffFactory
from courseware.tests.helpers import LoginEnrollmentTestCase
from lms.djangoapps.courseware.tests.helpers import LoginEnrollmentTestCase
from lms.djangoapps.courseware.tests.factories import GlobalStaffFactory
from lms.djangoapps.lms_xblock.runtime import quote_slashes from lms.djangoapps.lms_xblock.runtime import quote_slashes
from django.conf import settings
from django.core.urlresolvers import reverse
class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
""" """
...@@ -23,36 +28,47 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -23,36 +28,47 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
{'email': 'view@test.com', 'password': 'foo'}, {'email': 'view@test.com', 'password': 'foo'},
{'email': 'view2@test.com', 'password': 'foo'} {'email': 'view2@test.com', 'password': 'foo'}
] ]
XBLOCK_NAMES = ['crowdsource_hinter'] XBLOCK_NAMES = ['crowdsourcehinter']
def setUp(self): @classmethod
self.course = CourseFactory.create( def setUpClass(cls):
display_name='Crowdsource_Hinter_Test_Course' # Nose runs setUpClass methods even if a class decorator says to skip
) # the class: https://github.com/nose-devs/nose/issues/946
self.chapter = ItemFactory.create( # So, skip the test class here if we are not in the LMS.
parent=self.course, display_name='Overview' if settings.ROOT_URLCONF != 'lms.urls':
) raise unittest.SkipTest('Test only valid in lms')
self.section = ItemFactory.create(
parent=self.chapter, display_name='Welcome' super(TestCrowdsourceHinter, cls).setUpClass()
) cls.course = CourseFactory.create(
self.unit = ItemFactory.create( display_name='CrowdsourceHinter_Test_Course'
parent=self.section, display_name='New Unit'
)
self.xblock = ItemFactory.create(
parent=self.unit,
category='crowdsourcehinter',
display_name='crowdsource_hinter'
) )
with cls.store.bulk_operations(cls.course.id, emit_signals=False):
cls.chapter = ItemFactory.create(
parent=cls.course, display_name='Overview'
)
cls.section = ItemFactory.create(
parent=cls.chapter, display_name='Welcome'
)
cls.unit = ItemFactory.create(
parent=cls.section, display_name='New Unit'
)
cls.xblock = ItemFactory.create(
parent=cls.unit,
category='crowdsourcehinter',
display_name='crowdsourcehinter'
)
self.course_url = reverse( cls.course_url = reverse(
'courseware_section', 'courseware_section',
kwargs={ kwargs={
'course_id': self.course.id.to_deprecated_string(), 'course_id': cls.course.id.to_deprecated_string(),
'chapter': 'Overview', 'chapter': 'Overview',
'section': 'Welcome', 'section': 'Welcome',
} }
) )
def setUp(self):
super(TestCrowdsourceHinter, self).setUp()
for idx, student in enumerate(self.STUDENTS): for idx, student in enumerate(self.STUDENTS):
username = "u{}".format(idx) username = "u{}".format(idx)
self.create_account(username, student['email'], student['password']) self.create_account(username, student['email'], student['password'])
...@@ -66,10 +82,9 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -66,10 +82,9 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
""" """
if xblock_name is None: if xblock_name is None:
xblock_name = TestCrowdsourceHinter.XBLOCK_NAMES[0] xblock_name = TestCrowdsourceHinter.XBLOCK_NAMES[0]
usage_key = self.course.id.make_usage_key('crowdsourcehinter', xblock_name)
return reverse('xblock_handler', kwargs={ return reverse('xblock_handler', kwargs={
'course_id': self.course.id.to_deprecated_string(), 'course_id': self.course.id.to_deprecated_string(),
'usage_id': quote_slashes(usage_key.to_deprecated_string()), 'usage_id': quote_slashes(self.course.id.make_usage_key('crowdsourcehinter', xblock_name).to_deprecated_string()),
'handler': handler, 'handler': handler,
'suffix': '' 'suffix': ''
}) })
...@@ -90,6 +105,17 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -90,6 +105,17 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
self.login(email, password) self.login(email, password)
self.enroll(self.course, verify=True) self.enroll(self.course, verify=True)
def initialize_database_by_id(self, handler, resource_id, times, xblock_name=None):
"""
Call a ajax event (vote, delete, endorse) on a resource by its id
several times
"""
if xblock_name is None:
xblock_name = TestCrowdsourceHinter.XBLOCK_NAMES[0]
url = self.get_handler_url(handler, xblock_name)
for _ in range(times):
self.client.post(url, json.dumps({'id': resource_id}), '')
def call_event(self, handler, resource, xblock_name=None): def call_event(self, handler, resource, xblock_name=None):
""" """
Call a ajax event (add, edit, flag, etc.) by specifying the resource Call a ajax event (add, edit, flag, etc.) by specifying the resource
...@@ -98,8 +124,7 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -98,8 +124,7 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
if xblock_name is None: if xblock_name is None:
xblock_name = TestCrowdsourceHinter.XBLOCK_NAMES[0] xblock_name = TestCrowdsourceHinter.XBLOCK_NAMES[0]
url = self.get_handler_url(handler, xblock_name) url = self.get_handler_url(handler, xblock_name)
resp = self.client.post(url, json.dumps(resource), '') return self.client.post(url, json.dumps(resource), '')
return json.loads(resp.content)
def check_event_response_by_element(self, handler, resource, resp_key, resp_val, xblock_name=None): def check_event_response_by_element(self, handler, resource, resp_key, resp_val, xblock_name=None):
""" """
...@@ -112,7 +137,7 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase): ...@@ -112,7 +137,7 @@ class TestCrowdsourceHinter(SharedModuleStoreTestCase, LoginEnrollmentTestCase):
self.assertEqual(resp[resp_key], resp_val) self.assertEqual(resp[resp_key], resp_val)
self.assert_request_status_code(200, self.course_url) self.assert_request_status_code(200, self.course_url)
@attr('shard_1')
class TestHinterFunctions(TestCrowdsourceHinter): class TestHinterFunctions(TestCrowdsourceHinter):
""" """
Check that the essential functions of the hinter work as expected. Check that the essential functions of the hinter work as expected.
...@@ -123,10 +148,10 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -123,10 +148,10 @@ class TestHinterFunctions(TestCrowdsourceHinter):
""" """
Check that a generic statement is returned when no default/specific hints exist Check that a generic statement is returned when no default/specific hints exist
""" """
result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}) result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}, 'crowdsourcehinter')
expected = {'BestHint': 'Sorry, there are no hints for this answer.', 'StudentAnswer': 'incorrect answer 1', expected = {'BestHint': 'Sorry, there are no hints for this answer.', 'StudentAnswer': 'incorrect answer 1',
'HintCategory': False} 'HintCategory': False}
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_add_new_hint(self): def test_add_new_hint(self):
""" """
...@@ -138,7 +163,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -138,7 +163,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
result = self.call_event('add_new_hint', data) result = self.call_event('add_new_hint', data)
expected = {'success':True, expected = {'success':True,
'result': 'Hint added'} 'result': 'Hint added'}
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_get_hint(self): def test_get_hint(self):
""" """
...@@ -151,7 +176,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -151,7 +176,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}) result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'})
expected = {'BestHint': 'new hint for answer 1', 'StudentAnswer': 'incorrect answer 1', expected = {'BestHint': 'new hint for answer 1', 'StudentAnswer': 'incorrect answer 1',
'HintCategory': 'ErrorResponse'} 'HintCategory': 'ErrorResponse'}
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_rate_hint_upvote(self): def test_rate_hint_upvote(self):
""" """
...@@ -168,7 +193,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -168,7 +193,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
} }
expected = {'success': True} expected = {'success': True}
result = self.call_event('rate_hint', data) result = self.call_event('rate_hint', data)
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_rate_hint_downvote(self): def test_rate_hint_downvote(self):
""" """
...@@ -185,7 +210,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -185,7 +210,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
} }
expected = {'success': True} expected = {'success': True}
result = self.call_event('rate_hint', data) result = self.call_event('rate_hint', data)
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_report_hint(self): def test_report_hint(self):
""" """
...@@ -202,7 +227,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -202,7 +227,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
} }
expected = {'rating': 'reported', 'hint': 'new hint for answer 1'} expected = {'rating': 'reported', 'hint': 'new hint for answer 1'}
result = self.call_event('rate_hint', data) result = self.call_event('rate_hint', data)
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_dont_show_reported_hint(self): def test_dont_show_reported_hint(self):
""" """
...@@ -221,7 +246,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -221,7 +246,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}) result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'})
expected = {'BestHint': 'Sorry, there are no hints for this answer.', 'StudentAnswer': 'incorrect answer 1', expected = {'BestHint': 'Sorry, there are no hints for this answer.', 'StudentAnswer': 'incorrect answer 1',
'HintCategory': False} 'HintCategory': False}
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_get_used_hint_answer_data(self): def test_get_used_hint_answer_data(self):
""" """
...@@ -236,7 +261,7 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -236,7 +261,7 @@ class TestHinterFunctions(TestCrowdsourceHinter):
self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}) self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'})
result = self.call_event('get_used_hint_answer_data', "") result = self.call_event('get_used_hint_answer_data', "")
expected = {'new hint for answer 1': 'incorrect answer 1'} expected = {'new hint for answer 1': 'incorrect answer 1'}
self.assertEqual(result, expected) self.assertEqual(json.loads(result.content), expected)
def test_show_best_hint(self): def test_show_best_hint(self):
""" """
...@@ -264,4 +289,4 @@ class TestHinterFunctions(TestCrowdsourceHinter): ...@@ -264,4 +289,4 @@ class TestHinterFunctions(TestCrowdsourceHinter):
result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'}) result = self.call_event('get_hint', {'submittedanswer': 'ans=incorrect+answer+1'})
expected = {'BestHint': 'new hint for answer 1', 'StudentAnswer': 'incorrect answer 1', expected = {'BestHint': 'new hint for answer 1', 'StudentAnswer': 'incorrect answer 1',
'HintCategory': 'ErrorResponse'} 'HintCategory': 'ErrorResponse'}
self.assertEqual(expected, result) self.assertEqual(json.loads(result.content), expected)
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