Skip to content

Commit c5fcb52

Browse files
committed
Adds __eq__ and test of it
1 parent 49fcf9f commit c5fcb52

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

graphene/types/objecttype.py

+5
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ class Query(ObjectType):
118118
**kwargs (Dict[str: Any]): Keyword arguments to use for Field values of value object
119119
"""
120120

121+
def __eq__(self, other):
122+
if isinstance(other, self.__class__):
123+
return self.__dict__ == other.__dict__
124+
return False
125+
121126
@classmethod
122127
def __init_subclass_with_meta__(
123128
cls,

graphene/types/tests/test_objecttype.py

+34
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,40 @@ def get_type(self):
3535
return MyType
3636

3737

38+
def test_equality():
39+
# instances of object with no properties are equal
40+
class NoPropertiesObject(ObjectType):
41+
pass
42+
43+
my_obj = NoPropertiesObject()
44+
assert my_obj == NoPropertiesObject()
45+
46+
47+
# different classes are unequal
48+
class OtherNoPropertiesObject(ObjectType):
49+
pass
50+
51+
assert NoPropertiesObject() != OtherNoPropertiesObject()
52+
53+
54+
# compare instances of the same simple class
55+
class MyObjectType(ObjectType):
56+
prop = String()
57+
58+
my_obj = MyObjectType(prop="a")
59+
assert my_obj == MyObjectType(prop="a")
60+
assert my_obj != MyObjectType(prop="b")
61+
62+
63+
# complex instances of the same class
64+
# class contains another class in a field
65+
class ParentObjectType(ObjectType):
66+
child = Field(MyObjectType)
67+
68+
my_obj = ParentObjectType(child=MyObjectType(prop="a"))
69+
assert my_obj == ParentObjectType(child=MyObjectType(prop="a"))
70+
assert my_obj != ParentObjectType(child=MyObjectType(prop="b"))
71+
3872
def test_generate_objecttype():
3973
class MyObjectType(ObjectType):
4074
"""Documentation"""

0 commit comments

Comments
 (0)