Skip to content

Adds __eq__ and test of it #1181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions graphene/types/objecttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ class Query(ObjectType):
**kwargs (Dict[str: Any]): Keyword arguments to use for Field values of value object
"""

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return False

@classmethod
def __init_subclass_with_meta__(
cls,
Expand Down
34 changes: 34 additions & 0 deletions graphene/types/tests/test_objecttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ def get_type(self):
return MyType


def test_equality():
# instances of object with no properties are equal
class NoPropertiesObject(ObjectType):
pass

my_obj = NoPropertiesObject()
assert my_obj == NoPropertiesObject()


# different classes are unequal
class OtherNoPropertiesObject(ObjectType):
pass

assert NoPropertiesObject() != OtherNoPropertiesObject()


# compare instances of the same simple class
class MyObjectType(ObjectType):
prop = String()

my_obj = MyObjectType(prop="a")
assert my_obj == MyObjectType(prop="a")
assert my_obj != MyObjectType(prop="b")


# complex instances of the same class
# class contains another class in a field
class ParentObjectType(ObjectType):
child = Field(MyObjectType)

my_obj = ParentObjectType(child=MyObjectType(prop="a"))
assert my_obj == ParentObjectType(child=MyObjectType(prop="a"))
assert my_obj != ParentObjectType(child=MyObjectType(prop="b"))

def test_generate_objecttype():
class MyObjectType(ObjectType):
"""Documentation"""
Expand Down