Add support for contains and contained_by operators

This commit is contained in:
Konsta Vesterinen
2014-01-14 17:39:32 +02:00
parent 3c7d25d6f6
commit ebc5de98fb
2 changed files with 50 additions and 5 deletions

View File

@@ -59,7 +59,11 @@ Membership operators
:: ::
Car.price_range.in_([[300, 500]]) Car.price_range.contains([300, 500])
Car.price_range.contained_by([300, 500])
Car.price_range.in_([[300, 500], [800, 900]])
~ Car.price_range.in_([[300, 400], [700, 800]]) ~ Car.price_range.in_([[300, 400], [700, 800]])
@@ -158,6 +162,13 @@ class RangeComparator(types.TypeEngine.Comparator):
other = map(self.coerce_arg, other) other = map(self.coerce_arg, other)
return super(RangeComparator, self).notin_(other) return super(RangeComparator, self).notin_(other)
def contains(self, other, **kwargs):
other = self.coerce_arg(other)
return self.op('@>')(other)
def contained_by(self, other, **kwargs):
other = self.coerce_arg(other)
return self.op('<@')(other)
funcs = [ funcs = [

View File

@@ -101,16 +101,50 @@ class TestIntRangeTypeOnPostgres(NumberRangeTestCase):
@mark.parametrize( @mark.parametrize(
'number_range', 'number_range',
( (
[1, 3], [[1, 3]],
'1 - 3', ['1 - 3'],
(0, 4) [(0, 4)],
) )
) )
def test_in_operator(self, number_range): def test_in_operator(self, number_range):
self.create_building([1, 3]) self.create_building([1, 3])
query = ( query = (
self.session.query(self.Building) self.session.query(self.Building)
.filter(self.Building.persons_at_night.in_([number_range])) .filter(self.Building.persons_at_night.in_(number_range))
)
assert query.count()
@mark.parametrize(
'number_range',
(
[1, 3],
'1 - 3',
(1, 3),
2
)
)
def test_contains_operator(self, number_range):
self.create_building([1, 3])
query = (
self.session.query(self.Building)
.filter(self.Building.persons_at_night.contains(number_range))
)
assert query.count()
@mark.parametrize(
'number_range',
(
[1, 3],
'1 - 3',
(0, 8),
(-inf, inf)
)
)
def test_contained_by_operator(self, number_range):
self.create_building([1, 3])
query = (
self.session.query(self.Building)
.filter(self.Building.persons_at_night.contained_by(number_range))
) )
assert query.count() assert query.count()