Use TypeError for bad prepared stmt param types
This commit is contained in:
@@ -21,6 +21,9 @@ Other
|
||||
* Remove ignored ``tracing_enabled`` parameter for ``SimpleStatement``. The
|
||||
correct way to trace a query is by setting the ``trace`` argument to ``True``
|
||||
in ``Session.execute()`` and ``Session.execute_async()``.
|
||||
* Raise TypeError instead of cassandra.query.InvalidParameterTypeError when
|
||||
a parameter for a prepared statement has the wrong type; remove
|
||||
cassandra.query.InvalidParameterTypeError.
|
||||
|
||||
1.0.1
|
||||
=====
|
||||
|
@@ -238,10 +238,9 @@ class BoundStatement(Statement):
|
||||
expected_type = col_type
|
||||
actual_type = type(value)
|
||||
|
||||
err = InvalidParameterTypeError(col_name=col_name,
|
||||
expected_type=expected_type,
|
||||
actual_type=actual_type)
|
||||
raise err
|
||||
message = ('Received an argument of invalid type for column "%s". '
|
||||
'Expected: %s, Got: %s' % (col_name, expected_type, actual_type))
|
||||
raise TypeError(message)
|
||||
|
||||
return self
|
||||
|
||||
@@ -319,24 +318,6 @@ class TraceUnavailable(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class InvalidParameterTypeError(TypeError):
|
||||
"""
|
||||
Raised when a used tries to bind a prepared statement with an argument of an
|
||||
invalid type.
|
||||
"""
|
||||
|
||||
def __init__(self, col_name, expected_type, actual_type):
|
||||
self.col_name = col_name
|
||||
self.expected_type = expected_type
|
||||
self.actual_type = actual_type
|
||||
|
||||
values = (self.col_name, self.expected_type, self.actual_type)
|
||||
message = ('Received an argument of invalid type for column "%s". '
|
||||
'Expected: %s, Got: %s' % values)
|
||||
|
||||
super(InvalidParameterTypeError, self).__init__(message)
|
||||
|
||||
|
||||
class QueryTrace(object):
|
||||
"""
|
||||
A trace of the duration and events that occurred when executing
|
||||
|
@@ -5,7 +5,6 @@ except ImportError:
|
||||
|
||||
from cassandra.query import bind_params, ValueSequence
|
||||
from cassandra.query import PreparedStatement, BoundStatement
|
||||
from cassandra.query import InvalidParameterTypeError
|
||||
from cassandra.cqltypes import Int32Type
|
||||
|
||||
try:
|
||||
@@ -77,21 +76,12 @@ class BoundStatementTestCase(unittest.TestCase):
|
||||
|
||||
values = ['nonint', 1]
|
||||
|
||||
try:
|
||||
bound_statement.bind(values)
|
||||
except InvalidParameterTypeError as e:
|
||||
self.assertEqual(e.col_name, 'foo1')
|
||||
self.assertEqual(e.expected_type, Int32Type)
|
||||
self.assertEqual(e.actual_type, str)
|
||||
else:
|
||||
self.fail('Passed invalid type but exception was not thrown')
|
||||
|
||||
try:
|
||||
bound_statement.bind(values)
|
||||
except TypeError as e:
|
||||
self.assertEqual(e.col_name, 'foo1')
|
||||
self.assertEqual(e.expected_type, Int32Type)
|
||||
self.assertEqual(e.actual_type, str)
|
||||
self.assertIn('foo1', str(e))
|
||||
self.assertIn('Int32Type', str(e))
|
||||
self.assertIn('str', str(e))
|
||||
else:
|
||||
self.fail('Passed invalid type but exception was not thrown')
|
||||
|
||||
@@ -99,9 +89,9 @@ class BoundStatementTestCase(unittest.TestCase):
|
||||
|
||||
try:
|
||||
bound_statement.bind(values)
|
||||
except InvalidParameterTypeError as e:
|
||||
self.assertEqual(e.col_name, 'foo2')
|
||||
self.assertEqual(e.expected_type, Int32Type)
|
||||
self.assertEqual(e.actual_type, list)
|
||||
except TypeError as e:
|
||||
self.assertIn('foo2', str(e))
|
||||
self.assertIn('Int32Type', str(e))
|
||||
self.assertIn('list', str(e))
|
||||
else:
|
||||
self.fail('Passed invalid type but exception was not thrown')
|
||||
|
Reference in New Issue
Block a user