-
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.
* Update flask to 3.0 * Update SqlAlchemy to 1.4 * Abandon support for debian 10 * Abandon support for python 3.7 * Custom select class to replace query_class (to be removed) --------- Co-authored-by: TheoLechemia <[email protected]> Co-authored-by: Élie Bouttier <[email protected]> Co-authored-by: Jacques Fize <[email protected]> Co-authored-by: Pierre Narcisi <[email protected]>
- Loading branch information
1 parent
c809083
commit b5e306a
Showing
7 changed files
with
166 additions
and
16 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 |
---|---|---|
|
@@ -3,4 +3,4 @@ flask-sqlalchemy | |
flask-migrate | ||
marshmallow | ||
python-dateutil | ||
sqlalchemy>=1.3,<2 | ||
sqlalchemy<2 |
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,15 @@ | ||
from .sqlalchemy import CustomSelect | ||
from flask_sqlalchemy.model import Model | ||
|
||
|
||
class SelectModel(Model): | ||
__abstract__ = True | ||
|
||
@classmethod | ||
@property | ||
def select(cls): | ||
if hasattr(cls, "__select_class__"): | ||
select_cls = cls.__select_class__ | ||
else: | ||
select_cls = CustomSelect | ||
return select_cls._create_future_select(cls) # SQLA 2.0: _create_future_select → _create |
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,19 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
from sqlalchemy.util.langhelpers import public_factory | ||
from sqlalchemy.sql.expression import Select | ||
|
||
|
||
class CustomSelect(Select): | ||
inherit_cache = True | ||
|
||
def where_if(self, condition_to_execute_where, whereclause): | ||
if condition_to_execute_where: | ||
return self.where(whereclause) | ||
else: | ||
return self | ||
|
||
|
||
class CustomSQLAlchemy(SQLAlchemy): | ||
@staticmethod | ||
def select(*entities): | ||
return CustomSelect._create_future_select(*entities) |
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,89 @@ | ||
import pytest | ||
from flask import Flask | ||
from sqlalchemy import func | ||
|
||
from utils_flask_sqla.sqlalchemy import CustomSQLAlchemy, CustomSelect | ||
from utils_flask_sqla.models import SelectModel | ||
|
||
|
||
db = CustomSQLAlchemy(model_class=SelectModel) | ||
|
||
|
||
class FooModel(db.Model): | ||
pk = db.Column(db.Integer, primary_key=True) | ||
|
||
|
||
class BarSelect(CustomSelect): | ||
inherit_cache = True | ||
|
||
def where_pk(self, pk): | ||
return self.where(BarModel.pk == pk) | ||
|
||
|
||
class BarModel(db.Model): | ||
__select_class__ = BarSelect | ||
|
||
pk = db.Column(db.Integer, primary_key=True) | ||
|
||
|
||
@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 TestCustomSelect: | ||
def test_select_where_if(self, foo): | ||
# Filter does not apply, we get foo | ||
assert ( | ||
foo | ||
in db.session.scalars(db.select(FooModel).where_if(False, FooModel.pk != foo.pk)).all() | ||
) | ||
# Filter apply, we does not get foo | ||
assert ( | ||
foo | ||
not in db.session.scalars( | ||
db.select(FooModel).where_if(True, FooModel.pk != foo.pk) | ||
).all() | ||
) | ||
|
||
def test_model_where_if(self, foo): | ||
# Filter does not apply, we get foo | ||
assert ( | ||
foo in db.session.scalars(FooModel.select.where_if(False, FooModel.pk != foo.pk)).all() | ||
) | ||
# Filter apply, we does not get foo | ||
assert ( | ||
foo | ||
not in db.session.scalars(FooModel.select.where_if(True, FooModel.pk != foo.pk)).all() | ||
) | ||
|
||
def test_model_select_class(self, bar): | ||
assert db.session.scalars(BarModel.select.where_pk(bar.pk)).one_or_none() is bar | ||
assert db.session.scalars(BarModel.select.where_pk(bar.pk + 1)).one_or_none() is not bar | ||
|
||
def test_chain_custom_where(self, bar): | ||
assert ( | ||
db.session.scalars(BarModel.select.where_pk(bar.pk).where_pk(bar.pk)).one_or_none() | ||
is bar | ||
) |
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