def test_process_analytics_answer_dist_missing_correct(self):
        """
        Test with data that has correct answer missing.
        """
        data = [
            {
                'course_id': 'A/B/C',
                'module_id': 'i4x://A/B/problem/f3ed0ba7f89445ee9a83541e1fc8a2f2',
                'part_id': 'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1',
                'correct': False,
                'first_response_count': 7,
                'last_response_count': 9,
                'value_id': 'choice_0',
                'answer_value_text': 'Option 1',
                'answer_value_numeric': 'null',
                'variant': None,
                'created': '2014-10-15T101351',
            },
        ]

        question_types_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': 'radio',
        }

        num_options_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': 4,
        }

        processed_data = {
            'count_by_part': {
                'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': {
                    'totalFirstIncorrectCount': 7,
                    'totalLastIncorrectCount': 9,
                    'totalFirstAttemptCount': 7,
                    'totalLastAttemptCount': 9,
                    'totalFirstCorrectCount': 0,
                    'totalLastCorrectCount': 0,
                },
            },
            'data_by_part': {
                'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': [
                    {
                        'first_count': 7,
                        'last_count': 9,
                        'value_id': 'choice_0',
                        'correct': False,
                    },
                ]
            },
            'message_by_part': {
            },
            'last_update_date': 'Oct 15, 2014 at 10:13 UTC'
        }

        return_json = process_analytics_answer_dist(data, question_types_by_part, num_options_by_part)
        self.assertEquals(json.loads(return_json.content), processed_data)
    def test_process_analytics_answer_dist_checkbox(self):
        """
        Test with data having more rows than is possible for checkbox type.
        """
        data = [
            {
                'course_id': 'A/B/C',
                'module_id': 'i4x://A/B/problem/f3ed0ba7f89445ee9a83541e1fc8a2f2',
                'part_id': 'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1',
                'correct': False,
                'count': 7,
                'value_id': 'choice_0',
                'answer_value_text': 'Option 1',
                'answer_value_numeric': 'null',
                'variant': '123',
                'created': '2014-10-15T101351',
            },
            {
                'course_id': 'A/B/C',
                'module_id': 'i4x://A/B/problem/f3ed0ba7f89445ee9a83541e1fc8a2f2',
                'part_id': 'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1',
                'correct': True,
                'count': 23,
                'value_id': 'choice_1',
                'answer_value_text': 'Option 2',
                'answer_value_numeric': 'null',
                'variant': None,
                'created': '2014-10-15T101351',
            },
        ]

        question_types_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': 'checkbox',
        }

        num_options_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': 0,
        }

        processed_data = {
            'count_by_part': {
            },
            'data_by_part': {
            },
            'message_by_part': {
                'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': (
                    'The analytics cannot be displayed for this question as the number of rows returned did '
                    'not match the question definition.'
                )
            },
            'last_update_date': 'Oct 15, 2014 at 10:13 UTC'
        }

        return_json = process_analytics_answer_dist(data, question_types_by_part, num_options_by_part)
        self.assertEquals(json.loads(return_json.content), processed_data)
    def test_process_analytics_answer_dist_inconsistency(self):
        """
        Test with data having part_id that doesn't exist in the problem definition.
        """
        data = [
            {
                'course_id': 'A/B/C',
                'module_id': 'i4x://A/B/problem/f3ed0ba7f89445ee9a83541e1fc8a2f2',
                'part_id': 'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1',
                'correct': False,
                'count': 7,
                'value_id': 'null',
                'answer_value_text': 8,
                'answer_value_numeric': 'null',
                'variant': None,
                'created': '2014-10-15T101351',
            },
        ]

        question_types_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_2': 'option',
        }

        num_options_by_part = {
            'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_2': 0,
        }

        processed_data = {
            'count_by_part': {
            },
            'data_by_part': {
            },
            'message_by_part': {
                'i4x-A-B-problem-f3ed0ba7f89445ee9a83541e1fc8a2f2_2_1': (
                    'The analytics cannot be displayed as there is an inconsistency in the data.'
                )
            },
            'last_update_date': 'Oct 15, 2014 at 10:13 UTC'
        }

        return_json = process_analytics_answer_dist(data, question_types_by_part, num_options_by_part)
        self.assertEquals(json.loads(return_json.content), processed_data)