-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for qfilter + replace return_query by query + decorator is n…
…ow usable without args
- Loading branch information
1 parent
08d8134
commit 7637cf2
Showing
2 changed files
with
92 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
from flask import Flask | ||
from sqlalchemy import func | ||
|
||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
from utils_flask_sqla.models import qfilter | ||
|
||
|
||
db = SQLAlchemy() | ||
|
||
|
||
class FooModel(db.Model): | ||
pk = db.Column(db.Integer, primary_key=True) | ||
|
||
|
||
class BarModel(db.Model): | ||
pk = db.Column(db.Integer, primary_key=True) | ||
|
||
@qfilter | ||
def where_pk(cls, pk, **kwargs): | ||
return BarModel.pk == pk | ||
|
||
@qfilter(query=True) | ||
def where_pk_query(cls, pk, **kwargs): | ||
query = kwargs["query"] | ||
return query.where(BarModel.pk == pk) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def app(): | ||
app = Flask("utils-flask-sqla") | ||
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///" | ||
db.init_app(app) | ||
with app.app_context(): | ||
db.create_all() | ||
yield app | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def foo(app): | ||
foo = FooModel() | ||
db.session.add(foo) | ||
db.session.commit() | ||
return foo | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def bar(app): | ||
bar = BarModel() | ||
db.session.add(bar) | ||
db.session.commit() | ||
return bar | ||
|
||
|
||
class TestQfilter: | ||
def test_qfilter_returns_whereclause(self, bar): | ||
assert db.session.scalars(BarModel.where_pk_query(bar.pk)).one_or_none() is bar | ||
assert ( | ||
db.session.scalars(db.select(BarModel).where(BarModel.where_pk(bar.pk))).one_or_none() | ||
is bar | ||
) | ||
|
||
assert db.session.scalars(BarModel.where_pk_query(bar.pk + 1)).one_or_none() is not bar | ||
assert ( | ||
db.session.scalars( | ||
db.select(BarModel).where(BarModel.where_pk(bar.pk + 1)) | ||
).one_or_none() | ||
is not bar | ||
) |