-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(api): add Query Class to sites, grps & cats * feat(api): add sort and fix _get_model Via _get_entity * test(api): test sort query * feat(api): add api sort/sort_dir params To be able to sort through REST Api * fix(api): check if integer to avoid using ilike In filter_by_params * test(api): add test to check filter integer
- Loading branch information
1 parent
47a2e88
commit 2f6f8e2
Showing
7 changed files
with
140 additions
and
15 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,37 @@ | ||
from flask_sqlalchemy import BaseQuery | ||
from sqlalchemy import Integer, and_ | ||
from werkzeug.datastructures import MultiDict | ||
|
||
|
||
class Query(BaseQuery): | ||
def _get_entity(self, entity): | ||
if hasattr(entity, "_entities"): | ||
return self._get_entity(entity._entities[0]) | ||
return entity.entities[0] | ||
|
||
def _get_model(self): | ||
# When sqlalchemy is updated: | ||
# return self._raw_columns[0].entity_namespace | ||
# But for now: | ||
entity = self._get_entity(self) | ||
return entity.c | ||
|
||
def filter_by_params(self, params: MultiDict = None): | ||
model = self._get_model() | ||
and_list = [] | ||
for key, value in params.items(): | ||
column = getattr(model, key) | ||
if isinstance(column.type, Integer): | ||
and_list.append(column == value) | ||
else: | ||
and_list.append(column.ilike(f"%{value}%")) | ||
and_query = and_(*and_list) | ||
return self.filter(and_query) | ||
|
||
def sort(self, label: str, direction: str): | ||
model = self._get_model() | ||
order_by = getattr(model, label) | ||
if direction == "desc": | ||
order_by = order_by.desc() | ||
|
||
return self.order_by(order_by) |
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
36 changes: 36 additions & 0 deletions
36
backend/gn_module_monitoring/tests/test_monitoring/test_models/test_sites_groups.py
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,36 @@ | ||
import pytest | ||
|
||
from gn_module_monitoring.monitoring.models import TMonitoringSitesGroups | ||
|
||
|
||
@pytest.mark.usefixtures("temporary_transaction") | ||
class TestTMonitoringSitesGroups: | ||
def test_sort_desc(self, sites_groups): | ||
if len(sites_groups) < 2: | ||
pytest.xfail( | ||
"This test cannot work if there is less than 2 sites_groups in database (via fixtures or not)" | ||
) | ||
|
||
query = TMonitoringSitesGroups.query.filter( | ||
TMonitoringSitesGroups.id_sites_group.in_( | ||
group.id_sites_group for group in sites_groups.values() | ||
) | ||
).sort(label="id_sites_group", direction="desc") | ||
result = query.all() | ||
|
||
assert result[0].id_sites_group > result[1].id_sites_group | ||
|
||
def test_sort_asc(self, sites_groups): | ||
if len(sites_groups) < 2: | ||
pytest.xfail( | ||
"This test cannot work if there is less than 2 sites_groups in database (via fixtures or not)" | ||
) | ||
|
||
query = TMonitoringSitesGroups.query.filter( | ||
TMonitoringSitesGroups.id_sites_group.in_( | ||
group.id_sites_group for group in sites_groups.values() | ||
) | ||
).sort(label="id_sites_group", direction="asc") | ||
result = query.all() | ||
|
||
assert result[0].id_sites_group < result[1].id_sites_group |
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