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
Conversation
2eaa27f
to
e900700
Compare
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.
ACK
but misses a little of test coverage in 2 lines/conditions
@@ -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 |
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
: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 comment
The reason will be displayed to describe this comment to others. Learn more.
This line also missing test coverage
@rochacbruno coverage improved. |
For the one who is going to merge: please use "Squash and Merge" Once I fixed code on different commit. |
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.
ACK, pending comments.
] | ||
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 comment
The 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 comment
The 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 comment
The 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 comment
The 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 json_dct[field_name] = to_json_serializable(value)
works for None and is not aware of that specific case.
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.
@elyezer: done!
|
||
""" | ||
alice = SampleEntity(self.cfg, id=1, name='Alice') | ||
self.assertNotEquals(alice, None) |
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.
Use assertNotEqual
(without the s
at the end) since assertNotEquals
is deprecated and will produce a warning:
In [7]: t.assertNotEquals
Out[7]: <bound method TestCase._deprecate.<locals>.deprecated_func of <__main__.Test testMethod=runTest>>
In [9]: t.assertNotEquals??
Signature: t.assertNotEquals(*args, **kwargs)
Source:
def deprecated_func(*args, **kwargs):
warnings.warn(
'Please use {0} instead.'.format(original_func.__name__),
DeprecationWarning, 2)
return original_func(*args, **kwargs)
File: /usr/lib64/python3.5/unittest/case.py
Type: method
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.
Same applies for assertEquals
which should be assertEqual
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.
I will approve the PR, thanks for the clarification. The last comment is just to make sure we know why the new if block is needed in the future.
] | ||
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 comment
The 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 json_dct[field_name] = to_json_serializable(value)
works for None and is not aware of that specific case.
cd72f5d
to
3f667b7
Compare
|
||
""" | ||
alice = SampleEntity(self.cfg, id=1, name='Alice') | ||
self.assertFalse(alice == None) |
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.
Comparison to None should be alice is None
instead of ==
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.
Good catch, better to use self.assertIsNotNone(alice)
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.
The point is that I want eq to be called with None. So I am going to explicitly call it ;)
@rochacbruno take a look on the change |
(cherry picked from commit ecf91be)
close SatelliteQE#335 (cherry picked from commit ecf91be)
(cherry picked from commit ecf91be)
(cherry picked from commit ecf91be)
(cherry picked from commit ecf91be)
Shortlog of commits since last release: Andrii Balakhtar (4): Fix read() of hostgroup without content_source, lce or cv for 6.1 Increase timeout for manifest delete/refresh (SatelliteQE#359) Provide default name length & type for ComputeResource entities (SatelliteQE#369) Implemented Interface entity with all its fields and basic operations (SatelliteQE#373) Djebran Lezzoum (1): Merge pull request SatelliteQE#380 from svtkachenko/ct_update_payload Elyézer Rezende (1): Update travis config Oleksandr Shtaier (8): Merge pull request SatelliteQE#341 from sghai/update-test Merge pull request SatelliteQE#342 from renzon/332 Merge pull request SatelliteQE#344 from elyezer/update-travis Merge pull request SatelliteQE#352 from svtkachenko/sv_payload Merge pull request SatelliteQE#345 from abalakh/fix_hg_61_workaround Merge pull request SatelliteQE#366 from svtkachenko/update_template_kind Merge pull request SatelliteQE#361 from svtkachenko/update_sv Merge pull request SatelliteQE#383 from svtkachenko/63_image Renzo Nuccitelli (3): Added json method to Entity (SatelliteQE#326) Merge pull request SatelliteQE#347 from svtkachenko/add_image Implemented Entity __eq__ method comparing all fields (SatelliteQE#350) Stanislav Tkachenko (10): Add content related helpers for Repository entity. (SatelliteQE#327) Add Create and Delete mixins to Smart Proxy (SatelliteQE#343) Add mixins to Image Override create_payload and upload_payload for SmartVariable. Update Host.puppet_class field name. (SatelliteQE#346) Removed overriden read method for Smart Variable Update TemplateKind entity Add ProvisioningTemplate. (SatelliteQE#365) Fix update_payload method for ConfigTemplate/ProvisioningTemplate [6.2.z] Update Image and Host entities (SatelliteQE#356) oshtaier (2): Add UpdateMixin for CV Filter Rules entity Add host count field to CV entity renzon (5): Fixed ConfigTemplate create and update with TemplateCombination ListFiled handled on _payload api fixed and methods GET and DELETED added for TemplateCombination api fixed and methods GET and DELETED added for TemplateCombination api fixed and methods GET and DELETED added for TemplateCombination sghai (5): Updated discovery_rule entity with org & location (SatelliteQE#320) Added a new field root_pass to hostgroup entity (SatelliteQE#336) Added unit test for root_pass from hostgroup entity Merge pull request SatelliteQE#338 from oshtaier/cvf_update_mixin Merge pull request SatelliteQE#340 from oshtaier/cv_field
* Implemented Entity __eq__ method comparing all fields close SatelliteQE#335 * Improved Coverage close SatelliteQE#335 * Improved Coverage close SatelliteQE#335
Shortlog of commits since last release: Andrii Balakhtar (4): Fix read() of hostgroup without content_source, lce or cv for 6.1 Increase timeout for manifest delete/refresh (SatelliteQE#359) Provide default name length & type for ComputeResource entities (SatelliteQE#369) Implemented Interface entity with all its fields and basic operations (SatelliteQE#373) Djebran Lezzoum (1): Merge pull request SatelliteQE#380 from svtkachenko/ct_update_payload Elyézer Rezende (1): Update travis config Oleksandr Shtaier (8): Merge pull request SatelliteQE#341 from sghai/update-test Merge pull request SatelliteQE#342 from renzon/332 Merge pull request SatelliteQE#344 from elyezer/update-travis Merge pull request SatelliteQE#352 from svtkachenko/sv_payload Merge pull request SatelliteQE#345 from abalakh/fix_hg_61_workaround Merge pull request SatelliteQE#366 from svtkachenko/update_template_kind Merge pull request SatelliteQE#361 from svtkachenko/update_sv Merge pull request SatelliteQE#383 from svtkachenko/63_image Renzo Nuccitelli (3): Added json method to Entity (SatelliteQE#326) Merge pull request SatelliteQE#347 from svtkachenko/add_image Implemented Entity __eq__ method comparing all fields (SatelliteQE#350) Stanislav Tkachenko (10): Add content related helpers for Repository entity. (SatelliteQE#327) Add Create and Delete mixins to Smart Proxy (SatelliteQE#343) Add mixins to Image Override create_payload and upload_payload for SmartVariable. Update Host.puppet_class field name. (SatelliteQE#346) Removed overriden read method for Smart Variable Update TemplateKind entity Add ProvisioningTemplate. (SatelliteQE#365) Fix update_payload method for ConfigTemplate/ProvisioningTemplate [6.2.z] Update Image and Host entities (SatelliteQE#356) oshtaier (2): Add UpdateMixin for CV Filter Rules entity Add host count field to CV entity renzon (5): Fixed ConfigTemplate create and update with TemplateCombination ListFiled handled on _payload api fixed and methods GET and DELETED added for TemplateCombination api fixed and methods GET and DELETED added for TemplateCombination api fixed and methods GET and DELETED added for TemplateCombination sghai (5): Updated discovery_rule entity with org & location (SatelliteQE#320) Added a new field root_pass to hostgroup entity (SatelliteQE#336) Added unit test for root_pass from hostgroup entity Merge pull request SatelliteQE#338 from oshtaier/cvf_update_mixin Merge pull request SatelliteQE#340 from oshtaier/cv_field
close #335