Skip to content

Commit

Permalink
bug fix in option array enum filter
Browse files Browse the repository at this point in the history
  • Loading branch information
guruofgentoo committed Nov 10, 2021
1 parent 2ae9dea commit 4f09060
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions webgrid/filters.py
Expand Up @@ -655,14 +655,14 @@ def search(value):

def apply(self, query):
"""Query modifier to apply the needed clauses for filter inputs."""
if self.op == self.ops.is_:
if self.op == ops.is_:
if len(self.value1) == 1 and self.value1[0]:
return query.filter(self.sa_col.any(self.value1[0].name))
elif len(self.value1) > 1:
return query.filter(self.sa_col.contains(self.value1))
else:
return query
if self.op == self.ops.not_is:
if self.op == ops.not_is:
if len(self.value1) == 1 and self.value1[0]:
return query.filter(~self.sa_col.any(self.value1[0].name))
elif len(self.value1) > 1:
Expand Down
28 changes: 27 additions & 1 deletion webgrid/tests/test_filters.py
Expand Up @@ -8,9 +8,11 @@

from webgrid.filters import Operator
from webgrid.filters import OptionsFilterBase, TextFilter, IntFilter, NumberFilter, DateFilter, \
DateTimeFilter, FilterBase, TimeFilter, YesNoFilter, OptionsEnumFilter, AggregateIntFilter
DateTimeFilter, FilterBase, TimeFilter, YesNoFilter, OptionsEnumFilter, AggregateIntFilter, \
OptionsEnumArrayFilter
from webgrid.testing import query_to_str
from webgrid_ta.model.entities import ArrowRecord, Person, db, AccountType
from webgrid_ta.model import entities as ents

from .helpers import ModelBase

Expand Down Expand Up @@ -1649,6 +1651,30 @@ class AccountTypeFilter(OptionsEnumFilter):
self.assert_filter_query(filter, "WHERE persons.account_type = 'admin'")


@pytest.mark.skipif(
db.engine.dialect.name != 'postgresql',
reason='current filter depends on PG array type and cannot use the generic'
)
class TestEnumArrayFilter(CheckFilterBase):
def test_is(self):
filter = OptionsEnumArrayFilter(
ents.ArrayTable.account_type, enum_type=AccountType).new_instance()
filter.set('is', ['admin'])
self.assert_filter_query(filter, "WHERE 'admin' = ANY (array_table.account_type)")

def test_is_multiple(self):
filter = OptionsEnumArrayFilter(
ents.ArrayTable.account_type, enum_type=AccountType).new_instance()
filter.set('is', ['admin', 'manager'])
self.assert_filter_query(filter, "WHERE array_table.account_type @> ('admin', 'manager')")

def test_literal_value(self):
filter = OptionsEnumArrayFilter(
ents.ArrayTable.account_type, enum_type=AccountType).new_instance()
filter.set('is', [AccountType.admin])
self.assert_filter_query(filter, "WHERE 'admin' = ANY (array_table.account_type)")


class TestIntrospect(CheckFilterBase):
def test_new_instance(self):
class TestFilter(FilterBase):
Expand Down
9 changes: 9 additions & 0 deletions webgrid_ta/model/entities.py
Expand Up @@ -132,3 +132,12 @@ class Stopwatch(db.Model, DefaultMixin):
stop_time_lap2 = sa.Column(sa.DateTime)
start_time_lap3 = sa.Column(sa.DateTime)
stop_time_lap3 = sa.Column(sa.DateTime)


if db.engine.dialect.name == 'postgresql':
class ArrayTable(db.Model, DefaultMixin):
__tablename__ = 'array_table'

id = sa.Column(sa.Integer, primary_key=True)
account_type = sa.Column(
sa.dialects.postgresql.ARRAY(sa.Enum(AccountType, name='person_account_type')))

0 comments on commit 4f09060

Please sign in to comment.