<commit_before>from nose.plugins.skip import SkipTest

import os, sys
import copy
import subprocess
from StringIO import StringIO

from storm.uri import URI
from feedplatform.management import call_command
from feedplatform import conf


def setup_module():
    # if django is not installed, don't bother with these tests
    try:
        import django
    except ImportError:
        raise SkipTest()


def test_django_integration():
    """Check that the output of a manual "call_command" matches what
    we get from executing ./manage.py.
    """

    # 1. get via process
    managepy = os.path.join(os.path.dirname(__file__), 'proj', 'manage.py')
    process = subprocess.Popen([managepy, 'feedplatform', 'models'],
                               stdout=subprocess.PIPE,
                               shell=os.name == 'nt' and True or False,
                               universal_newlines=True)
    stdout, stderr = process.communicate()
    process_output = stdout

    # 2. get via callcommand
    oldconfig = conf.config._target
    try:
        conf.config.reset()
        sys.path.append(os.path.join(os.path.dirname(__file__), 'proj'))
        try:
            os.environ['FEEDPLATFORM_CONFIG'] = 'feedplatform_config'
            real_stdout = sys.stdout
            sys.stdout = StringIO()
            try:
                call_command('models')
                sys.stdout.seek(0)
                command_output = sys.stdout.read()
            finally:
                # set back to real stdout
                sys.stdout = real_stdout
        finally:
            # remove test django project from path
            sys.path.pop()
    finally:
        # reset to previously used config
        conf.config._target = oldconfig

    # 3. check that the two are the same
    print "command: (%s)" % repr(command_output), len(command_output)
    print "process: (%s)" % repr(process_output), len(process_output)
    assert process_output ==  command_output


def test_make_dsn():
    """Test the Django DSN string builder."""

    # Setup django in the current thread; other tests just
    # used it via an external process.
    from django.core.management import setup_environ<commit_msg>Updated test to use a local Django import rather than going through an external process; this allows us to make sure the Django script is using the FeedPlatform lib we are currently testing, rather than the one installed globally as a default.<commit_after>from nose.plugins.skip import SkipTest

import os, sys
import copy
import subprocess
from StringIO import StringIO

from storm.uri import URI
from feedplatform.management import call_command
from feedplatform import conf


def setup_module():
    # if django is not installed, don't bother with these tests
    try:
        import django
    except ImportError:
        raise SkipTest()


def test_django_integration():
    """Check that the output of a manual "call_command" matches what
    we get from executing through a Django project.
    """

    def _capture_with_fake_syspath(path, call, *args, **kwargs):
        sys.path.append(path)
        try:
            real_stdout = sys.stdout
            sys.stdout = StringIO()
            try:
                call(*args, **kwargs)
                sys.stdout.seek(0)
                return sys.stdout.read()
            finally:
                sys.stdout = real_stdout
        finally:
            sys.path.pop()

    # 1. get through Django
    os.environ['DJANGO_SETTINGS_MODULE'] = 'proj.settings'
    from django.core.management import call_command as dj_call_command
    django_output = _capture_with_fake_syspath(
                          os.path.join(os.path.dirname(__file__)),
                          dj_call_command, 'feedplatform', 'models')

    # 2. get via own callcommand
    oldconfig = conf.config._target
    try:
        conf.config.reset()
        os.environ['FEEDPLATFORM_CONFIG'] = 'feedplatform_config'
        local_output = _capture_with_fake_syspath(
                            os.path.join(os.path.dirname(__file__), 'proj'),
                            call_command, 'models')
    finally:
        # reset to previously used config
        conf.config._target = oldconfig

    # 3. check that the two are the same
    print "command: (%s)" % repr(django_output), len(django_output)
    print "process: (%s)" % repr(local_output), len(local_output)
    assert django_output == local_output


def test_make_dsn():
    """Test the Django DSN string builder."""

    # Setup django in the current thread; other tests just
    # used it via an external process.
    from django.core.management import setup_environ
{'fraction_non_alphanumeric': 0.08290378006872852, 'fraction_numerical': 0.001718213058419244, 'mean_word_length': 2.3795355587808418, 'pattern_counts': {'":': 0, '<': 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': '6946610', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1232, 'n_words': 401}