<commit_before># file django/fedora/__init__.py
# 
#   Copyright 2010 Emory University General Library
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from django.conf import settings
from django.core.management import call_command

from eulcore.django.testsetup import starting_tests, finished_tests
from eulcore.django.fedora.server import Repository, init_pooled_connection

_stored_default_fedora_root = None
_stored_default_fedora_pidspace = None

def _use_test_fedora(sender, **kwargs):
    global _stored_default_fedora_root
    global _stored_default_fedora_pidspace
    
    _stored_default_fedora_root = getattr(settings, "FEDORA_ROOT", None)
    _stored_default_fedora_pidspace = getattr(settings, "FEDORA_PIDSPACE", None)

    if getattr(settings, "FEDORA_TEST_ROOT", None):
        settings.FEDORA_ROOT = settings.FEDORA_TEST_ROOT
        print "Switching to test Fedora: %s" % settings.FEDORA_ROOT
        # pooled fedora connection gets initialized before this change;
        # re-initialize connection with new fedora root configured
        init_pooled_connection()
    else:
        print "FEDORA_TEST_ROOT is not configured in settings; tests will run against %s" % \
            settings.FEDORA_ROOT

    if getattr(settings, "FEDORA_TEST_PIDSPACE", None):
        settings.FEDORA_PIDSPACE = settings.FEDORA_TEST_PIDSPACE
    elif getattr(settings, "FEDORA_PIDSPACE", None):
        settings.FEDORA_PIDSPACE = "%s-test" % settings.FEDORA_PIDSPACE
    print "Using Fedora pidspace: %s" % settings.FEDORA_PIDSPACE

    # run syncrepo to load any content models or fixtures
    call_command('syncrepo')

def _restore_fedora_root(sender, **kwargs):
    global _stored_default_fedora_root
    global _stored_default_fedora_pidspace

    # if there was a pidspace configured, clean up any test objects
    if _stored_default_fedora_pidspace is not None:
        repo = Repository()
        test_objects = repo.find_objects(pid__contains='%s*' % settings.FEDORA_PIDSPACE)
        count = 0
        for obj in test_objects:
            repo.purge_object(obj.pid, "removing test object")
            count += 1
        if count:
            print "Removed %s test object(s) with pidspace %s" % (count, settings.FEDORA_PIDSPACE)
        print "Restoring Fedora pidspace: %s" % _stored_default_fedora_pidspace
        settings.FEDORA_PIDSPACE = _stored_default_fedora_pidspace        
    if _stored_default_fedora_root is not None:
        print "Restoring Fedora root: %s" % _stored_default_fedora_root
        settings.FEDORA_ROOT = _stored_default_fedora_root
        # re-initialize pooled connection with restored fedora root
        init_pooled_connection()


starting_tests.connect(_use_test_fedora)<commit_msg>clean out test objects at beginning and end of fedora unit tests<commit_after># file django/fedora/__init__.py
# 
#   Copyright 2010 Emory University General Library
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

from django.conf import settings
from django.core.management import call_command

from eulcore.django.testsetup import starting_tests, finished_tests
from eulcore.django.fedora.server import Repository, init_pooled_connection

_stored_default_fedora_root = None
_stored_default_fedora_pidspace = None

def _use_test_fedora(sender, **kwargs):
    global _stored_default_fedora_root
    global _stored_default_fedora_pidspace
    
    _stored_default_fedora_root = getattr(settings, "FEDORA_ROOT", None)
    _stored_default_fedora_pidspace = getattr(settings, "FEDORA_PIDSPACE", None)

    if getattr(settings, "FEDORA_TEST_ROOT", None):
        settings.FEDORA_ROOT = settings.FEDORA_TEST_ROOT
        print "Switching to test Fedora: %s" % settings.FEDORA_ROOT
        # pooled fedora connection gets initialized before this change;
        # re-initialize connection with new fedora root configured
        init_pooled_connection()
    else:
        print "FEDORA_TEST_ROOT is not configured in settings; tests will run against %s" % \
            settings.FEDORA_ROOT

    if getattr(settings, "FEDORA_TEST_PIDSPACE", None):
        settings.FEDORA_PIDSPACE = settings.FEDORA_TEST_PIDSPACE
    elif getattr(settings, "FEDORA_PIDSPACE", None):
        settings.FEDORA_PIDSPACE = "%s-test" % settings.FEDORA_PIDSPACE
    print "Using Fedora pidspace: %s" % settings.FEDORA_PIDSPACE
    
    # remove any test objects left over from a previous test run
    remove_test_objects()
    # run syncrepo to load any content models or fixtures
    call_command('syncrepo')

def _restore_fedora_root(sender, **kwargs):
    global _stored_default_fedora_root
    global _stored_default_fedora_pidspace

    # if there was a pidspace configured, clean up any test objects
    if _stored_default_fedora_pidspace is not None:
        remove_test_objects()
        print "Restoring Fedora pidspace: %s" % _stored_default_fedora_pidspace
        settings.FEDORA_PIDSPACE = _stored_default_fedora_pidspace        
    if _stored_default_fedora_root is not None:
        print "Restoring Fedora root: %s" % _stored_default_fedora_root
        settings.FEDORA_ROOT = _stored_default_fedora_root
        # re-initialize pooled connection with restored fedora root
        init_pooled_connection()

def remove_test_objects():
    repo = Repository()
    test_objects = repo.find_objects(pid__contains='%s*' % settings.FEDORA_PIDSPACE)
    count = 0
    for obj in test_objects:
        repo.purge_object(obj.pid, "removing test object")
        count += 1
    if count:
        print "Removed %s test object(s) with pidspace %s" % (count, settings.FEDORA_PIDSPACE)


starting_tests.connect(_use_test_fedora)
{'fraction_non_alphanumeric': 0.06452581032412966, 'fraction_numerical': 0.003001200480192077, 'mean_word_length': 3.8756400877834674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5379889', 'n_tokens_mistral': 2065, 'n_tokens_neox': 2038, 'n_words': 581}