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
        )
        generate_matviews()
        ensure_transaction_delta_view_exists()

    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)
def convert_traditional_views_to_materialized_views(transactional_db):
    """
    In conftest.py, we convert materialized views to traditional views for testing performance reasons.
    For the following test to work, we will need to undo all the traditional view stuff and generate
    actual materialized views.  Once done, we want to convert everything back to traditional views.
    """

    # Get rid of the traditional views that replace our materialized views for tests.
    with connection.cursor() as cursor:
        cursor.execute("; ".join(f"drop view if exists {v} cascade"
                                 for v in ALL_MATVIEWS))

    # Create materialized views.
    generate_matviews(materialized_views_as_traditional_views=False)

    yield

    # Great.  Test is over.  Drop all of our materialized views.
    with connection.cursor() as cursor:
        cursor.execute("; ".join(
            f"drop materialized view if exists {v} cascade"
            for v in ALL_MATVIEWS))

    # Recreate our traditional views.
    generate_matviews(materialized_views_as_traditional_views=True)
Exemplo n.º 3
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.º 4
0
def refresh_matviews():
    generate_matviews()
def refresh_matviews():
    generate_matviews()