Skip to content

Commit 0c83799

Browse files
committed
Move PairSet/DefaultOrderedDict to graphql.core.pyutils.
* Implement unit tests for PairSet.
1 parent a88c628 commit 0c83799

File tree

5 files changed

+65
-21
lines changed

5 files changed

+65
-21
lines changed

graphql/core/pyutils/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class PairSet(object):
2+
def __init__(self):
3+
self._data = set()
4+
5+
def __contains__(self, item):
6+
return item in self._data
7+
8+
def has(self, a, b):
9+
return (a, b) in self._data
10+
11+
def add(self, a, b):
12+
self._data.add((a, b))
13+
self._data.add((b, a))
14+
return self
15+
16+
def remove(self, a, b):
17+
self._data.discard((a, b))
18+
self._data.discard((b, a))

graphql/core/validation/rules/utils.py renamed to graphql/core/pyutils/default_ordered_dict.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
from collections import Callable, OrderedDict
22

33

4-
class PairSet(object):
5-
def __init__(self):
6-
self._data = set()
7-
8-
def __contains__(self, item):
9-
return item in self._data
10-
11-
def has(self, a, b):
12-
return (a, b) in self._data
13-
14-
def add(self, a, b):
15-
self._data.add((a, b))
16-
self._data.add((b, a))
17-
return self
18-
19-
def remove(self, a, b):
20-
self._data.discard((a, b))
21-
self._data.discard((b, a))
22-
23-
244
class DefaultOrderedDict(OrderedDict):
255
# Source: http://stackoverflow.com/a/6190500/562769
266
def __init__(self, default_factory=None, *a, **kw):

graphql/core/pyutils/pair_set.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class PairSet(object):
2+
def __init__(self):
3+
self._data = set()
4+
5+
def __contains__(self, item):
6+
return item in self._data
7+
8+
def has(self, a, b):
9+
return (a, b) in self._data
10+
11+
def add(self, a, b):
12+
self._data.add((a, b))
13+
self._data.add((b, a))
14+
return self
15+
16+
def remove(self, a, b):
17+
self._data.discard((a, b))
18+
self._data.discard((b, a))

graphql/core/validation/rules/overlapping_fields_can_be_merged.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
from ...error import GraphQLError
44
from ...language import ast
55
from ...language.printer import print_ast
6+
from ...pyutils.default_ordered_dict import DefaultOrderedDict
7+
from ...pyutils.pair_set import PairSet
68
from ...type.definition import (
79
GraphQLInterfaceType,
810
GraphQLObjectType,
911
get_named_type,
1012
)
1113
from ...utils.type_from_ast import type_from_ast
1214
from .base import ValidationRule
13-
from .utils import DefaultOrderedDict, PairSet
1415

1516

1617
class OverlappingFieldsCanBeMerged(ValidationRule):

tests/core_pyutils/test_pair_set.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from graphql.core.pyutils.pair_set import PairSet
2+
3+
4+
def test_pair_set():
5+
ps = PairSet()
6+
7+
ps.add(1, 2)
8+
ps.add(2, 4)
9+
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)
16+
17+
assert not ps.has(2, 3)
18+
assert not ps.has(1, 3)
19+
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
25+
26+
assert ps.has(4, 2)
27+
assert ps.has(2, 4)

0 commit comments

Comments
 (0)