Пример #1
0
    def test_save_response_multiple_choices_single_select(self):
        """
        save_response() should fail when multiple selected choices
        are given for a CheckboxQuestion that is marked as
        single-select.  This means it should return false and no
        response object should be saved in the database.
        """

        data = {
            'quiz_id':
            self.quiz.user_id,
            'name':
            'Benjamin',
            'emoji':
            '😀',
            'background_color':
            '333333',
            'questions': [
                {
                    'question_id': self.q1.id,
                    'answer': [self.c11.id, self.c12.id]
                },
                {
                    'question_id': self.q2.id,
                    'answer': [self.c21.id, self.c23.id]
                },
            ]
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #2
0
    def test_save_response_checkbox_with_int_answer(self):
        """
        save_response() should fail when the answer to a
        CheckboxQuestion is given as an integer, and not a list (just
        an int is the answer for a SliderQuestion). This means it
        should return false and no response object should be saved in
        the database.
        """

        data = {
            'quiz_id':
            self.quiz.user_id,
            'name':
            'Benjamin',
            'emoji':
            '😀',
            'background_color':
            '333333',
            'questions': [
                {
                    'question_id': self.q1.id,
                    'answer': 2
                },
                {
                    'question_id': self.q2.id,
                    'answer': [self.c21.id, self.c23.id]
                },
                {
                    'question_id': self.q3.id,
                    'answer': [self.c33.id, self.c34.id]
                },
                {
                    'question_id': self.q4.id,
                    'answer': 34
                },
            ]
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #3
0
    def test_save_response_checkbox_bad_choice_id(self):
        """
        save_response() should fail when one of the choice ids is
        invalid.  in a question. This means it should return false and
        no response object should be saved in the database.
        """

        data = {
            'quiz_id':
            self.quiz.user_id,
            'name':
            'Benjamin',
            'emoji':
            '😀',
            'background_color':
            '333333',
            'questions': [
                {
                    'question_id': self.q1.id,
                    'answer': [self.c11.id]
                },
                {
                    'question_id': self.q2.id,
                    'answer': [self.c21.id, -24]
                },
                {
                    'question_id': self.q3.id,
                    'answer': [self.c33.id, self.c34.id]
                },
                {
                    'question_id': self.q4.id,
                    'answer': 34
                },
            ]
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #4
0
    def test_save_response_bad_bg_color(self):
        """
        save_response() should fail when there is an improper
        background color. This means it should return false and no
        response object should be saved in the database.
        """

        data = {
            'quiz_id': 'none',
            'name': 'Benjamin',
            'emoji': '😀',
            'background_color': '1000000',
            'questions': []
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #5
0
    def test_save_response_empty_emoji(self):
        """
        save_response() should fail when there is no emoji field. This
        means it should return false and no response object should be
        saved in the database.
        """

        data = {
            'quiz_id': 'none',
            'name': 'Benjamin',
            'emoji': '',
            'background_color': '333333',
            'questions': []
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #6
0
    def test_save_response_slider_no_question_id_field(self):
        """
        save_response() should fail when there is no question id field
        in a question. This means it should return false and no
        response object should be saved in the database.
        """

        data = {
            'quiz_id':
            self.quiz.user_id,
            'name':
            'Benjamin',
            'emoji':
            '😀',
            'background_color':
            '333333',
            'questions': [
                {
                    'question_id': self.q1.id,
                    'answer': [self.c11.id]
                },
                {
                    'question_id': self.q2.id,
                    'answer': [self.c23.id]
                },
                {
                    'answer': 40
                },
            ]
        }

        result = save_response(data)

        self.assertFalse(result)

        self.assertEqual(Response.objects.count(), 0)
        self.assertEqual(QuestionResponse.objects.count(), 0)
Пример #7
0
def handle_response(request):
    data = json.loads(request.body)

    if save_response(data):
        return JsonResponse({'status': 'success'})
    return JsonResponse({'status': 'error'})
Пример #8
0
    def test_save_response(self):
        """
        save_response() takes a user's response to a Quiz, processes it,
        and loads the data into database models. This tests that function.
        """

        data = {
            'quiz_id':
            self.quiz.user_id,
            'name':
            'Benjamin',
            'emoji':
            '😀',
            'background_color':
            '333333',
            'questions': [
                {
                    'question_id': self.q1.id,
                    'answer': [self.c11.id]
                },
                {
                    'question_id': self.q2.id,
                    'answer': [self.c21.id, self.c23.id]
                },
                {
                    'question_id': self.q3.id,
                    'answer': [self.c33.id, self.c34.id]
                },
                {
                    'question_id': self.q4.id,
                    'answer': 34
                },
            ]
        }

        result = save_response(data)

        self.assertTrue(result)
        self.assertEqual(Response.objects.count(), 1)
        r = Response.objects.all()[0]
        self.assertEqual(r.name, data.get('name'))

        self.assertCountEqual(QuestionResponse.objects.all(), r.answers.all())

        # Make sure choice objects are associated with the right
        # QuestionResponses
        self.assertIn(
            Choice.objects.filter(id=self.c11.id)[0],
            r.answers.all()[0].choices.all())

        self.assertIn(
            Choice.objects.filter(id=self.c21.id)[0],
            r.answers.all()[1].choices.all())
        self.assertIn(
            Choice.objects.filter(id=self.c23.id)[0],
            r.answers.all()[1].choices.all())

        self.assertIn(
            Choice.objects.filter(id=self.c33.id)[0],
            r.answers.all()[2].choices.all())
        self.assertIn(
            Choice.objects.filter(id=self.c34.id)[0],
            r.answers.all()[2].choices.all())

        self.assertEqual(SliderResponse.objects.all()[0].question,
                         SliderQuestion.objects.filter(id=self.q4.id)[0])
        self.assertEqual(SliderResponse.objects.all()[0].answer, 34)