[CalendarServer-changes] [8112] CalendarServer/trunk/twext/enterprise/dal
source_changes at macosforge.org
source_changes at macosforge.org
Wed Sep 21 19:26:30 PDT 2011
Revision: 8112
http://trac.macosforge.org/projects/calendarserver/changeset/8112
Author: glyph at apple.com
Date: 2011-09-21 19:26:28 -0700 (Wed, 21 Sep 2011)
Log Message:
-----------
deal with manpiulation of column syntaxes as dictionary keys
Modified Paths:
--------------
CalendarServer/trunk/twext/enterprise/dal/syntax.py
CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py
Modified: CalendarServer/trunk/twext/enterprise/dal/syntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/syntax.py 2011-09-21 08:02:43 UTC (rev 8111)
+++ CalendarServer/trunk/twext/enterprise/dal/syntax.py 2011-09-22 02:26:28 UTC (rev 8112)
@@ -20,6 +20,7 @@
"""
from itertools import count, repeat
+from operator import eq, ne
from zope.interface import implements
@@ -565,6 +566,7 @@
]
+
class ColumnSyntax(ExpressionSyntax):
"""
Syntactic convenience for L{Column}.
@@ -592,7 +594,11 @@
return SQLFragment(name)
+ def __hash__(self):
+ return hash(self.model) + 10
+
+
class Comparison(ExpressionSyntax):
def __init__(self, a, op, b):
@@ -668,14 +674,23 @@
+_operators = {"=": eq, "!=": ne}
+
class ColumnComparison(CompoundComparison):
"""
Comparing two columns is the same as comparing any other two expressions,
- (for now).
+ except that Python can retrieve a truth value, so that columns may be
+ compared for value equality in scripts that want to interrogate schemas.
"""
+ def __nonzero__(self):
+ thunk = _operators.get(self.op)
+ if thunk is None:
+ return super(ColumnComparison, self).__nonzero__()
+ return thunk(self.a.model, self.b.model)
+
class _AllColumns(object):
def subSQL(self, metadata, allTables):
Modified: CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py
===================================================================
--- CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py 2011-09-21 08:02:43 UTC (rev 8111)
+++ CalendarServer/trunk/twext/enterprise/dal/test/test_sqlsyntax.py 2011-09-22 02:26:28 UTC (rev 8112)
@@ -134,12 +134,15 @@
def test_comparisonTestErrorPrevention(self):
"""
- The comparison object between columns raises an exception when compared
- for a truth value, so that code will not accidentally test for '==' and
- always get a true value.
+ The comparison object between SQL expressions raises an exception when
+ compared for a truth value, so that code will not accidentally operate
+ on SQL objects and get a truth value.
+
+ (Note that this has a caveat, in test_columnsAsDictKeys and
+ test_columnEqualityTruth.)
"""
def sampleComparison():
- if self.schema.FOO.BAR == self.schema.FOO.BAZ:
+ if self.schema.FOO.BAR > self.schema.FOO.BAZ:
return 'comparison should not succeed'
self.assertRaises(ValueError, sampleComparison)
@@ -1093,3 +1096,28 @@
)
+ def test_columnEqualityTruth(self):
+ """
+ Mostly in support of test_columnsAsDictKeys, the 'same' column should
+ compare True to itself and False to other values.
+ """
+ s = self.schema
+ self.assertEquals(bool(s.FOO.BAR == s.FOO.BAR), True)
+ self.assertEquals(bool(s.FOO.BAR != s.FOO.BAR), False)
+ self.assertEquals(bool(s.FOO.BAZ != s.FOO.BAR), True)
+
+
+ def test_columnsAsDictKeys(self):
+ """
+ An odd corner of the syntactic sugar provided by the DAL is that the
+ column objects have to participate both in augmented equality comparison
+ ("==" returns an expression object) as well as dictionary keys (for
+ Insert and Update statement objects). Therefore it should be possible
+ to I{manipulate} dictionaries of keys as well.
+ """
+ values = {self.schema.FOO.BAR: 1}
+ self.assertEquals(values, {self.schema.FOO.BAR: 1})
+ values.pop(self.schema.FOO.BAR)
+ self.assertEquals(values, {})
+
+
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20110921/5eda90f8/attachment.html>
More information about the calendarserver-changes
mailing list