Add count() support to find_streams

Returns [{'count': num_streams}]

Change-Id: I37e7fc462a14dda793ac5c231439ed7d5e5ee523
This commit is contained in:
Sandy Walsh 2015-01-27 10:45:39 -08:00
parent c5f0eca828
commit 460f8231ee
2 changed files with 10 additions and 2 deletions

View File

@ -446,4 +446,6 @@ class TestDB(unittest.TestCase):
with self.assertRaises(db.NoSuchStreamError):
self.db.get_stream_by_id(1)
def test_find_stream_count(self):
count = self.db.find_streams(count=True)
self.assertEqual([{'count': 8}], count)

View File

@ -235,7 +235,8 @@ class DBInterface(object):
return stream
@sessioned
def find_streams(self, stream_id=None, state=None, older_than=None, younger_than=None,
def find_streams(self, count=False, stream_id=None, state=None,
older_than=None, younger_than=None,
name=None, distinguishing_traits=None,
session=None, include_events=False):
q = session.query(models.Stream)
@ -254,6 +255,11 @@ class DBInterface(object):
q = q.filter(models.Stream.distinguished_by.any(and_(
models.DistinguishingTrait.name == name,
models.DistinguishingTrait.value == val)))
if count:
q = q.count()
return [{"count": q}]
stream_info = []
for stream in q.all():
info = stream.as_dict