Skip to content

Commit

Permalink
prevent Filter.is_active from false positives due to a default op bei…
Browse files Browse the repository at this point in the history
…ng set (necessary values may not be provided for the filter to apply)
  • Loading branch information
guruofgentoo committed Jan 9, 2017
1 parent c89b3cf commit 0a5e60b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog
0.1.32 released <in development>
==========================

- ?
- corrected the results of Filter.is_active to account for default operation with no value

0.1.31 released 2016-11-03
==========================
Expand Down
5 changes: 4 additions & 1 deletion webgrid/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ def __init__(self, sa_col, default_op=None, default_value1=None, default_value2=

@property
def is_active(self):
return self.op is not None and not self.error
operator_by_key = {op.key: op for op in self.operators}
return self.op is not None and not self.error and (
operator_by_key[self.op].field_type is None or self.value1 is not None
)

@property
def is_display_active(self):
Expand Down
8 changes: 8 additions & 0 deletions webgrid/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_doesnt_contain(self):
def test_default(self):
tf = TextFilter(Person.firstname, default_op='contains', default_value1='foo')
tf.set(None, None)
assert tf.is_active
query = tf.apply(db.session.query(Person.id))
self.assert_in_query(query, "WHERE persons.firstname LIKE '%foo%'")

Expand All @@ -80,6 +81,7 @@ def def_op():
return 'contains'
tf = TextFilter(Person.firstname, default_op=def_op, default_value1='bar')
tf.set(None, None)
assert tf.is_active
query = tf.apply(db.session.query(Person.id))
self.assert_in_query(query, "WHERE persons.firstname LIKE '%bar%'")

Expand All @@ -88,9 +90,15 @@ def def_val():
return 'bar'
tf = TextFilter(Person.firstname, default_op='contains', default_value1=def_val)
tf.set(None, None)
assert tf.is_active
query = tf.apply(db.session.query(Person.id))
self.assert_in_query(query, "WHERE persons.firstname LIKE '%bar%'")

def test_default_no_value(self):
tf = TextFilter(Person.firstname, default_op='contains')
tf.set(None, None)
assert not tf.is_active


class TestTextFilterWithCaseSensitiveDialect(CheckFilterBase):
def get_filter(self):
Expand Down

0 comments on commit 0a5e60b

Please sign in to comment.