Exemple #1
0
def db_doctest(request, _django_cursor_wrapper):
    from django.test import TestCase as django_case
    _django_cursor_wrapper.enable()
    request.addfinalizer(_django_cursor_wrapper.disable)
    case = django_case(methodName='__init__')
    case._pre_setup()
    request.addfinalizer(case._post_teardown)
Exemple #2
0
def _django_db_fixture_helper(request,
                              django_db_blocker,
                              transactional=False,
                              reset_sequences=False):
    if is_django_unittest(request):
        return

    if not transactional and "live_server" in request.fixturenames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    if transactional:
        from django.test import TransactionTestCase as django_case

        if reset_sequences:

            class ResetSequenceTestCase(django_case):
                reset_sequences = True

            django_case = ResetSequenceTestCase
    else:
        from django.test import TestCase as django_case

    test_case = django_case(methodName="__init__")
    test_case._pre_setup()
    request.addfinalizer(test_case._post_teardown)
Exemple #3
0
def _django_db_fixture_helper(request, django_db_blocker,
                              transactional=False, reset_sequences=False):
    if is_django_unittest(request):
        return

    if not transactional and 'live_server' in request.funcargnames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    if transactional:
        from django.test import TransactionTestCase as django_case

        if reset_sequences:
            class ResetSequenceTestCase(django_case):
                reset_sequences = True
            django_case = ResetSequenceTestCase
    else:
        from django.test import TestCase as django_case

    test_case = django_case(methodName='__init__')
    test_case._pre_setup()
    request.addfinalizer(test_case._post_teardown)
def _django_db_fixture_helper(transactional, request, _django_cursor_wrapper):
    if is_django_unittest(request):
        return

    if not transactional and 'live_server' in request.funcargnames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_case = None

    _django_cursor_wrapper.enable()
    request.addfinalizer(_django_cursor_wrapper.disable)

    if transactional:
        from django import get_version

        if get_version() >= '1.5':
            from django.test import TransactionTestCase as django_case

        else:
            # Django before 1.5 flushed the DB during setUp.
            # Use pytest-django's old behavior with it.
            def flushdb():
                """Flush the database and close database connections"""
                # Django does this by default *before* each test
                # instead of after.
                from django.db import connections
                from django.core.management import call_command

                for db in connections:
                    call_command('flush',
                                 interactive=False,
                                 database=db,
                                 verbosity=pytest.config.option.verbose)
                for conn in connections.all():
                    conn.close()

            request.addfinalizer(flushdb)

    else:
        from django.test import TestCase as django_case

    if django_case:
        case = django_case(methodName='__init__')
        case._pre_setup()
        request.addfinalizer(case._post_teardown)
Exemple #5
0
def _django_db_fixture_helper(transactional, serialized_rollback,
                              request, _django_cursor_wrapper):
    if is_django_unittest(request):
        return

    if not transactional and 'live_server' in request.funcargnames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_case = None

    _django_cursor_wrapper.enable()
    request.addfinalizer(_django_cursor_wrapper.disable)

    if transactional:
        from django import get_version

        if get_version() >= '1.5':
            from django.test import TransactionTestCase as django_case

        else:
            # Django before 1.5 flushed the DB during setUp.
            # Use pytest-django's old behavior with it.
            def flushdb():
                """Flush the database and close database connections"""
                # Django does this by default *before* each test
                # instead of after.
                from django.db import connections
                from django.core.management import call_command

                for db in connections:
                    call_command('flush', interactive=False, database=db,
                                 verbosity=pytest.config.option.verbose)
                for conn in connections.all():
                    conn.close()
            request.addfinalizer(flushdb)

    else:
        from django.test import TestCase as django_case

    if django_case:
        case = django_case(methodName='__init__')
        case.serialized_rollback = serialized_rollback
        case._pre_setup()
        request.addfinalizer(case._post_teardown)
Exemple #6
0
def _django_db_fixture_helper(transactional, request, django_db_blocker):
    if is_django_unittest(request):
        return

    if not transactional and 'live_server' in request.funcargnames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    if transactional:
        from django.test import TransactionTestCase as django_case
    else:
        from django.test import TestCase as django_case

    test_case = django_case(methodName='__init__')
    test_case._pre_setup()
    request.addfinalizer(test_case._post_teardown)
Exemple #7
0
def _django_db_fixture_helper(transactional, request, django_db_blocker):
    if is_django_unittest(request):
        return

    if not transactional and 'live_server' in request.funcargnames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_db_blocker.enable_database_access()
    request.addfinalizer(django_db_blocker.restore_previous_access)

    if transactional:
        from django.test import TransactionTestCase as django_case
    else:
        from django.test import TestCase as django_case

    test_case = django_case(methodName='__init__')
    test_case._pre_setup()
    request.addfinalizer(test_case._post_teardown)
Exemple #8
0
def _django_db_fixture_helper(
    request, django_db_blocker, databases, transactional=False, reset_sequences=False
):
    if is_django_unittest(request):
        return

    if not transactional and "live_server" in request.fixturenames:
        # Do nothing, we get called with transactional=True, too.
        return

    django_db_blocker.unblock()
    request.addfinalizer(django_db_blocker.restore)

    if transactional:
        from django.test import TransactionTestCase as django_case

        if reset_sequences:

            class ResetSequenceTestCase(django_case):
                reset_sequences = True

            django_case = ResetSequenceTestCase
    else:
        from django.test import TestCase as django_case

    def restore_databases(cls, dbs):
        cls.databases = dbs

    old_databases = getattr(django_case, "databases", None)
    setattr(django_case, "databases", databases)

    test_case = django_case(methodName="__init__")
    test_case._pre_setup()
    test_case.setUpClass()

    request.addfinalizer(
        partial(restore_databases, cls=django_case, dbs=old_databases)
    )
    request.addfinalizer(test_case.tearDownClass)
    request.addfinalizer(test_case._post_teardown)