mirror of
https://github.com/d3vyce/sqlalchemy-pgview.git
synced 2026-03-01 23:00:46 +01:00
114 lines
3.6 KiB
Python
114 lines
3.6 KiB
Python
"""Tests for View and MaterializedView classes."""
|
|
|
|
from sqlalchemy import func, select
|
|
|
|
from sqlalchemy_pgview import MaterializedView, View
|
|
|
|
|
|
class TestView:
|
|
"""Tests for the View class."""
|
|
|
|
def test_view_creation(self, sample_tables: tuple) -> None:
|
|
"""Test basic view creation."""
|
|
users, orders = sample_tables
|
|
view = View(
|
|
"user_order_count",
|
|
select(users.c.id, func.count(orders.c.id).label("order_count"))
|
|
.select_from(users.join(orders, users.c.id == orders.c.user_id))
|
|
.group_by(users.c.id),
|
|
)
|
|
|
|
assert view.name == "user_order_count"
|
|
assert view.schema is None
|
|
assert view.fullname == "user_order_count"
|
|
|
|
def test_view_with_schema(self, sample_tables: tuple) -> None:
|
|
"""Test view creation with schema."""
|
|
users, _ = sample_tables
|
|
view = View(
|
|
"active_users",
|
|
select(users.c.id, users.c.name),
|
|
schema="analytics",
|
|
)
|
|
|
|
assert view.name == "active_users"
|
|
assert view.schema == "analytics"
|
|
assert view.fullname == "analytics.active_users"
|
|
|
|
def test_view_columns(self, sample_tables: tuple) -> None:
|
|
"""Test view column derivation."""
|
|
users, _ = sample_tables
|
|
view = View(
|
|
"user_names",
|
|
select(users.c.id, users.c.name.label("user_name")),
|
|
)
|
|
|
|
columns = view.columns
|
|
assert len(columns) == 2
|
|
assert columns[0].name == "id"
|
|
assert columns[1].name == "user_name"
|
|
|
|
def test_view_as_table(self, sample_tables: tuple) -> None:
|
|
"""Test converting view to table for querying."""
|
|
users, _ = sample_tables
|
|
view = View(
|
|
"user_names",
|
|
select(users.c.id, users.c.name),
|
|
)
|
|
|
|
table = view.as_table()
|
|
assert table.name == "user_names"
|
|
assert len(table.columns) == 2
|
|
|
|
def test_view_repr(self, sample_tables: tuple) -> None:
|
|
"""Test view string representation."""
|
|
users, _ = sample_tables
|
|
view = View(
|
|
"test_view",
|
|
select(users.c.id),
|
|
schema="public",
|
|
)
|
|
|
|
assert repr(view) == "View('test_view', schema='public')"
|
|
|
|
|
|
class TestMaterializedView:
|
|
"""Tests for the MaterializedView class."""
|
|
|
|
def test_materialized_view_creation(self, sample_tables: tuple) -> None:
|
|
"""Test basic materialized view creation."""
|
|
users, orders = sample_tables
|
|
mview = MaterializedView(
|
|
"monthly_totals",
|
|
select(users.c.id, func.sum(orders.c.total).label("total"))
|
|
.select_from(users.join(orders, users.c.id == orders.c.user_id))
|
|
.group_by(users.c.id),
|
|
with_data=True,
|
|
)
|
|
|
|
assert mview.name == "monthly_totals"
|
|
assert mview.with_data is True
|
|
|
|
def test_materialized_view_without_data(self, sample_tables: tuple) -> None:
|
|
"""Test materialized view creation without data."""
|
|
users, _ = sample_tables
|
|
mview = MaterializedView(
|
|
"empty_view",
|
|
select(users.c.id),
|
|
with_data=False,
|
|
)
|
|
|
|
assert mview.with_data is False
|
|
|
|
def test_materialized_view_repr(self, sample_tables: tuple) -> None:
|
|
"""Test materialized view string representation."""
|
|
users, _ = sample_tables
|
|
mview = MaterializedView(
|
|
"test_mview",
|
|
select(users.c.id),
|
|
schema="public",
|
|
with_data=True,
|
|
)
|
|
|
|
assert repr(mview) == "MaterializedView('test_mview', schema='public', with_data=True)"
|