From 460f8231ee67d41d10fdb6cf1a634175cc9c09c1 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 27 Jan 2015 10:45:39 -0800 Subject: [PATCH] Add count() support to find_streams Returns [{'count': num_streams}] Change-Id: I37e7fc462a14dda793ac5c231439ed7d5e5ee523 --- tests/test_db.py | 4 +++- winchester/db/interface.py | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_db.py b/tests/test_db.py index f838422..78f5d70 100644 --- a/tests/test_db.py +++ b/tests/test_db.py @@ -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) diff --git a/winchester/db/interface.py b/winchester/db/interface.py index bfa028b..b94e21b 100644 --- a/winchester/db/interface.py +++ b/winchester/db/interface.py @@ -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