Fixed error in schema mismatch in composite caster

This commit is contained in:
Daniele Varrazzo 2011-12-15 19:23:08 +00:00
parent ba7a0a3008
commit bb8e1e9455
2 changed files with 12 additions and 2 deletions

View File

@ -841,8 +841,8 @@ class CompositeCaster(object):
tokens = self.tokenize(s)
if len(tokens) != len(self.atttypes):
raise psycopg2.DataError(
"expecting %d components for the type %s, %d found instead",
(len(self.atttypes), self.name, len(self.tokens)))
"expecting %d components for the type %s, %d found instead" %
(len(self.atttypes), self.name, len(tokens)))
attrs = [ curs.cast(oid, token)
for oid, token in zip(self.atttypes, tokens) ]

View File

@ -648,6 +648,16 @@ class AdaptTypeTestCase(unittest.TestCase):
self.assertEqual(v[1][1], "world")
self.assertEqual(v[1][2], date(2011,1,3))
@skip_if_no_composite
def test_wrong_schema(self):
oid = self._create_type("type_ii", [("a", "integer"), ("b", "integer")])
from psycopg2.extras import CompositeCaster
c = CompositeCaster('type_ii', oid, [('a', 23), ('b', 23), ('c', 23)])
curs = self.conn.cursor()
psycopg2.extensions.register_type(c.typecaster, curs)
curs.execute("select (1,2)::type_ii")
self.assertRaises(psycopg2.DataError, curs.fetchone)
def _create_type(self, name, fields):
curs = self.conn.cursor()
try: