Skip to content

Commit

Permalink
Adds test for filtering on hybrid property.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed Sep 19, 2016
1 parent 80fe63b commit 9a25a94
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from sqlalchemy import Unicode
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import backref
from sqlalchemy.orm import relationship
from testing.postgresql import PostgresqlFactory as PGFactory
Expand Down Expand Up @@ -122,6 +123,12 @@ class Person(self.Base):
birth_datetime = Column(DateTime)
bedtime = Column(Time)

@hybrid_property
def is_minor(self):
if not hasattr(self, 'age') or self.age is None:
return False
return self.age <= 18

class Comment(self.Base):
__tablename__ = 'comment'
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -780,6 +787,25 @@ def test_to_many_relation(self):
articles = document['data']
assert ['3', '4'] == sorted(article['id'] for article in articles)

def test_hybrid_property(self):
"""Test for filtering on a hybrid property.
In this test, the hybrid attribute and the SQL expression are of
the same type.
"""
person1 = self.Person(id=1, age=10)
person2 = self.Person(id=2, age=20)
self.session.add_all([person1, person2])
self.session.commit()
filters = [dict(name='is_minor', op='==', val=True)]
response = self.search('/api/person', filters)
document = loads(response.data)
people = document['data']
self.assertEqual(len(people), 1)
person = people[0]
self.assertEqual(person['id'], '1')


class TestSimpleFiltering(ManagerTestBase):
"""Unit tests for "simple" filter query parameters.
Expand Down

0 comments on commit 9a25a94

Please sign in to comment.