Skip to content

Commit

Permalink
support null/is_null sub-ops inside has/any ops
Browse files Browse the repository at this point in the history
The code in `_sub_operator` was assuming that the sub-operator dictionary argument had a `val` key, but in the case of the `null` and `is_null` operators, there is no `val` key, so we use `get` and pass `None` to `_create_operation`.
  • Loading branch information
glyphobet committed Jun 20, 2014
1 parent cbfcb16 commit 6d1a2ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion flask_restless/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _sub_operator(model, argument, fieldname):
if isinstance(argument, dict):
fieldname = argument['name']
operator = argument['op']
argument = argument['val']
argument = argument.get('val')
relation = None
if '__' in fieldname:
fieldname, relation = fieldname.split('__')
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class Computer(self.Base):
owner_id = Column(Integer, ForeignKey('person.id'))
owner = relationship('Person')
programs = relationship('ComputerProgram',
cascade="all, delete-orphan")
cascade="all, delete-orphan", backref='computer')

def speed(self):
return 42
Expand Down
16 changes: 14 additions & 2 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ def test_has_and_any_suboperators(self):
computer1 = self.Computer(name=u'c1', vendor=u'foo')
computer2 = self.Computer(name=u'c2', vendor=u'bar')
computer3 = self.Computer(name=u'c3', vendor=u'bar')
computer4 = self.Computer(name=u'c4', vendor=u'bar')
computer5 = self.Computer(name=u'c5', vendor=u'foo')
computer4 = self.Computer(name=u'c4', vendor=None, programs=[
self.ComputerProgram(program=self.Program())
])
computer5 = self.Computer(name=u'c5', vendor=u'foo', programs=[
self.ComputerProgram(program=self.Program())
])
computer6 = self.Computer(name=u'c6', vendor=u'foo')
self.session.add_all((computer1, computer2, computer3, computer4,
computer5, computer6))
Expand All @@ -214,11 +218,19 @@ def test_has_and_any_suboperators(self):
d = dict(filters=[dict(name='computers', op='any', val=val)])
result = search(self.session, self.Person, d)
assert result.count() == 2
val = dict(name='vendor', op='is_null')
d = dict(filters=[dict(name='computers', op='any', val=val)])
result = search(self.session, self.Person, d)
assert result.count() == 1
# test 'has'
val = dict(name='name', op='like', val=u'%incol%')
d = dict(filters=[dict(name='owner', op='has', val=val)])
result = search(self.session, self.Computer, d)
assert result.count() == 3
val = dict(name='vendor', op='is_null')
d = dict(filters=[dict(name='computer', op='has', val=val)])
result = search(self.session, self.ComputerProgram, d)
assert result.count() == 1

def test_has_and_any_nested_suboperators(self):
"""Tests for the ``"has"`` and ``"any"`` operators with nested
Expand Down

0 comments on commit 6d1a2ad

Please sign in to comment.