Author: phd
Date: 2007-12-26 05:31:08 -0700 (Wed, 26 Dec 2007)
New Revision: 3184
Added:
SQLObject/trunk/sqlobject/tests/test_comparison.py
Modified:
SQLObject/trunk/docs/News.txt
SQLObject/trunk/sqlobject/main.py
Log:
Added rich comparison methods.
Modified: SQLObject/trunk/docs/News.txt
===================================================================
--- SQLObject/trunk/docs/News.txt 2007-12-20 00:16:20 UTC (rev 3183)
+++ SQLObject/trunk/docs/News.txt 2007-12-26 12:31:08 UTC (rev 3184)
@@ -56,6 +56,10 @@
* Changed the implementation type in BoolCol under SQLite from TINYINT to
BOOLEAN and made fromDatabase machinery to recognize it.
+* Added rich comparison methods; SQLObjects of the same class are
+ considered equal is they have the same id; other methods return
+ NotImplemented.
+
* A number of changes ported from `SQLObject 0.7.10`_.
SQLObject 0.9.3
Modified: SQLObject/trunk/sqlobject/main.py
===================================================================
--- SQLObject/trunk/sqlobject/main.py 2007-12-20 00:16:20 UTC (rev 3183)
+++ SQLObject/trunk/sqlobject/main.py 2007-12-26 12:31:08 UTC (rev 3184)
@@ -1581,15 +1581,37 @@
if isinstance(value, basestring):
value = dbconnection.connectionForURI(value)
cls._connection = value
+
setConnection = classmethod(setConnection)
def __sqlrepr__(self, db):
return sqlbuilder.sqlrepr(self.__class__.q.id==self.id)
-
+
def tablesUsedImmediate(self):
return [self.__class__.q]
+ def __eq__(self, other):
+ if self.__class__ is other.__class__:
+ if self.id == other.id:
+ return True
+ return False
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __lt__(self, other):
+ return NotImplemented
+
+ def __le__(self, other):
+ return NotImplemented
+
+ def __gt__(self, other):
+ return NotImplemented
+
+ def __ge__(self, other):
+ return NotImplemented
+
+
def capitalize(name):
return name[0].capitalize() + name[1:]
Added: SQLObject/trunk/sqlobject/tests/test_comparison.py
===================================================================
--- SQLObject/trunk/sqlobject/tests/test_comparison.py (rev 0)
+++ SQLObject/trunk/sqlobject/tests/test_comparison.py 2007-12-26 12:31:08 UTC (rev 3184)
@@ -0,0 +1,24 @@
+from sqlobject import *
+from sqlobject.tests.dbtest import *
+
+class TestComparison(SQLObject):
+ pass
+
+def test_eq():
+ setupClass(TestComparison)
+ t1 = TestComparison()
+ t2 = TestComparison()
+
+ TestComparison._connection.cache.clear()
+ t3 = TestComparison.get(1)
+ t4 = TestComparison.get(2)
+
+ assert t1.id == t3.id
+ assert t2.id == t4.id
+ assert t1 is not t3
+ assert t2 is not t4
+ assert t1 == t3
+ assert t2 == t4
+ assert t1 <> t2
+ assert t1 < t2
+ assert not (t1 >= t2)
|