Mercurial > hg > ltpdarepo
changeset 74:b8c1ce741745
Rework Objs listing internal API. Add unit test.
author | Daniele Nicolodi <daniele@grinta.net> |
---|---|
date | Mon, 15 Aug 2011 20:06:27 +0200 |
parents | 3d5850d65603 |
children | f9d554de3793 |
files | src/ltpdarepo/tests/test_objs.py src/ltpdarepo/views/browse.py |
diffstat | 2 files changed, 63 insertions(+), 32 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ltpdarepo/tests/test_objs.py Mon Aug 15 20:06:27 2011 +0200 @@ -0,0 +1,36 @@ +import unittest2 as unittest + +from ltpdarepo.tests.utils import RequestContextTestCase +from ltpdarepo.views.browse import Objs + +class TestCase(RequestContextTestCase): + + @classmethod + def setUpClass(self): + from ltpdarepo.admin import wipe, setup + wipe() + setup() + + def test_simple(self): + objs = Objs(database='db1') + self.assertEqual(len(objs.all()), objs.count()) + + def test_limit(self): + objs = Objs(database='db1').limit(20) + self.assertEqual(len(objs.all()), 20) + + objs = Objs(database='db1').limit(20, 2) + self.assertEqual(objs.all()[0]['id'], 21) + self.assertEqual(len(objs.all()), 2) + + def test_orderby(self): + objs = Objs(database='db1').orderby('obj_id') + self.assertEqual(objs[0]['id'], 1) + + objs = Objs(database='db1').orderby('obj_id', desc=True) + self.assertEqual(objs[0]['id'], 30) + + def test_where(self): + objs = Objs(database='db1').filter('obj_id > %s AND obj_id < %s', 10, 20) + self.assertEqual(objs[0]['id'], 11) +
--- a/src/ltpdarepo/views/browse.py Mon Aug 15 20:06:25 2011 +0200 +++ b/src/ltpdarepo/views/browse.py Mon Aug 15 20:06:27 2011 +0200 @@ -102,32 +102,23 @@ query += " LIMIT %d,%d" % self._limit return query - def filter(self, where, values): + def filter(self, where, *values): self._where = where self._values = values return self - def orderby(self, order): - if isinstance(order, tuple): - self._orderby, self._reverse = order - else: - self._orderby = order + def orderby(self, order, desc=False): + self._orderby = order + self._reverse = desc return self - def __getitem__(self, item): - if isinstance(item, slice): - start, stop, step = item.indices(sys.maxint) - count = stop - start - self._limit = (start, count) - return self.__call__() - else: - start, count = item, 1 - self._limit = (start, count) - objs = self.__call__() - if objs is not None: - return objs[0] - - def __call__(self): + def limit(self, start, count=None): + if count is None: + return self.limit(0, start) + self._limit = (start, count) + return self + + def all(self): curs = g.db.cursor(DictCursor) curs.execute(self._query, self._values) return curs.fetchall() @@ -140,6 +131,12 @@ def __len__(self): return self.count() + def __getitem__(self, item): + self._limit = (item, 1) + objs = self.all() + if objs is not None: + return objs[0] + app = Blueprint('browse', __name__) @@ -169,7 +166,7 @@ count = Objs(database=database).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).orderby(_current_ordering())[batch.slice] + objs = Objs(database=database).orderby(*_current_ordering()).limit(*batch.limits).all() return render_template('browse.html', objs=objs, fields=FIELDS, database=db, batch=batch) @@ -268,12 +265,12 @@ # search criteria q = request.args.get('q', '') - q = ('%%%s%%' % q) + q = '%%%s%%' % q # query count = Objs(database=database).filter('name LIKE %s', q).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).filter('name LIKE %s', q).orderby(_current_ordering())[batch.slice] + objs = Objs(database=database).filter('name LIKE %s', q).orderby(*_current_ordering()).limit(*batch.limits).all() return render_template('browse.html', objs=objs, fields=FIELDS, database=db, batch=batch) @@ -332,9 +329,9 @@ where = ' AND '.join(q) # get objects - count = Objs(database=database).filter(where, values).count() + count = Objs(database=database).filter(where, *values).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).filter(where, values).orderby(_current_ordering())[batch.slice] + objs = Objs(database=database).filter(where, *values).orderby(*_current_ordering()).limit(*batch.limits).all() # collect search critaeria criteria = _indexes(database, 'objmeta') @@ -369,9 +366,9 @@ querystring = ' AND '.join("%s %s %s" % tuple(v) for v in query) # get objects - count = Objs(database=database).filter(where, values).count() + count = Objs(database=database).filter(where, *values).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Objs(database=database).filter(where, values).orderby(_current_ordering())[batch.slice] + objs = Objs(database=database).filter(where, *values).orderby(*_current_ordering()).limit(*batch.limits).all() return render_template('namedquery.html', objs=objs, database=db, fields=FIELDS, batch=batch, query=querystring) @@ -404,7 +401,6 @@ query += " DESC" if self._limit: query += " LIMIT %d,%d" % self._limit - print query return query @property @@ -419,11 +415,10 @@ query += " AND %s" % self._where if self._limit: query += " LIMIT %d,%d" % self._limit - print query return query -@app.route('/<database>/timeseries/') +@app.route('/<database>/timeseries') @require('user') def timeseries(database): db = Database().load(database) @@ -444,9 +439,9 @@ end = request.args.get('to', None) or None # get objects - count = Timeseries(database=database).filter(where, values).timespan(start, end).count() + count = Timeseries(database=database).filter(where, *values).timespan(start, end).count() batch = Pagination(_current_page(), size=PAGESIZE, count=count) - objs = Timeseries(database=database).filter(where, values).orderby(_current_ordering()).timespan(start, end)[batch.slice] + objs = Timeseries(database=database).filter(where, *values).orderby(*_current_ordering()).timespan(start, end).limit(*batch.limits).all() # collect search critaeria criteria = _indexes(database, 'objmeta')