Skip to content

Commit cd807c0

Browse files
committed
Validation: improving overlapping fields quality. Commit: graphql/graphql-js@4afb263#diff-325ff93247e6e817d761b030604b95c0L369
1 parent ab7071a commit cd807c0

File tree

5 files changed

+580
-192
lines changed

5 files changed

+580
-192
lines changed

graphql/pyutils/pair_set.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,44 @@ class PairSet(object):
22
__slots__ = '_data',
33

44
def __init__(self):
5-
self._data = set()
5+
self._data = {}
66

77
def __contains__(self, item):
8-
return item in self._data
8+
return self.has(item[0], item[1], item[2])
99

10-
def has(self, a, b):
11-
return (a, b) in self._data
10+
def __str__(self):
11+
return str(self._data)
1212

13-
def add(self, a, b):
14-
self._data.add((a, b))
15-
self._data.add((b, a))
13+
def __repr__(self):
14+
return str(self._data)
15+
16+
def has(self, a, b, are_mutually_exclusive):
17+
first = self._data.get(a)
18+
print(first)
19+
result = first and first.get(b)
20+
print(result)
21+
if result is None:
22+
return False
23+
24+
# are_mutually_exclusive being false is a superset of being true,
25+
# hence if we want to know if this PairSet "has" these two with no
26+
# exclusivity, we have to ensure it was added as such.
27+
if not are_mutually_exclusive:
28+
return not result
29+
30+
return True
31+
32+
def add(self, a, b, are_mutually_exclusive):
33+
_pair_set_add(self._data, a, b, are_mutually_exclusive)
34+
_pair_set_add(self._data, b, a, are_mutually_exclusive)
1635
return self
1736

18-
def remove(self, a, b):
19-
self._data.discard((a, b))
20-
self._data.discard((b, a))
37+
38+
def _pair_set_add(data, a, b, are_mutually_exclusive):
39+
sub_dict = data.get(a)
40+
41+
if not sub_dict:
42+
sub_dict = {}
43+
data[a] = sub_dict
44+
45+
sub_dict[b] = are_mutually_exclusive

graphql/pyutils/tests/test_pair_set.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,39 @@
33

44
def test_pair_set():
55
ps = PairSet()
6+
are_mutually_exclusive = True
67

7-
ps.add(1, 2)
8-
ps.add(2, 4)
8+
ps.add(1, 2, are_mutually_exclusive)
9+
ps.add(2, 4, are_mutually_exclusive)
910

10-
assert ps.has(1, 2)
11-
assert ps.has(2, 1)
12-
assert (1, 2) in ps
13-
assert (2, 1) in ps
14-
assert ps.has(4, 2)
15-
assert ps.has(2, 4)
11+
assert ps.has(1, 2, are_mutually_exclusive)
12+
assert ps.has(2, 1, are_mutually_exclusive)
13+
assert not ps.has(1, 2, not are_mutually_exclusive)
14+
assert not ps.has(2, 1, not are_mutually_exclusive)
1615

17-
assert not ps.has(2, 3)
18-
assert not ps.has(1, 3)
16+
assert (1, 2, are_mutually_exclusive) in ps
17+
assert (2, 1, are_mutually_exclusive) in ps
18+
assert (1, 2, (not are_mutually_exclusive)) not in ps
19+
assert (2, 1, (not are_mutually_exclusive)) not in ps
1920

20-
ps.remove(1, 2)
21-
assert not ps.has(1, 2)
22-
assert not ps.has(2, 1)
23-
assert (1, 2) not in ps
24-
assert (2, 1) not in ps
21+
assert ps.has(4, 2, are_mutually_exclusive)
22+
assert ps.has(2, 4, are_mutually_exclusive)
2523

26-
assert ps.has(4, 2)
27-
assert ps.has(2, 4)
24+
assert not ps.has(2, 3, are_mutually_exclusive)
25+
assert not ps.has(1, 3, are_mutually_exclusive)
26+
27+
assert ps.has(4, 2, are_mutually_exclusive)
28+
assert ps.has(2, 4, are_mutually_exclusive)
29+
30+
31+
def test_pair_set_not_mutually_exclusive():
32+
ps = PairSet()
33+
are_mutually_exclusive = False
34+
35+
ps.add(1, 2, are_mutually_exclusive)
36+
37+
assert ps.has(1, 2, are_mutually_exclusive)
38+
assert ps.has(2, 1, are_mutually_exclusive)
39+
40+
assert ps.has(1, 2, not are_mutually_exclusive)
41+
assert ps.has(2, 1, not are_mutually_exclusive)

0 commit comments

Comments
 (0)