# Path: psqlextra/models/view.py
# class PostgresMaterializedViewModel(
#     PostgresViewModel, metaclass=PostgresViewModelMeta
# ):
#     """Base class for creating a model that is a materialized view."""
# 
#     class Meta:
#         abstract = True
#         base_manager_name = "objects"
# 
#     @classmethod
#     def refresh(
#         cls, concurrently: bool = False, using: Optional[str] = None
#     ) -> None:
#         """Refreshes this materialized view.
# 
#         Arguments:
#             concurrently:
#                 Whether to tell PostgreSQL to refresh this
#                 materialized view concurrently.
# 
#             using:
#                 Optionally, the name of the database connection
#                 to use for refreshing the materialized view.
#         """
# 
#         conn_name = using or "default"
# 
#         with connections[conn_name].schema_editor() as schema_editor:
#             schema_editor.refresh_materialized_view_model(cls, concurrently)
# 
# class PostgresViewModel(PostgresModel, metaclass=PostgresViewModelMeta):
#     """Base class for creating a model that is a view."""
# 
#     class Meta:
#         abstract = True
#         base_manager_name = "objects"
# 
# Path: tests/fake_model.py
# def define_fake_model(
#     fields=None, model_base=PostgresModel, meta_options={}, **attributes
# ):
#     """Defines a fake model (but does not create it in the database)."""
# 
#     name = str(uuid.uuid4()).replace("-", "")[:8].title()
# 
#     attributes = {
#         "app_label": meta_options.get("app_label") or "tests",
#         "__module__": __name__,
#         "__name__": name,
#         "Meta": type("Meta", (object,), meta_options),
#         **attributes,
#     }
# 
#     if fields:
#         attributes.update(fields)
# 
#     model = type(name, (model_base,), attributes)
# 
#     apps.app_configs[attributes["app_label"]].models[name] = model
#     return model
# 
# def define_fake_view_model(
#     fields=None, view_options={}, meta_options={}, model_base=PostgresViewModel
# ):
#     """Defines a fake view model."""
# 
#     model = define_fake_model(
#         fields=fields,
#         model_base=model_base,
#         meta_options=meta_options,
#         ViewMeta=type("ViewMeta", (object,), view_options),
#     )
# 
#     return model


import pytest
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from psqlextra.models import PostgresMaterializedViewModel, PostgresViewModel
from .fake_model import define_fake_model, define_fake_view_model
    )

    expected_sql = 'SELECT "{0}"."id", "{0}"."name" FROM "{0}"'.format(
        model._meta.db_table
    )
    assert view_model._view_meta.query == (expected_sql, tuple())


@pytest.mark.parametrize(
    "model_base", [PostgresViewModel, PostgresMaterializedViewModel]
)
@pytest.mark.parametrize("bind_params", [("test",), ["test"]])
def test_view_model_meta_sql_with_params(model_base, bind_params):
    """Tests whether you can set a raw SQL query with a tuple of bind params as
    the underlying query for a view."""

    model = define_fake_model({"name": models.TextField()})
    sql = "select * from %s where name = %s" % (model._meta.db_table, "%s")
    sql_with_params = (sql, bind_params)

    view_model = define_fake_view_model(
        {"name": models.TextField()},
        model_base=model_base,
        view_options={"query": sql_with_params},
    )

    assert view_model._view_meta.query == sql_with_params


@pytest.mark.parametrize(

# Path: psqlextra/models/view.py
# class PostgresMaterializedViewModel(
#     PostgresViewModel, metaclass=PostgresViewModelMeta
# ):
#     """Base class for creating a model that is a materialized view."""
# 
#     class Meta:
#         abstract = True
#         base_manager_name = "objects"
# 
#     @classmethod
#     def refresh(
#         cls, concurrently: bool = False, using: Optional[str] = None
#     ) -> None:
#         """Refreshes this materialized view.
# 
#         Arguments:
#             concurrently:
#                 Whether to tell PostgreSQL to refresh this
#                 materialized view concurrently.
# 
#             using:
#                 Optionally, the name of the database connection
#                 to use for refreshing the materialized view.
#         """
# 
#         conn_name = using or "default"
# 
#         with connections[conn_name].schema_editor() as schema_editor:
#             schema_editor.refresh_materialized_view_model(cls, concurrently)
# 
# class PostgresViewModel(PostgresModel, metaclass=PostgresViewModelMeta):
#     """Base class for creating a model that is a view."""
# 
#     class Meta:
#         abstract = True
#         base_manager_name = "objects"
# 
# Path: tests/fake_model.py
# def define_fake_model(
#     fields=None, model_base=PostgresModel, meta_options={}, **attributes
# ):
#     """Defines a fake model (but does not create it in the database)."""
# 
#     name = str(uuid.uuid4()).replace("-", "")[:8].title()
# 
#     attributes = {
#         "app_label": meta_options.get("app_label") or "tests",
#         "__module__": __name__,
#         "__name__": name,
#         "Meta": type("Meta", (object,), meta_options),
#         **attributes,
#     }
# 
#     if fields:
#         attributes.update(fields)
# 
#     model = type(name, (model_base,), attributes)
# 
#     apps.app_configs[attributes["app_label"]].models[name] = model
#     return model
# 
# def define_fake_view_model(
#     fields=None, view_options={}, meta_options={}, model_base=PostgresViewModel
# ):
#     """Defines a fake view model."""
# 
#     model = define_fake_model(
#         fields=fields,
#         model_base=model_base,
#         meta_options=meta_options,
#         ViewMeta=type("ViewMeta", (object,), view_options),
#     )
# 
#     return model


# Path: tests/test_view_models.py
import pytest
from django.core.exceptions import ImproperlyConfigured
from django.db import models
from psqlextra.models import PostgresMaterializedViewModel, PostgresViewModel
from .fake_model import define_fake_model, define_fake_view_model

    )

    expected_sql = 'SELECT "{0}"."id", "{0}"."name" FROM "{0}"'.format(
        model._meta.db_table
    )
    assert view_model._view_meta.query == (expected_sql, tuple())


@pytest.mark.parametrize(
    "model_base", [PostgresViewModel, PostgresMaterializedViewModel]
)
@pytest.mark.parametrize("bind_params", [("test",), ["test"]])
def test_view_model_meta_sql_with_params(model_base, bind_params):
    """Tests whether you can set a raw SQL query with a tuple of bind params as
    the underlying query for a view."""

    model = define_fake_model({"name": models.TextField()})
    sql = "select * from %s where name = %s" % (model._meta.db_table, "%s")
    sql_with_params = (sql, bind_params)

    view_model = define_fake_view_model(
        {"name": models.TextField()},
        model_base=model_base,
        view_options={"query": sql_with_params},
    )

    assert view_model._view_meta.query == sql_with_params


@pytest.mark.parametrize(

    "model_base", [PostgresViewModel, PostgresMaterializedViewModel]