Add int range canonicalization

This commit is contained in:
Konsta Vesterinen
2014-01-13 20:33:57 +02:00
parent 3087f10502
commit 4753613116
2 changed files with 12 additions and 6 deletions

View File

@@ -82,9 +82,14 @@ class IntRangeType(types.TypeDecorator, ScalarCoercible):
def process_result_value(self, value, dialect):
if value:
return intervals.IntInterval(value)
return self.canonicalize_result_value(
intervals.IntInterval(value)
)
return value
def canonicalize_result_value(self, value):
return intervals.canonicalize(value, True, True)
def _coerce(self, value):
if value is not None:
value = intervals.IntInterval(value)

View File

@@ -6,6 +6,7 @@ try:
except ImportError:
pass
from tests import TestCase
from infinity import inf
from sqlalchemy_utils import IntRangeType
@@ -43,28 +44,28 @@ class NumberRangeTestCase(TestCase):
def test_infinite_upper_bound(self):
building = self.Building(
persons_at_night=intervals.IntInterval(1, float('inf'))
persons_at_night=intervals.IntInterval([1, inf])
)
self.session.add(building)
self.session.commit()
building = self.session.query(self.Building).first()
assert building.persons_at_night.lower == 1
assert building.persons_at_night.upper == float('inf')
assert building.persons_at_night.upper == inf
def test_infinite_lower_bound(self):
building = self.Building(
persons_at_night=intervals.IntInterval(-float('inf'), 1)
persons_at_night=intervals.IntInterval([-inf, 1])
)
self.session.add(building)
self.session.commit()
building = self.session.query(self.Building).first()
assert building.persons_at_night.lower == -float('inf')
assert building.persons_at_night.lower == -inf
assert building.persons_at_night.upper == 1
def test_nullify_number_range(self):
building = self.Building(
persons_at_night=intervals.IntInterval(1, 3)
persons_at_night=intervals.IntInterval([1, 3])
)
self.session.add(building)