[SQL-CVS] r650 - in trunk/SQLObject/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <sub...@co...> - 2005-02-25 17:50:43
|
Author: phd Date: 2005-02-25 17:50:40 +0000 (Fri, 25 Feb 2005) New Revision: 650 Added: trunk/SQLObject/sqlobject/tests/test_minmax.py Modified: trunk/SQLObject/sqlobject/sresults.py Log: Added min() and max() - patch by michelts <mic...@gm...>. Added avg() and tests. Modified: trunk/SQLObject/sqlobject/sresults.py =================================================================== --- trunk/SQLObject/sqlobject/sresults.py 2005-02-25 15:37:35 UTC (rev 649) +++ trunk/SQLObject/sqlobject/sresults.py 2005-02-25 17:50:40 UTC (rev 650) @@ -158,4 +158,37 @@ expression = sqlbuilder.func.SUM(attribute) return self.accumulate(expression) + def min(self, attribute): + """ Giving the min of a given select result attribute. + `attribute` can be a column name (like 'a_column') + or a dot-q attribute (like Table.q.aColumn) + """ + if type(attribute) == type(''): + expression = 'MIN(%s)' % attribute + else: + expression = sqlbuilder.func.MIN(attribute) + return self.accumulate(expression) + + def avg(self, attribute): + """ Giving the average of a given select result attribute. + `attribute` can be a column name (like 'a_column') + or a dot-q attribute (like Table.q.aColumn) + """ + if type(attribute) == type(''): + expression = 'AVG(%s)' % attribute + else: + expression = sqlbuilder.func.AVG(attribute) + return self.accumulate(expression) + + def max(self, attribute): + """ Giving the max of a given select result attribute. + `attribute` can be a column name (like 'a_column') + or a dot-q attribute (like Table.q.aColumn) + """ + if type(attribute) == type(''): + expression = 'MAX(%s)' % attribute + else: + expression = sqlbuilder.func.MAX(attribute) + return self.accumulate(expression) + __all__ = ['SelectResults'] Added: trunk/SQLObject/sqlobject/tests/test_minmax.py =================================================================== --- trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-02-25 15:37:35 UTC (rev 649) +++ trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-02-25 17:50:40 UTC (rev 650) @@ -0,0 +1,21 @@ +from sqlobject import * +from sqlobject.tests.dbtest import * + +# Test MIN, AVG, MAX, SUM + +class Accumulator(SQLObject): + value = IntCol() + +def test_minmax(): + setupClass(Accumulator) + Accumulator(value=1) + Accumulator(value=2) + Accumulator(value=3) + + assert Accumulator.select().min(Accumulator.q.value) == 1 + assert Accumulator.select().avg(Accumulator.q.value) == 2 + assert Accumulator.select().max(Accumulator.q.value) == 3 + assert Accumulator.select().sum(Accumulator.q.value) == 6 + + assert Accumulator.select(Accumulator.q.value > 1).max(Accumulator.q.value) == 3 + assert Accumulator.select(Accumulator.q.value > 1).sum(Accumulator.q.value) == 5 |