strings and unicodes are compared via == rather than 'is'.

TypeDecorator uses underlying impl for mutator/comparison functions by default
This commit is contained in:
Mike Bayer
2006-10-16 17:20:15 +00:00
parent 433ed0fbdb
commit 3bb71bdd2d
2 changed files with 26 additions and 2 deletions
+8 -1
View File
@@ -98,6 +98,12 @@ class TypeDecorator(AbstractType):
return instance
def get_dbapi_type(self, dbapi):
return self.impl.get_dbapi_type(dbapi)
def copy_value(self, value):
return self.impl.copy_value(value)
def compare_values(self, x, y):
return self.impl.compare_values(x,y)
def is_mutable(self):
return self.impl.is_mutable()
class MutableType(object):
"""a mixin that marks a Type as holding a mutable object"""
@@ -144,7 +150,6 @@ class NullTypeEngine(TypeEngine):
def convert_result_value(self, value, dialect):
return value
class String(TypeEngine):
def __new__(cls, *args, **kwargs):
if cls is not String or len(args) > 0 or kwargs.has_key('length'):
@@ -167,6 +172,8 @@ class String(TypeEngine):
return value.decode(dialect.encoding)
def get_dbapi_type(self, dbapi):
return dbapi.STRING
def compare_values(self, x, y):
return x == y
class Unicode(TypeDecorator):
impl = String
+18 -1
View File
@@ -239,7 +239,8 @@ class MutableTypesTest(UnitOfWorkTest):
metadata = BoundMetaData(testbase.db)
table = Table('mutabletest', metadata,
Column('id', Integer, primary_key=True),
Column('data', PickleType, nullable=False))
Column('data', PickleType),
Column('value', Unicode(30)))
table.create()
def tearDownAll(self):
table.drop()
@@ -279,6 +280,22 @@ class MutableTypesTest(UnitOfWorkTest):
print f2.data, f3.data
assert (f3.data.x, f3.data.y) == (4,19)
def testunicode(self):
"""test that two equivalent unicode values dont get flagged as changed.
apparently two equal unicode objects dont compare via "is" in all cases, so this
tests the compare_values() call on types.String and its usage via types.Unicode."""
class Foo(object):pass
mapper(Foo, table)
f1 = Foo()
f1.value = u'hi'
ctx.current.flush()
ctx.current.clear()
f1 = ctx.current.get(Foo, f1.id)
f1.value = u'hi'
def go():
ctx.current.flush()
self.assert_sql_count(db, go, 0)
class PKTest(UnitOfWorkTest):