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

Fix MarathonResource hash as well #205

Merged
merged 2 commits into from
Jun 27, 2017
Merged
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
6 changes: 6 additions & 0 deletions marathon/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ def __eq__(self, other):
except:
return False

def __hash__(self):
# Technically this class shouldn't be hashable because it often
# contains mutable fields, but in practice this class is used more
# like a record or namedtuple.
return hash(self.to_json())

def __str__(self):
return "{clazz}::".format(clazz=self.__class__.__name__) + str(self.__dict__)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_model_object.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# encoding: utf-8

from marathon.models.base import MarathonObject
from marathon.models.base import MarathonResource
import unittest


Expand All @@ -19,3 +20,21 @@ def test_hashable(self):
collection = {}
collection[obj] = True
assert collection[obj]


class MarathonResourceHashable(unittest.TestCase):

def test_hashable(self):
"""
Regression test for issue #203

MarathonResource defined __eq__ but not __hash__, meaning that in
in Python2.7 MarathonResources are hashable, but in Python3 they're
not

This test ensures that we are hashable in all versions of python
"""
obj = MarathonResource()
collection = {}
collection[obj] = True
assert collection[obj]