<commit_before>import json
import copy
from uuid import uuid4
from django.test import TestCase
from django.conf import settings

from student.models import Registration
from django.contrib.auth.models import User

import xmodule.modulestore.django
from xmodule.templates import update_templates


class ModuleStoreTestCase(TestCase):
    """ Subclass for any test case that uses the mongodb
    module store. This populates a uniquely named modulestore
    collection with templates before running the TestCase
    and drops it they are finished. """

    def _pre_setup(self):
        super(ModuleStoreTestCase, self)._pre_setup()

        # Use a uuid to differentiate
        # the mongo collections on jenkins.
        self.orig_MODULESTORE = copy.deepcopy(settings.MODULESTORE)
        self.test_MODULESTORE = self.orig_MODULESTORE
        self.test_MODULESTORE['default']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex
        self.test_MODULESTORE['direct']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex
        settings.MODULESTORE = self.test_MODULESTORE

        # Flush and initialize the module store
        # It needs the templates because it creates new records
        # by cloning from the template.
        # Note that if your test module gets in some weird state
        # (though it shouldn't), do this manually
        # from the bash shell to drop it:
        # $ mongo test_xmodule --eval "db.dropDatabase()"
        xmodule.modulestore.django._MODULESTORES = {}
        update_templates()

    def _post_teardown(self):
        # Make sure you flush out the modulestore.
        # Drop the collection at the end of the test,
        # otherwise there will be lingering collections leftover
        # from executing the tests.
        xmodule.modulestore.django._MODULESTORES = {}
        xmodule.modulestore.django.modulestore().collection.drop()
        settings.MODULESTORE = self.orig_MODULESTORE

        super(ModuleStoreTestCase, self)._post_teardown()


def parse_json(response):
    """Parse response, which is assumed to be json"""
    return json.loads(response.content)


def user(email):
    """look up a user by email"""
    return User.objects.get(email=email)


def registration(email):
    """look up registration object by email"""
    return Registration.objects.get(user__email=email)<commit_msg>Optimized ModuleStoreTestCase to reload templates only once over all test runs.<commit_after>'''
Utilities for contentstore tests
'''

#pylint: disable=W0603

import json
import copy
from uuid import uuid4
from django.test import TestCase
from django.conf import settings

from student.models import Registration
from django.contrib.auth.models import User

import xmodule.modulestore.django
from xmodule.templates import update_templates

# Share modulestore setup between classes
# We need to use global variables, because
# each ModuleStoreTestCase subclass will have its
# own class variables, and we want to re-use the
# same modulestore for all test cases.

#pylint: disable=C0103
test_modulestore = None
#pylint: disable=C0103
orig_modulestore = None


class ModuleStoreTestCase(TestCase):
    """ Subclass for any test case that uses the mongodb
    module store. This populates a uniquely named modulestore
    collection with templates before running the TestCase
    and drops it they are finished. """

    @staticmethod
    def flush_mongo_except_templates():
        '''
        Delete everything in the module store except templates
        '''
        modulestore = xmodule.modulestore.django.modulestore()

        # This query means: every item in the collection
        # that is not a template
        query = { "_id.course": { "$ne": "templates" }}

        # Remove everything except templates
        modulestore.collection.remove(query)

    @staticmethod
    def load_templates_if_necessary():
        '''
        Load templates into the modulestore only if they do not already exist.
        We need the templates, because they are copied to create
        XModules such as sections and problems
        '''
        modulestore = xmodule.modulestore.django.modulestore()

        # Count the number of templates
        query = { "_id.course": "templates"}
        num_templates = modulestore.collection.find(query).count()

        if num_templates < 1:
            update_templates()

    @classmethod
    def setUpClass(cls):
        '''
        Flush the mongo store and set up templates
        '''
        global test_modulestore
        global orig_modulestore

        # Use a uuid to differentiate
        # the mongo collections on jenkins.
        if test_modulestore is None:
            orig_modulestore = copy.deepcopy(settings.MODULESTORE)
            test_modulestore = orig_modulestore
            test_modulestore['default']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex
            test_modulestore['direct']['OPTIONS']['collection'] = 'modulestore_%s' % uuid4().hex
            xmodule.modulestore.django._MODULESTORES = {}

        settings.MODULESTORE = test_modulestore

        TestCase.setUpClass()

    @classmethod
    def tearDownClass(cls):
        '''
        Revert to the old modulestore settings
        '''
        settings.MODULESTORE = orig_modulestore

    def _pre_setup(self):
        '''
        Remove everything but the templates before each test
        '''

        # Flush anything that is not a template
        ModuleStoreTestCase.flush_mongo_except_templates()

        # Check that we have templates loaded; if not, load them
        ModuleStoreTestCase.load_templates_if_necessary()

        # Call superclass implementation
        TestCase._pre_setup(self)

    def _post_teardown(self):
        '''
        Flush everything we created except the templates
        '''
        # Flush anything that is not a template
        ModuleStoreTestCase.flush_mongo_except_templates()

        # Call superclass implementation
        TestCase._post_teardown(self)



def parse_json(response):
    """Parse response, which is assumed to be json"""
    return json.loads(response.content)


def user(email):
    """look up a user by email"""
    return User.objects.get(email=email)


def registration(email):
    """look up registration object by email"""
    return Registration.objects.get(user__email=email)
{'fraction_non_alphanumeric': 0.07681410357311506, 'fraction_numerical': 0.0029907130489532506, 'mean_word_length': 3.057471264367816, 'pattern_counts': {'":': 3, '<': 4, '<?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': '1999893', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1686, 'n_words': 546}