Skip to content

Commit

Permalink
Improved arguments and structures comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Oct 1, 2016
1 parent c792923 commit bd207b5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
8 changes: 8 additions & 0 deletions graphene/types/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def __init__(self, type, default_value=None, description=None, name=None, requir
self.default_value = default_value
self.description = description

def __eq__(self, other):
return isinstance(other, Argument) and (
self.name == other.name,
self.type == other.type,
self.default_value == other.default_value,
self.description == other.description
)


def to_arguments(args, extra_args):
from .unmountedtype import UnmountedType
Expand Down
14 changes: 14 additions & 0 deletions graphene/types/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class List(Structure):
def __str__(self):
return '[{}]'.format(self.of_type)

def __eq__(self, other):
return isinstance(other, List) and (
self.of_type == other.of_type and
self.args == other.args and
self.kwargs == other.kwargs
)


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

def __str__(self):
return '{}!'.format(self.of_type)

def __eq__(self, other):
return isinstance(other, NonNull) and (
self.of_type == other.of_type and
self.args == other.args and
self.kwargs == other.kwargs
)
26 changes: 26 additions & 0 deletions graphene/types/tests/test_argument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from ..argument import Argument
from ..structures import NonNull
from ..scalars import String


def test_argument():
arg = Argument(String, default_value='a', description='desc', name='b')
assert arg.type == String
assert arg.default_value == 'a'
assert arg.description == 'desc'
assert arg.name == 'b'


def test_argument_comparasion():
arg1 = Argument(String, name='Hey', description='Desc', default_value='default')
arg2 = Argument(String, name='Hey', description='Desc', default_value='default')

assert arg1 == arg2
assert arg1 != String()


def test_argument_required():
arg = Argument(String, required=True)
assert arg.type == NonNull(String)
44 changes: 44 additions & 0 deletions graphene/types/tests/test_structures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest

from ..structures import List, NonNull
from ..scalars import String


def test_list():
_list = List(String)
assert _list.of_type == String
assert str(_list) == '[String]'


def test_nonnull():
nonnull = NonNull(String)
assert nonnull.of_type == String
assert str(nonnull) == 'String!'


def test_list_comparasion():
list1 = List(String)
list2 = List(String)
list3 = List(None)

list1_argskwargs = List(String, None, b=True)
list2_argskwargs = List(String, None, b=True)

assert list1 == list2
assert list1 != list3
assert list1_argskwargs == list2_argskwargs
assert list1 != list1_argskwargs


def test_nonnull_comparasion():
nonnull1 = NonNull(String)
nonnull2 = NonNull(String)
nonnull3 = NonNull(None)

nonnull1_argskwargs = NonNull(String, None, b=True)
nonnull2_argskwargs = NonNull(String, None, b=True)

assert nonnull1 == nonnull2
assert nonnull1 != nonnull3
assert nonnull1_argskwargs == nonnull2_argskwargs
assert nonnull1 != nonnull1_argskwargs

0 comments on commit bd207b5

Please sign in to comment.