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
Implemented Entity __eq__ method comparing all fields #350
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,7 @@ def _poll_task(task_id, server_config, poll_rate=None, timeout=None): | |
def raise_task_timeout(): # pragma: no cover | ||
"""Raise a KeyboardInterrupt exception in the main thread.""" | ||
thread.interrupt_main() | ||
|
||
timer = threading.Timer(timeout, raise_task_timeout) | ||
|
||
# Poll until the task finishes. The timeout prevents an infinite loop. | ||
|
@@ -551,17 +552,30 @@ def to_json_dict(self): | |
json_dct = {} | ||
for field_name, field in fields.items(): | ||
if field_name in values: | ||
value = values[field_name] | ||
if value is None: | ||
json_dct[field_name] = None | ||
if isinstance(field, OneToOneField): | ||
json_dct[field_name] = values[field_name].to_json_dict() | ||
json_dct[field_name] = value.to_json_dict() | ||
elif isinstance(field, OneToManyField): | ||
json_dct[field_name] = [ | ||
entity.to_json_dict() for entity in values[field_name] | ||
entity.to_json_dict() for entity in value | ||
] | ||
else: | ||
json_dct[field_name] = to_json_serializable( | ||
values[field_name]) | ||
json_dct[field_name] = to_json_serializable(value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did the following test: In [1]: from nailgun.entity_mixins import to_json_serializable
In [2]: to_json_serializable(None)
In [3]: print(to_json_serializable(None))
None There is no need to treat the value is None since the else clause will already take care of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first I was doing it. But discovered a case during test on robottelo where an OneToOne is None and. 'json_dct[field_name] = value.to_json_dict()' lead to error because to_json_dict() is been called on a None value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finnaly found the case I mentioned above: hostgroup = entities.HostGroup(
location=[self.loc],
organization=[self.org],
).create()
hostgroup.to_json_dict() Stack trace: def to_json_dict(self):
"""Create a dct with Entity properties for json encoding.
It can be overridden by subclasses for each standard serialization
doesn't work. By default it call _to_json_dict on OneToOne fields
and build a list calling the same method on each object on OneToMany
fields.
:return: dct
"""
fields, values = self.get_fields(), self.get_values()
json_dct = {}
for field_name, field in fields.items():
if field_name in values:
if isinstance(field, OneToOneField):
> json_dct[field_name] = values[field_name].to_json_dict()
E AttributeError: 'NoneType' object has no attribute 'to_json_dict' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, do you mind adding a comment on that if clause? It may happen that someone else realize that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elyezer: done! |
||
return json_dct | ||
|
||
def __eq__(self, other): | ||
"""Compare two entities based on their properties. Even nested | ||
objects are considered for equality | ||
|
||
:param other: entity to compare self to | ||
:return: boolean indicating if entities are equal or not | ||
""" | ||
if other is None: | ||
return False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line also missing test coverage |
||
return self.to_json_dict() == other.to_json_dict() | ||
|
||
|
||
class EntityDeleteMixin(object): | ||
"""This mixin provides the ability to delete an entity. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coveralls says this line misses test coverage