more useful error message when comparing ranges with non-ranges

This commit is contained in:
Chris Withers 2013-05-26 21:20:40 +01:00
parent bbe0bf955c
commit 8bb44f3444
2 changed files with 12 additions and 0 deletions

View File

@ -121,6 +121,8 @@ class Range(object):
return self._bounds is not None
def __eq__(self, other):
if not isinstance(other, self.__class__):
raise TypeError('Cannot compare %r with %r' % (self, other))
return (self._lower == other._lower
and self._upper == other._upper
and self._bounds == other._bounds)

View File

@ -1212,6 +1212,16 @@ class RangeTestCase(unittest.TestCase):
assert_not_equal(Range(10, 20), Range(11, 20))
assert_not_equal(Range(10, 20, '[)'), Range(10, 20, '[]'))
def test_eq_wrong_type(self):
from psycopg2.extras import Range
try:
Range(10, 20)==()
except TypeError, e:
self.assertEqual(
str(e), "Cannot compare Range(10, 20, '[)') with ()")
else:
self.fail('No exception raised')
def test_not_ordered(self):
from psycopg2.extras import Range
self.assertRaises(TypeError, lambda: Range(empty=True) < Range(0,4))