mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-18 03:52:24 +03:00
Adds __eq__ and test of it
This commit is contained in:
parent
49fcf9f2e6
commit
c5fcb52845
|
@ -118,6 +118,11 @@ class ObjectType(BaseType, metaclass=ObjectTypeMeta):
|
||||||
**kwargs (Dict[str: Any]): Keyword arguments to use for Field values of value object
|
**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
|
@classmethod
|
||||||
def __init_subclass_with_meta__(
|
def __init_subclass_with_meta__(
|
||||||
cls,
|
cls,
|
||||||
|
|
|
@ -35,6 +35,40 @@ class MyScalar(UnmountedType):
|
||||||
return MyType
|
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():
|
def test_generate_objecttype():
|
||||||
class MyObjectType(ObjectType):
|
class MyObjectType(ObjectType):
|
||||||
"""Documentation"""
|
"""Documentation"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user