Exemplo n.º 1
0
def django_db_setup(django_db_blocker,
                    django_db_keepdb,
                    local,
                    request):

    from pytest_django.compat import setup_databases, teardown_databases

    setup_databases_args = {}

    with django_db_blocker.unblock():
        db_cfg = setup_databases(
            verbosity=pytest.config.option.verbose,
            interactive=False,
            **setup_databases_args
        )

    def teardown_database():
        with django_db_blocker.unblock():
            teardown_databases(
                db_cfg,
                verbosity=pytest.config.option.verbose,
            )

    if not django_db_keepdb:
        request.addfinalizer(teardown_database)
Exemplo n.º 2
0
def before_all(context):
    setup_test_environment()
    context.db_cfg = setup_databases(
        verbosity=False,
        interactive=False,
        keepdb=not context.config.userdata.getbool('RESETDB')
    )
def django_db_setup(django_db_blocker,
                    django_db_keepdb,
                    local,
                    request):

    from pytest_django.compat import setup_databases, teardown_databases

    setup_databases_args = {}

    with django_db_blocker.unblock():
        db_cfg = setup_databases(
            verbosity=pytest.config.option.verbose,
            interactive=False,
            **setup_databases_args
        )

    def teardown_database():
        with django_db_blocker.unblock():
            teardown_databases(
                db_cfg,
                verbosity=pytest.config.option.verbose,
            )

    if not django_db_keepdb:
        request.addfinalizer(teardown_database)
Exemplo n.º 4
0
def django_db_setup(
    request,
    django_test_environment,
    django_db_blocker,
    django_db_use_migrations,
    django_db_keepdb,
    django_db_createdb,
    django_db_modify_db_settings,
):
    """This is an override of the original implementation in the https://github.com/pytest-dev/pytest-django plugin
    from file /pytest-django/fixtures.py.

    Because this "hides" the original implementation, it may get out-of-date as that plugin is upgraded. This override
    takes the implementation from pytest-django Release 3.5.1, and extends it in order to execute materialized views
    as part of database setup.

    If requirements.txt shows a different version than the one this is based on: compare, update, and test.
    More work could be put into trying to patch, replace, or wrap implementation of
    ``django.test.utils.setup_databases``, which is the actual method that needs to be wrapped and extended.
    """
    from pytest_django.compat import setup_databases, teardown_databases
    from pytest_django.fixtures import _disable_native_migrations

    setup_databases_args = {}

    if not django_db_use_migrations:
        _disable_native_migrations()

    if django_db_keepdb and not django_db_createdb:
        setup_databases_args["keepdb"] = True

    with django_db_blocker.unblock():
        db_cfg = setup_databases(verbosity=request.config.option.verbose,
                                 interactive=False,
                                 **setup_databases_args)
        # If migrations are skipped, assume matviews and views are not to be (re)created either
        # Other scenarios (such as reuse or keep DB) may still lead to creation of a non-existent DB, so they must be
        # (re)created under those conditions
        if not django_db_use_migrations:
            logger.warning(
                "Skipping generation of materialized views or other views in this test run because migrations are also "
                "being skipped. ")
        else:
            generate_matviews()
            ensure_transaction_etl_view_exists()

    def teardown_database():
        with django_db_blocker.unblock():
            try:
                teardown_databases(db_cfg,
                                   verbosity=request.config.option.verbose)
            except Exception as exc:
                request.node.warn(
                    pytest.PytestWarning(
                        "Error when trying to teardown test databases: %r" %
                        exc))

    if not django_db_keepdb:
        request.addfinalizer(teardown_database)
Exemplo n.º 5
0
def django_db_setup(
    request,
    django_test_environment,
    django_db_blocker,
    django_db_use_migrations,
    django_db_keepdb,
    django_db_createdb,
    django_db_modify_db_settings,
):
    """
    Copied and pasted from here:
    https://github.com/pytest-dev/pytest-django/blob/d2973e21c34d843115acdbccdd7a16cb2714f4d3/pytest_django/fixtures.py#L84

    We override this fixture and give it a "function" scope as a hack to force
    the test database to be re-created per test case.  This causes the same
    database ids to be used for payment records in each test run.  Usage of the
    `reset_sequences` flag provided by pytest-django isn't always sufficient
    since it can depend on whether or not a particular database supports
    sequence resets.

    As an example of why sequence resets are necessary, tests in the
    `tests.integration.test_confirm_payments` module will fail if database ids
    are not reset per test function.  This is because the test wallet on the
    Goerli test net must hardcode payment ids into transactions and token
    transfers.  If payment ids don't deterministically begin at 1 per test
    case, payment ids in the Goerli test wallet won't correctly correspond to
    payment ids generated during test runs.
    """
    from pytest_django.compat import setup_databases, teardown_databases

    setup_databases_args = {}

    if not django_db_use_migrations:
        _disable_native_migrations()

    if django_db_keepdb and not django_db_createdb:
        setup_databases_args["keepdb"] = True

    with django_db_blocker.unblock():
        db_cfg = setup_databases(
            verbosity=request.config.option.verbose,
            interactive=False,
            **setup_databases_args
        )

    def teardown_database():
        with django_db_blocker.unblock():
            try:
                teardown_databases(db_cfg, verbosity=request.config.option.verbose)
            except Exception as exc:
                request.node.warn(
                    pytest.PytestWarning(
                        "Error when trying to teardown test databases: %r" % exc
                    )
                )

    if not django_db_keepdb:
        request.addfinalizer(teardown_database)
Exemplo n.º 6
0
def setup_test_environment(request):
    """
    Sets up a django test environment, makes migration files (deletes
    existing ones first), creates test database and applies migrations.
    At the end database is destroyed, migration files are deleted.

    It is not easy to run two different django apps in one process, even if
    we do it one after the other. Most modules need to be reimported, which
    involves some hacking :(.

    Modules must define:
        - `DJANGO_SETTINGS_MODULE`: will set the environment variable
        - `DJANGO_PROJECT`: path to the project main directory relative to
                            `pytest.ini`
    Optional:
        - `DISABLE_MIGRATIONS`: When set to `True`, test db creation will not
                                use the migration machinery.
    """
    global modules_not_to_delete

    settings_module = request.module.DJANGO_SETTINGS_MODULE
    os.environ['DJANGO_SETTINGS_MODULE'] = settings_module

    project_path = get_project_path(request.module.DJANGO_PROJECT)
    orig_path = sys.path[:]
    sys.path.insert(0, project_path)

    if not modules_not_to_delete:
        modules_not_to_delete = sys.modules.keys()
    else:
        for module in sys.modules.keys():
            if module not in modules_not_to_delete:
                del sys.modules[module]

    _setup_django()

    from django.conf import settings

    settings.DEBUG = False

    if getattr(request.module, 'DISABLE_MIGRATIONS', False):
        settings.MIGRATION_MODULES = DisableMigrations()
    else:
        makemigrations(project_path)

    from pytest_django.compat import (setup_test_environment,
                                      teardown_test_environment,
                                      setup_databases, teardown_databases)

    setup_test_environment()
    db_cfg = setup_databases(verbosity=0, interactive=False)

    def teardown():
        db_keep = getattr(request.module, 'DB_KEEP', False)
        if not db_keep:
            teardown_databases(db_cfg)
        teardown_test_environment()
        sys.path = orig_path
        delete_migrations(project_path)

    request.addfinalizer(teardown)
def setup_test_environment(request):
    """
    Sets up a django test environment, makes migration files (deletes
    existing ones first), creates test database and applies migrations.
    At the end database is destroyed, migration files are deleted.

    It is not easy to run two different django apps in one process, even if
    we do it one after the other. Most modules need to be reimported, which
    involves some hacking :(.

    Modules must define:
        - `DJANGO_SETTINGS_MODULE`: will set the environment variable
        - `DJANGO_PROJECT`: path to the project main directory relative to
                            `pytest.ini`
    Optional:
        - `DISABLE_MIGRATIONS`: When set to `True`, test db creation will not
                                use the migration machinery.
    """
    global modules_not_to_delete

    settings_module = request.module.DJANGO_SETTINGS_MODULE
    os.environ['DJANGO_SETTINGS_MODULE'] = settings_module

    project_path = get_project_path(request.module.DJANGO_PROJECT)
    orig_path = sys.path[:]
    sys.path.insert(0, project_path)

    if not modules_not_to_delete:
        modules_not_to_delete = sys.modules.keys()
    else:
        for module in sys.modules.keys():
            if module not in modules_not_to_delete:
                del sys.modules[module]

    _setup_django()

    from django.conf import settings

    settings.DEBUG = False

    if getattr(request.module, 'DISABLE_MIGRATIONS', False):
        settings.MIGRATION_MODULES = DisableMigrations()
    else:
        makemigrations(project_path)

    from pytest_django.compat import (setup_test_environment,
                                      teardown_test_environment,
                                      setup_databases,
                                      teardown_databases)

    setup_test_environment()
    db_cfg = setup_databases(verbosity=0, interactive=False)

    def teardown():
        db_keep = getattr(request.module, 'DB_KEEP', False)
        if not db_keep:
            teardown_databases(db_cfg)
        teardown_test_environment()
        sys.path = orig_path
        delete_migrations(project_path)

    request.addfinalizer(teardown)
Exemplo n.º 8
0
def before_all(context):
    setup_test_environment()
    context.db_cfg = setup_databases(
        verbosity=False,
        interactive=False,
        keepdb=not context.config.userdata.getbool('RESETDB'))