Skip to content
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

Merged
merged 3 commits into from Jan 10, 2017

Conversation

renzon
Copy link
Contributor

@renzon renzon commented Jan 3, 2017

close #335

@coveralls
Copy link

coveralls commented Jan 3, 2017

Coverage Status

Coverage decreased (-0.1%) to 98.478% when pulling 0edac6c on renzon:335 into ba008a5 on SatelliteQE:master.

@renzon renzon self-assigned this Jan 3, 2017
@renzon renzon added review and removed in progress labels Jan 3, 2017
Copy link
Contributor

@rochacbruno rochacbruno left a 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
Copy link
Contributor

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
Copy link
Contributor

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

@renzon
Copy link
Contributor Author

renzon commented Jan 4, 2017

@rochacbruno coverage improved.

@renzon
Copy link
Contributor Author

renzon commented Jan 4, 2017

For the one who is going to merge: please use "Squash and Merge" Once I fixed code on different commit.

@coveralls
Copy link

coveralls commented Jan 4, 2017

Coverage Status

Coverage increased (+0.005%) to 98.583% when pulling 7d82a01 on renzon:335 into ba008a5 on SatelliteQE:master.

Copy link
Contributor

@elyezer elyezer left a 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)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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'

Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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

Copy link
Contributor

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

@coveralls
Copy link

coveralls commented Jan 9, 2017

Coverage Status

Coverage increased (+0.005%) to 98.583% when pulling 331ce50 on renzon:335 into ba008a5 on SatelliteQE:master.

Copy link
Contributor

@elyezer elyezer left a 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)
Copy link
Contributor

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.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.05%) to 98.531% when pulling 3f667b7 on renzon:335 into ba008a5 on SatelliteQE:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 98.583% when pulling 3f667b7 on renzon:335 into ba008a5 on SatelliteQE:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.005%) to 98.583% when pulling 3f667b7 on renzon:335 into ba008a5 on SatelliteQE:master.


"""
alice = SampleEntity(self.cfg, id=1, name='Alice')
self.assertFalse(alice == None)
Copy link
Contributor

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 ==

Copy link
Contributor

@elyezer elyezer Jan 10, 2017

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)

Copy link
Contributor Author

@renzon renzon Jan 10, 2017

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 ;)

@coveralls
Copy link

coveralls commented Jan 10, 2017

Coverage Status

Coverage increased (+0.005%) to 98.583% when pulling 93e0097 on renzon:335 into ba008a5 on SatelliteQE:master.

@renzon
Copy link
Contributor Author

renzon commented Jan 10, 2017

@rochacbruno take a look on the change

@rochacbruno rochacbruno merged commit ecf91be into SatelliteQE:master Jan 10, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 16, 2017
renzon added a commit that referenced this pull request Jan 16, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 16, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 16, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 16, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 16, 2017
rochacbruno pushed a commit that referenced this pull request Jan 16, 2017
renzon added a commit to renzon/nailgun that referenced this pull request Jan 19, 2017
(cherry picked from commit ecf91be)

(cherry picked from commit f231e8c)
renzon added a commit to renzon/nailgun that referenced this pull request Jan 19, 2017
(cherry picked from commit ecf91be)

(cherry picked from commit f231e8c)
renzon added a commit to renzon/nailgun that referenced this pull request Jan 19, 2017
(cherry picked from commit ecf91be)

(cherry picked from commit f231e8c)
rochacbruno pushed a commit that referenced this pull request Jan 23, 2017
(cherry picked from commit ecf91be)

(cherry picked from commit f231e8c)
elyezer added a commit to elyezer/nailgun that referenced this pull request Mar 6, 2017
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
renzon pushed a commit to renzon/nailgun that referenced this pull request Mar 9, 2017
(cherry picked from commit ecf91be)

(cherry picked from commit f231e8c)
lpramuk pushed a commit to lpramuk/nailgun that referenced this pull request Sep 26, 2023
* Implemented Entity __eq__ method comparing all fields

close SatelliteQE#335

* Improved Coverage

close SatelliteQE#335

* Improved Coverage

close SatelliteQE#335
lpramuk pushed a commit to lpramuk/nailgun that referenced this pull request Sep 26, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make entities __eq__ comparable
4 participants