<commit_before>from lettuce import before, world, after
from salad.logger import logger

try:
    from django.core.management import call_command
    from django.conf import settings
    from django.test.simple  import DjangoTestSuiteRunner
    from django.core import mail
    try:
        from south.management.commands import patch_for_test_db_setup
        USE_SOUTH = getattr(settings, "SOUTH_TESTS_MIGRATE", False)
    except:
        USE_SOUTH = False

    @before.all
    def setup_database():
        logger.info("Setting up a test database ...\n")

        if settings.DATABASES["default"]["ENGINE"] != "django.db.backends.sqlite3":
            from django.db import connection
            database_name = settings.DATABASES["default"]["NAME"]
            cursor = connection.cursor()
            cursor.execute("DROP DATABASE %s" % database_name)
            cursor.execute("CREATE DATABASE %s" % database_name)
            connection.close()
        else:
            # connection.settings_dict["NAME"] = ":memory:"
            pass

        if USE_SOUTH:
            patch_for_test_db_setup()

        world.test_runner = DjangoTestSuiteRunner(interactive=False)
        world.test_runner.setup_test_environment()

        call_command('syncdb', interactive=False, verbosity=0)
        call_command('flush', interactive=False, verbosity=0)

    @after.all
    def teardown_database(total):
        logger.info("Destroying test database ...\n")
        world.test_runner.teardown_test_environment()

    @after.each_feature
    def reset_data(scenario):
        logger.info("Flushing...")
        call_command('flush', interactive=False, verbosity=0)

    @before.each_feature
    def empty_outbox(scenario):
        logger.info("Emptying outbox...")
        mail.outbox = []


except:
    logger.warn("Django terrains not imported.")<commit_msg>Fixes #3: PostgreSQL test database now created and destroyed correctly when running Django tests<commit_after>from lettuce import before, world, after
from salad.logger import logger

old_database_name = None

try:
    from django.core.management import call_command
    from django.conf import settings
    from django.test.simple  import DjangoTestSuiteRunner
    from django.core import mail
    try:
        from south.management.commands import patch_for_test_db_setup
        USE_SOUTH = getattr(settings, "SOUTH_TESTS_MIGRATE", False)
    except:
        USE_SOUTH = False

    @before.all
    def setup_database():
        logger.info("Setting up a test database ...\n")

        if settings.DATABASES["default"]["ENGINE"] != "django.db.backends.sqlite3":
            from django.db import connection
            global old_database_name
            old_database_name = settings.DATABASES["default"]["NAME"]
            connection.creation.create_test_db()
        else:
            # connection.settings_dict["NAME"] = ":memory:"
            pass

        if USE_SOUTH:
            patch_for_test_db_setup()

        world.test_runner = DjangoTestSuiteRunner(interactive=False)
        world.test_runner.setup_test_environment()

        call_command('syncdb', interactive=False, verbosity=0)
        call_command('flush', interactive=False, verbosity=0)

    @after.all
    def teardown_database(total):
        logger.info("Destroying test database ...\n")
        world.test_runner.teardown_test_environment()
        if settings.DATABASES["default"]["ENGINE"] != "django.db.backends.sqlite3":
            from django.db import connection
            connection.creation.destroy_test_db(old_database_name)

    @after.each_feature
    def reset_data(scenario):
        logger.info("Flushing...")
        call_command('flush', interactive=False, verbosity=0)

    @before.each_feature
    def empty_outbox(scenario):
        logger.info("Emptying outbox...")
        mail.outbox = []


except:
    logger.warn("Django terrains not imported.")
{'fraction_non_alphanumeric': 0.09971874200971619, 'fraction_numerical': 0.0025568908207619537, 'mean_word_length': 2.9002991026919243, 'pattern_counts': {'":': 5, '<': 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': '2105153', 'n_tokens_mistral': 1129, 'n_tokens_neox': 1110, 'n_words': 234}