Skip to content

Commit de07d0a

Browse files
authored
Add issubset and issuperset methods to the Range type (#563)
1 parent d076169 commit de07d0a

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

asyncpg/types.py

+33
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ def upper_inf(self):
8585
def isempty(self):
8686
return self._empty
8787

88+
def _issubset_lower(self, other):
89+
if other._lower is None:
90+
return True
91+
if self._lower is None:
92+
return False
93+
94+
return self._lower > other._lower or (
95+
self._lower == other._lower
96+
and (other._lower_inc or not self._lower_inc)
97+
)
98+
99+
def _issubset_upper(self, other):
100+
if other._upper is None:
101+
return True
102+
if self._upper is None:
103+
return False
104+
105+
return self._upper < other._upper or (
106+
self._upper == other._upper
107+
and (other._upper_inc or not self._upper_inc)
108+
)
109+
110+
def issubset(self, other):
111+
if self._empty:
112+
return True
113+
if other._empty:
114+
return False
115+
116+
return self._issubset_lower(other) and self._issubset_upper(other)
117+
118+
def issuperset(self, other):
119+
return other.issubset(self)
120+
88121
def __bool__(self):
89122
return not self._empty
90123

tests/test_types.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright (C) 2016-present the ayncpg authors and contributors
2+
# <see AUTHORS file>
3+
#
4+
# This module is part of asyncpg and is released under
5+
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
6+
7+
from itertools import product
8+
9+
from asyncpg.types import Range
10+
from asyncpg import _testbase as tb
11+
12+
13+
class TestTypes(tb.TestCase):
14+
15+
def test_range_issubset(self):
16+
subs = [
17+
Range(empty=True),
18+
Range(lower=1, upper=5, lower_inc=True, upper_inc=False),
19+
Range(lower=1, upper=5, lower_inc=True, upper_inc=True),
20+
Range(lower=1, upper=5, lower_inc=False, upper_inc=True),
21+
Range(lower=1, upper=5, lower_inc=False, upper_inc=False),
22+
Range(lower=-5, upper=10),
23+
Range(lower=2, upper=3),
24+
Range(lower=1, upper=None),
25+
Range(lower=None, upper=None)
26+
]
27+
28+
sups = [
29+
Range(empty=True),
30+
Range(lower=1, upper=5, lower_inc=True, upper_inc=False),
31+
Range(lower=1, upper=5, lower_inc=True, upper_inc=True),
32+
Range(lower=1, upper=5, lower_inc=False, upper_inc=True),
33+
Range(lower=1, upper=5, lower_inc=False, upper_inc=False),
34+
Range(lower=None, upper=None)
35+
]
36+
37+
# Each row is 1 subs with all sups
38+
results = [
39+
True, True, True, True, True, True,
40+
False, True, True, False, False, True,
41+
False, False, True, False, False, True,
42+
False, False, True, True, False, True,
43+
False, True, True, True, True, True,
44+
False, False, False, False, False, True,
45+
False, True, True, True, True, True,
46+
False, False, False, False, False, True,
47+
False, False, False, False, False, True
48+
]
49+
50+
for (sub, sup), res in zip(product(subs, sups), results):
51+
self.assertIs(
52+
sub.issubset(sup), res, "Sub:{}, Sup:{}".format(sub, sup)
53+
)
54+
self.assertIs(
55+
sup.issuperset(sub), res, "Sub:{}, Sup:{}".format(sub, sup)
56+
)

0 commit comments

Comments
 (0)