From bd207b5f06a64dbbd610ea901f0d76c5b4918734 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 1 Oct 2016 11:49:30 -0700 Subject: [PATCH] Improved arguments and structures comparison --- graphene/types/argument.py | 8 +++++ graphene/types/structures.py | 14 ++++++++ graphene/types/tests/test_argument.py | 26 +++++++++++++++ graphene/types/tests/test_structures.py | 44 +++++++++++++++++++++++++ 4 files changed, 92 insertions(+) create mode 100644 graphene/types/tests/test_argument.py create mode 100644 graphene/types/tests/test_structures.py diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 07dc1d1d..49784b10 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -18,6 +18,14 @@ class Argument(OrderedType): 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 diff --git a/graphene/types/structures.py b/graphene/types/structures.py index 6cf0e4aa..6c9c0e7e 100644 --- a/graphene/types/structures.py +++ b/graphene/types/structures.py @@ -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): ''' @@ -49,3 +56,10 @@ class NonNull(Structure): 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 + ) diff --git a/graphene/types/tests/test_argument.py b/graphene/types/tests/test_argument.py new file mode 100644 index 00000000..34ed3144 --- /dev/null +++ b/graphene/types/tests/test_argument.py @@ -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) diff --git a/graphene/types/tests/test_structures.py b/graphene/types/tests/test_structures.py new file mode 100644 index 00000000..9027895e --- /dev/null +++ b/graphene/types/tests/test_structures.py @@ -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