Created
April 4, 2016 23:55
-
-
Save cnk/8fcd98e0f87a803466a81d41486931d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
manage.py test --keepdb course_builder.multiple_choice_question.tests2 | |
Using existing test database for alias 'default'... | |
<class 'dict'> | |
200 | |
{'correct_answer': {'answer_text': '1', 'correct_answer': '/course_materials/course0/multiple_choice_answers/90/'}, | |
'student_answer': {'is_answer_correct': True, 'answer': '/course_materials/course0/multiple_choice_answers/90/'}} | |
. | |
---------------------------------------------------------------------- | |
Ran 1 test in 0.417s | |
OK | |
Preserving test database for alias 'default'... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import status | |
from rest_framework.test import APITestCase, APIClient | |
from django.core.urlresolvers import reverse | |
from course_materials.tests.factories import CourseFactory, MaterialVersionFactory, MaterialVersionBlockFactory, \ | |
MultipleChoiceQuestionFactory, MultipleChoiceAnswerFactory, UserWithRoleFactory, TextElementFactory, HintFactory | |
class MCTests(APITestCase): | |
''' | |
This class tests permissions for the API endpoints for course_builder/multiple_choice_question. | |
It also serves as the tests for the most general happy path through our API. | |
''' | |
@classmethod | |
def setUpClass(cls): | |
super(MCTests, cls).setUpClass() | |
cls.course = CourseFactory.create() | |
cls.question = MultipleChoiceQuestionFactory.create(course=cls.course) | |
cls.answers = [] | |
for i in range(4): | |
cls.answers.append(MultipleChoiceAnswerFactory.create(question=cls.question, answer_text=i)) | |
cls.question.correct_answer = cls.answers[1] | |
cls.question.save() | |
cls.mv = MaterialVersionFactory(course=cls.course) | |
cls.mvb = MaterialVersionBlockFactory.create(course=cls.course, material_version=cls.mv, | |
block_id=cls.question.id, block_type=cls.question.model_name) | |
# People we need to test the various roles: | |
cls.tester = UserWithRoleFactory.create(username='tester', is_superuser=False, role='tester', course=cls.course\ | |
) | |
cls.team = UserWithRoleFactory.create(username='team', is_superuser=False, role='course_team', course=cls.cours\ | |
e) # noqa | |
cls.lead = UserWithRoleFactory.create(username='leader', is_superuser=False, role='course_lead', course=cls.cou\ | |
rse) # noqa | |
cls.reqClient = APIClient() | |
@classmethod | |
def tearDownClass(cls): | |
cls.course.delete() | |
cls.lead.delete() | |
cls.tester.delete() | |
cls.team.delete() | |
super(MCTests, cls).tearDownClass() | |
def test_lead_can_access_submit_multiple_choice_answer(self): | |
self.reqClient.login(username=self.lead.username, password='password') | |
url = reverse('course_builder:multiple_choice_question:submit_multiple_choice_answer', | |
kwargs={'course_key': self.course.course_key, 'pk': self.question.id}) | |
response = self.reqClient.post(url, {'answer': self.question.correct_answer.get_absolute_url()}) | |
print(response.status_code) | |
print(response.data) | |
self.assertEqual(response.status_code, status.HTTP_200_OK) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@api_view(['POST']) | |
@permission_classes((IsAuthenticated, HasCourseBuilderRoleForCourse)) | |
def submit_multiple_choice_answer(request, course_key, pk): | |
print(type(request.data)) # this is giving me <class 'dict'> in my tests | |
question = get_object_or_404(MultipleChoiceQuestion, pk=pk) | |
answer = request.data.get('answer', None) # url for the MC answer | |
is_answer_correct = (answer == question.correct_answer.get_absolute_url()) | |
output = {'student_answer': {'is_answer_correct': is_answer_correct, 'answer': answer}} | |
if is_answer_correct: | |
output['correct_answer'] = {'correct_answer': question.correct_answer.get_absolute_url(), | |
'answer_text': question.correct_answer.answer_text} | |
return Response(output, status=status.HTTP_200_OK) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment