[SQL-CVS] r652 - in trunk/SQLObject/sqlobject: . tests
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
|
From: <sub...@co...> - 2005-02-27 19:32:13
|
Author: phd
Date: 2005-02-27 19:32:06 +0000 (Sun, 27 Feb 2005)
New Revision: 652
Modified:
trunk/SQLObject/sqlobject/dbconnection.py
trunk/SQLObject/sqlobject/tests/test_minmax.py
Log:
Some statistical functions (MIN, AVG, MAX) may return fload result.
Added tests for float functions.
Modified: trunk/SQLObject/sqlobject/dbconnection.py
===================================================================
--- trunk/SQLObject/sqlobject/dbconnection.py 2005-02-26 13:09:20 UTC (rev 651)
+++ trunk/SQLObject/sqlobject/dbconnection.py 2005-02-27 19:32:06 UTC (rev 652)
@@ -265,15 +265,13 @@
select, keepConnection=False)
def accumulateSelect(self, select, expression):
- """ Apply an accumulate function (like SUM, COUNT, ..)
+ """ Apply an accumulate function (SUM, COUNT, MIN, AVG, MAX, etc...)
to the select object.
- Return the value resulting from the SQL accumulate function
- as an integer.
"""
q = "SELECT %s" % expression
q += " FROM %s WHERE" % ", ".join(select.tables)
q = self._addWhereClause(select, q, limit=0, order=0)
- val = int(self.queryOne(q)[0])
+ val = self.queryOne(q)[0]
return val
def queryForSelect(self, select):
Modified: trunk/SQLObject/sqlobject/tests/test_minmax.py
===================================================================
--- trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-02-26 13:09:20 UTC (rev 651)
+++ trunk/SQLObject/sqlobject/tests/test_minmax.py 2005-02-27 19:32:06 UTC (rev 652)
@@ -3,19 +3,44 @@
# Test MIN, AVG, MAX, SUM
-class Accumulator(SQLObject):
+
+class IntAccumulator(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
+class FloatAccumulator(SQLObject):
+ value = FloatCol()
- assert Accumulator.select(Accumulator.q.value > 1).max(Accumulator.q.value) == 3
- assert Accumulator.select(Accumulator.q.value > 1).sum(Accumulator.q.value) == 5
+
+def test_integer():
+ setupClass(IntAccumulator)
+ IntAccumulator(value=1)
+ IntAccumulator(value=2)
+ IntAccumulator(value=3)
+
+ assert IntAccumulator.select().min(IntAccumulator.q.value) == 1
+ assert IntAccumulator.select().avg(IntAccumulator.q.value) == 2
+ assert IntAccumulator.select().max(IntAccumulator.q.value) == 3
+ assert IntAccumulator.select().sum(IntAccumulator.q.value) == 6
+
+ assert IntAccumulator.select(IntAccumulator.q.value > 1).max(IntAccumulator.q.value) == 3
+ assert IntAccumulator.select(IntAccumulator.q.value > 1).sum(IntAccumulator.q.value) == 5
+
+
+def floatcmp(f1, f2):
+ if abs(f1-f2) < 0.1:
+ return 0
+ if f1 < f2:
+ return 1
+ return -1
+
+def test_float():
+ setupClass(FloatAccumulator)
+ FloatAccumulator(value=1.2)
+ FloatAccumulator(value=2.4)
+ FloatAccumulator(value=3.8)
+
+ assert floatcmp(FloatAccumulator.select().min(FloatAccumulator.q.value), 1.2) == 0
+ assert floatcmp(FloatAccumulator.select().avg(FloatAccumulator.q.value), 2.5) == 0
+ assert floatcmp(FloatAccumulator.select().max(FloatAccumulator.q.value), 3.8) == 0
+ assert floatcmp(FloatAccumulator.select().sum(FloatAccumulator.q.value), 7.4) == 0
|