Author: phd
Date: 2008-05-19 08:58:03 -0600 (Mon, 19 May 2008)
New Revision: 3434
Added:
SQLObject/branches/0.10/sqlobject/tests/test_aggregates.py
Removed:
SQLObject/branches/0.10/sqlobject/tests/test_minmax.py
Modified:
SQLObject/branches/0.10/docs/News.txt
SQLObject/branches/0.10/sqlobject/sresults.py
Log:
Aggregate functions now honors 'distinct'.
Modified: SQLObject/branches/0.10/docs/News.txt
===================================================================
--- SQLObject/branches/0.10/docs/News.txt 2008-05-19 14:53:49 UTC (rev 3433)
+++ SQLObject/branches/0.10/docs/News.txt 2008-05-19 14:58:03 UTC (rev 3434)
@@ -7,6 +7,11 @@
.. _start:
+SQLObject 0.10.2
+================
+
+* A number of changes ported from `SQLObject 0.9.7`_.
+
SQLObject 0.10.1
================
@@ -86,6 +91,11 @@
* Under MySQL, PickleCol no longer uses TEXT column types; the smallest
column is now BLOB - it is not possible to create TINYBLOB column.
+SQLObject 0.9.7
+===============
+
+* Aggregate functions now honors 'distinct'.
+
SQLObject 0.9.6
===============
Modified: SQLObject/branches/0.10/sqlobject/sresults.py
===================================================================
--- SQLObject/branches/0.10/sqlobject/sresults.py 2008-05-19 14:53:49 UTC (rev 3433)
+++ SQLObject/branches/0.10/sqlobject/sresults.py 2008-05-19 14:58:03 UTC (rev 3434)
@@ -229,11 +229,15 @@
or a dot-q attribute (like Table.q.aColumn)
"""
expressions = []
+ conn = self._getConnection()
+ if self.ops.get('distinct'):
+ distinct = 'DISTINCT '
+ else:
+ distinct = ''
for func_name, attribute in attributes:
- if isinstance(attribute, str):
- expression = '%s(%s)' % (func_name, attribute)
- else:
- expression = getattr(sqlbuilder.func, func_name)(attribute)
+ if not isinstance(attribute, str):
+ attribute = conn.sqlrepr(attribute)
+ expression = '%s(%s%s)' % (func_name, distinct, attribute)
expressions.append(expression)
return self.accumulate(*expressions)
Copied: SQLObject/branches/0.10/sqlobject/tests/test_aggregates.py (from rev 3433, SQLObject/branches/0.9/sqlobject/tests/test_aggregates.py)
===================================================================
--- SQLObject/branches/0.10/sqlobject/tests/test_aggregates.py (rev 0)
+++ SQLObject/branches/0.10/sqlobject/tests/test_aggregates.py 2008-05-19 14:58:03 UTC (rev 3434)
@@ -0,0 +1,67 @@
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+
+# Test MIN, AVG, MAX, COUNT, SUM
+
+
+class IntAccumulator(SQLObject):
+ value = IntCol()
+
+
+class FloatAccumulator(SQLObject):
+ value = FloatCol()
+
+
+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
+
+
+def test_many():
+ setupClass(IntAccumulator)
+ IntAccumulator(value=1)
+ IntAccumulator(value=1)
+ IntAccumulator(value=2)
+ IntAccumulator(value=2)
+ IntAccumulator(value=3)
+ IntAccumulator(value=3)
+
+ attribute = IntAccumulator.q.value
+ assert IntAccumulator.select().accumulateMany(
+ ("MIN", attribute), ("AVG", attribute), ("MAX", attribute),
+ ("COUNT", attribute), ("SUM", attribute)
+ ) == (1, 2, 3, 6, 12)
+
+ assert IntAccumulator.select(distinct=True).accumulateMany(
+ ("MIN", attribute), ("AVG", attribute), ("MAX", attribute),
+ ("COUNT", attribute), ("SUM", attribute)
+ ) == (1, 2, 3, 3, 6)
Deleted: SQLObject/branches/0.10/sqlobject/tests/test_minmax.py
===================================================================
--- SQLObject/branches/0.10/sqlobject/tests/test_minmax.py 2008-05-19 14:53:49 UTC (rev 3433)
+++ SQLObject/branches/0.10/sqlobject/tests/test_minmax.py 2008-05-19 14:58:03 UTC (rev 3434)
@@ -1,59 +0,0 @@
-from sqlobject import *
-from sqlobject.tests.dbtest import *
-
-# Test MIN, AVG, MAX, SUM
-
-
-class IntAccumulator(SQLObject):
- value = IntCol()
-
-
-class FloatAccumulator(SQLObject):
- value = FloatCol()
-
-
-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
-
-
-def test_many():
- setupClass(IntAccumulator)
- IntAccumulator(value=1)
- IntAccumulator(value=2)
- IntAccumulator(value=3)
-
- select = IntAccumulator.select()
- attribute = IntAccumulator.q.value
- assert select.accumulateMany(
- ("MIN", attribute), ("AVG", attribute), ("MAX", attribute)
- ) == (1, 2, 3)
|