def test_0130_intersect(self): self.assertIntervalOperationEquals(Interval.intersect, [ (((0, 0), (0, 0)), Interval(0, 0)), (((0, 2), (1, 3)), Interval(1, 2)), (((0, 1), (2, 3)), None), (((None, 2), (1, None)), Interval(1, 2)), (((1, 2), (None, None)), Interval(1, 2)), (((None, None), (None, None)), Interval(None, None)), ])
def deserialize_interval(value): try: lower_value = (deserialize_param(value['lower_value']) if value['lower_value'] is not None else NegativeInfinity) lower_bound = Bound(lower_value, value['lower_closed']) upper_value = (deserialize_param(value['upper_value']) if value['upper_value'] is not None else PositiveInfinity) upper_bound = Bound(upper_value, value['upper_closed']) except KeyError: raise ValueError() return Interval(lower_bound, upper_bound)
def assertIntervalOperationEquals(self, operation, args_and_expected): """ :param callable operation: :param list[iterable[Interval], unknown)] args_and_expected: """ for intervals, expected in args_and_expected: intervals = map(lambda args: Interval(*args), intervals) message = "{0}({1}) != {2}".format(operation.__name__, ', '.join(map(str, intervals)), expected) assert operation(*intervals) == expected, message
def process_result_value(self, value: DateTimeTZRange | None, dialect)\ -> Interval | None: if value is None: return None if not isinstance(value, DateTimeTZRange): # see https://github.com/sqlalchemy/sqlalchemy/discussions/6942 raise PycroftModelException( f"Unable to deserialize TsTzRange value {value!r} of type {type(value)}." " Usually, this value should've been deserialized by psycopg2 into a" " DatetimeTzRange. Did you make a mistake in your query?" " Note that you may have to use `cast(…, TsTzRange)` to let sqlalchemy know" " of the return type –– even if you specified the type in `literal()` already!" ) return Interval.from_explicit_data(value.lower, value.lower_inc, value.upper, value.upper_inc)
def _to_date_interval(interval): """ :param Interval[datetime] interval: :rtype: Interval[date] """ if interval.lower_bound.unbounded: lower_bound = interval.lower_bound else: lower_bound = Bound(interval.lower_bound.value.date(), interval.lower_bound.closed) if interval.upper_bound.unbounded: upper_bound = interval.upper_bound else: upper_bound = Bound(interval.upper_bound.value.date(), interval.upper_bound.closed) return Interval(lower_bound, upper_bound)
def test_before(one: Interval, other: Interval, is_before: bool): assert one.before(other) == is_before
def test_strictly_before(one: Interval, other: Interval, strictly_before: bool): assert one.strictly_before(other) == strictly_before
def test_during(one: Interval, other: Interval, is_during: bool): assert one.during(other) == is_during
def test_strictly_during(one: Interval, other: Interval, is_strictly_during: bool): assert one.strictly_during(other) == is_strictly_during
def test_begin_greater_than_end(): with pytest.raises(ValueError): Interval(1, 0)
def test_overlaps(one: Interval, other: Interval, overlaps: bool): assert one.overlaps(other) == overlaps