-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from DanCardin/dc/view_function
feat: Allow __view__ to be a callable.
- Loading branch information
Showing
3 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from pytest_mock_resources import create_postgres_fixture | ||
from sqlalchemy import Column, text, types | ||
|
||
from sqlalchemy_declarative_extensions import ( | ||
declarative_database, | ||
register_sqlalchemy_events, | ||
view, | ||
) | ||
from sqlalchemy_declarative_extensions.sqlalchemy import declarative_base, select | ||
|
||
_Base = declarative_base() | ||
|
||
|
||
@declarative_database | ||
class Base(_Base): # type: ignore | ||
__abstract__ = True | ||
|
||
|
||
class Foo(Base): | ||
__tablename__ = "foo" | ||
id = Column(types.Integer(), primary_key=True) | ||
|
||
|
||
@view(Base) | ||
class HighFoo: | ||
__tablename__ = "high_foo" | ||
|
||
@staticmethod | ||
def __view__(): | ||
return select(Foo.__table__).where(Foo.__table__.c.id >= 10) | ||
|
||
|
||
register_sqlalchemy_events(Base.metadata, schemas=True, views=True, rows=True) | ||
|
||
pg = create_postgres_fixture( | ||
scope="function", engine_kwargs={"echo": True}, session=True | ||
) | ||
|
||
|
||
def test_created_together(pg): | ||
Base.metadata.create_all(pg.connection()) | ||
|
||
for i in range(8, 12): | ||
pg.add(Foo(id=i)) | ||
pg.commit() | ||
|
||
result = [i for (i,) in pg.execute(text('select id from "high_foo"'))] | ||
assert result == [10, 11] |