Skip to content

Commit bd207b5

Browse files
committed
Improved arguments and structures comparison
1 parent c792923 commit bd207b5

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

graphene/types/argument.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ def __init__(self, type, default_value=None, description=None, name=None, requir
1818
self.default_value = default_value
1919
self.description = description
2020

21+
def __eq__(self, other):
22+
return isinstance(other, Argument) and (
23+
self.name == other.name,
24+
self.type == other.type,
25+
self.default_value == other.default_value,
26+
self.description == other.description
27+
)
28+
2129

2230
def to_arguments(args, extra_args):
2331
from .unmountedtype import UnmountedType

graphene/types/structures.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class List(Structure):
2727
def __str__(self):
2828
return '[{}]'.format(self.of_type)
2929

30+
def __eq__(self, other):
31+
return isinstance(other, List) and (
32+
self.of_type == other.of_type and
33+
self.args == other.args and
34+
self.kwargs == other.kwargs
35+
)
36+
3037

3138
class NonNull(Structure):
3239
'''
@@ -49,3 +56,10 @@ def __init__(self, *args, **kwargs):
4956

5057
def __str__(self):
5158
return '{}!'.format(self.of_type)
59+
60+
def __eq__(self, other):
61+
return isinstance(other, NonNull) and (
62+
self.of_type == other.of_type and
63+
self.args == other.args and
64+
self.kwargs == other.kwargs
65+
)

graphene/types/tests/test_argument.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
3+
from ..argument import Argument
4+
from ..structures import NonNull
5+
from ..scalars import String
6+
7+
8+
def test_argument():
9+
arg = Argument(String, default_value='a', description='desc', name='b')
10+
assert arg.type == String
11+
assert arg.default_value == 'a'
12+
assert arg.description == 'desc'
13+
assert arg.name == 'b'
14+
15+
16+
def test_argument_comparasion():
17+
arg1 = Argument(String, name='Hey', description='Desc', default_value='default')
18+
arg2 = Argument(String, name='Hey', description='Desc', default_value='default')
19+
20+
assert arg1 == arg2
21+
assert arg1 != String()
22+
23+
24+
def test_argument_required():
25+
arg = Argument(String, required=True)
26+
assert arg.type == NonNull(String)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
3+
from ..structures import List, NonNull
4+
from ..scalars import String
5+
6+
7+
def test_list():
8+
_list = List(String)
9+
assert _list.of_type == String
10+
assert str(_list) == '[String]'
11+
12+
13+
def test_nonnull():
14+
nonnull = NonNull(String)
15+
assert nonnull.of_type == String
16+
assert str(nonnull) == 'String!'
17+
18+
19+
def test_list_comparasion():
20+
list1 = List(String)
21+
list2 = List(String)
22+
list3 = List(None)
23+
24+
list1_argskwargs = List(String, None, b=True)
25+
list2_argskwargs = List(String, None, b=True)
26+
27+
assert list1 == list2
28+
assert list1 != list3
29+
assert list1_argskwargs == list2_argskwargs
30+
assert list1 != list1_argskwargs
31+
32+
33+
def test_nonnull_comparasion():
34+
nonnull1 = NonNull(String)
35+
nonnull2 = NonNull(String)
36+
nonnull3 = NonNull(None)
37+
38+
nonnull1_argskwargs = NonNull(String, None, b=True)
39+
nonnull2_argskwargs = NonNull(String, None, b=True)
40+
41+
assert nonnull1 == nonnull2
42+
assert nonnull1 != nonnull3
43+
assert nonnull1_argskwargs == nonnull2_argskwargs
44+
assert nonnull1 != nonnull1_argskwargs

0 commit comments

Comments
 (0)