Merge pull request #1347 from fcurella/hash

Make resources hashable, so that they can be added to `set`s
This commit is contained in:
Ben Firshman 2017-01-18 16:49:35 +01:00 committed by GitHub
commit 468bb1c545
2 changed files with 17 additions and 0 deletions

View File

@ -23,6 +23,9 @@ class Model(object):
def __eq__(self, other):
return isinstance(other, self.__class__) and self.id == other.id
def __hash__(self):
return hash("%s:%s" % (self.__class__.__name__, self.id))
@property
def id(self):
"""

View File

@ -12,3 +12,17 @@ class ModelTest(unittest.TestCase):
container.reload()
assert client.api.inspect_container.call_count == 2
assert container.attrs['Name'] == "foobar"
def test_hash(self):
client = make_fake_client()
container1 = client.containers.get(FAKE_CONTAINER_ID)
my_set = set([container1])
assert len(my_set) == 1
container2 = client.containers.get(FAKE_CONTAINER_ID)
my_set.add(container2)
assert len(my_set) == 1
image1 = client.images.get(FAKE_CONTAINER_ID)
my_set.add(image1)
assert len(my_set) == 2