Add int range canonicalization
This commit is contained in:
@@ -82,9 +82,14 @@ class IntRangeType(types.TypeDecorator, ScalarCoercible):
|
|||||||
|
|
||||||
def process_result_value(self, value, dialect):
|
def process_result_value(self, value, dialect):
|
||||||
if value:
|
if value:
|
||||||
return intervals.IntInterval(value)
|
return self.canonicalize_result_value(
|
||||||
|
intervals.IntInterval(value)
|
||||||
|
)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def canonicalize_result_value(self, value):
|
||||||
|
return intervals.canonicalize(value, True, True)
|
||||||
|
|
||||||
def _coerce(self, value):
|
def _coerce(self, value):
|
||||||
if value is not None:
|
if value is not None:
|
||||||
value = intervals.IntInterval(value)
|
value = intervals.IntInterval(value)
|
||||||
|
@@ -6,6 +6,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
pass
|
||||||
from tests import TestCase
|
from tests import TestCase
|
||||||
|
from infinity import inf
|
||||||
from sqlalchemy_utils import IntRangeType
|
from sqlalchemy_utils import IntRangeType
|
||||||
|
|
||||||
|
|
||||||
@@ -43,28 +44,28 @@ class NumberRangeTestCase(TestCase):
|
|||||||
|
|
||||||
def test_infinite_upper_bound(self):
|
def test_infinite_upper_bound(self):
|
||||||
building = self.Building(
|
building = self.Building(
|
||||||
persons_at_night=intervals.IntInterval(1, float('inf'))
|
persons_at_night=intervals.IntInterval([1, inf])
|
||||||
)
|
)
|
||||||
self.session.add(building)
|
self.session.add(building)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
|
|
||||||
building = self.session.query(self.Building).first()
|
building = self.session.query(self.Building).first()
|
||||||
assert building.persons_at_night.lower == 1
|
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):
|
def test_infinite_lower_bound(self):
|
||||||
building = self.Building(
|
building = self.Building(
|
||||||
persons_at_night=intervals.IntInterval(-float('inf'), 1)
|
persons_at_night=intervals.IntInterval([-inf, 1])
|
||||||
)
|
)
|
||||||
self.session.add(building)
|
self.session.add(building)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
building = self.session.query(self.Building).first()
|
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
|
assert building.persons_at_night.upper == 1
|
||||||
|
|
||||||
def test_nullify_number_range(self):
|
def test_nullify_number_range(self):
|
||||||
building = self.Building(
|
building = self.Building(
|
||||||
persons_at_night=intervals.IntInterval(1, 3)
|
persons_at_night=intervals.IntInterval([1, 3])
|
||||||
)
|
)
|
||||||
|
|
||||||
self.session.add(building)
|
self.session.add(building)
|
||||||
|
Reference in New Issue
Block a user