<commit_before>"""
Tests for class dashboard (Metrics tab in instructor dashboard)
"""
from __future__ import absolute_import

import json

from django.test.client import RequestFactory
from mock import patch
from six import text_type

from class_dashboard import views
from student.tests.factories import AdminFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory


class TestViews(ModuleStoreTestCase):
    """
    Tests related to class_dashboard/views.py
    """

    def setUp(self):
        super(TestViews, self).setUp()

        self.request_factory = RequestFactory()
        self.request = self.request_factory.get('')
        self.request.user = None
        self.simple_data = {'error': 'error'}

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

        self.assertEqual(json.dumps(self.simple_data), response.content.decode('utf-8'))

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

        self.assertEqual(
            "{\"error\": \"Access Denied: User does not have access to this course\'s data\"}",
            response.content.decode('utf-8')
        )

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

        self.assertEqual(json.dumps(self.simple_data), response.content)

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

        self.assertEqual("{\"error\": \"Access Denied: User does not have access to this course\'s data\"}", response.content)

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

        self.assertEqual(json.dumps(self.simple_data), response.content)

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

        self.assertEqual("{\"error\": \"Access Denied: User does not have access to this course\'s data\"}", response.content)

    def test_sending_deprecated_id(self):

        course = CourseFactory.create()
        instructor = AdminFactory.create()
        self.request.user = instructor

        response = views.all_sequential_open_distrib(self.request, text_type(course.id))
        self.assertEqual('[]', response.content.decode('utf-8'))

        response = views.all_problem_grade_distribution(self.request, text_type(course.id))
        self.assertEqual('[]', response.content.decode('utf-8'))

        response = views.section_problem_grade_distrib(self.request, text_type(course.id), 'no section')
        self.assertEqual('{"error": "error"}', response.content.decode('utf-8'))
<commit_msg>Fixes for Python 3 decoding<commit_after>"""
Tests for class dashboard (Metrics tab in instructor dashboard)
"""
from __future__ import absolute_import

import json

from django.test.client import RequestFactory
from mock import patch
from six import text_type

from class_dashboard import views
from student.tests.factories import AdminFactory
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory


class TestViews(ModuleStoreTestCase):
    """
    Tests related to class_dashboard/views.py
    """

    def setUp(self):
        super(TestViews, self).setUp()

        self.request_factory = RequestFactory()
        self.request = self.request_factory.get('')
        self.request.user = None
        self.simple_data = {'error': 'error'}

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

        self.assertEqual(json.dumps(self.simple_data), response.content.decode('utf-8'))

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_problem_grade_distribution(self.request, 'test/test/test')

        self.assertEqual(
            "{\"error\": \"Access Denied: User does not have access to this course\'s data\"}",
            response.content.decode('utf-8')
        )

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

        self.assertEqual(json.dumps(self.simple_data), response.content.decode('utf-8'))

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_all_sequential_open_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.all_sequential_open_distrib(self.request, 'test/test/test')

        self.assertEqual(
            "{\"error\": \"Access Denied: User does not have access to this course\'s data\"}",
            response.content.decode('utf-8')
        )

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_has_access(self, has_access):
        """
        Test returns proper value when have proper access
        """
        has_access.return_value = True
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

        self.assertEqual(json.dumps(self.simple_data), response.content.decode('utf-8'))

    @patch('class_dashboard.views.has_instructor_access_for_class')
    def test_section_problem_grade_distribution_no_access(self, has_access):
        """
        Test for no access
        """
        has_access.return_value = False
        response = views.section_problem_grade_distrib(self.request, 'test/test/test', '1')

        self.assertEqual(
            "{\"error\": \"Access Denied: User does not have access to this course\'s data\"}",
            response.content.decode('utf-8')
        )

    def test_sending_deprecated_id(self):

        course = CourseFactory.create()
        instructor = AdminFactory.create()
        self.request.user = instructor

        response = views.all_sequential_open_distrib(self.request, text_type(course.id))
        self.assertEqual('[]', response.content.decode('utf-8'))

        response = views.all_problem_grade_distribution(self.request, text_type(course.id))
        self.assertEqual('[]', response.content.decode('utf-8'))

        response = views.section_problem_grade_distrib(self.request, text_type(course.id), 'no section')
        self.assertEqual('{"error": "error"}', response.content.decode('utf-8'))

{'fraction_non_alphanumeric': 0.10656316160903317, 'fraction_numerical': 0.002234768289814161, 'mean_word_length': 3.45883586785527, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7583584', 'n_tokens_mistral': 2450, 'n_tokens_neox': 2391, 'n_words': 460}