mirror of https://github.com/docker/docker-py.git
Merge pull request #1347 from fcurella/hash
Make resources hashable, so that they can be added to `set`s
This commit is contained in:
commit
468bb1c545
|
@ -23,6 +23,9 @@ class Model(object):
|
||||||
def __eq__(self, other):
|
def __eq__(self, other):
|
||||||
return isinstance(other, self.__class__) and self.id == other.id
|
return isinstance(other, self.__class__) and self.id == other.id
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash("%s:%s" % (self.__class__.__name__, self.id))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def id(self):
|
def id(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -12,3 +12,17 @@ class ModelTest(unittest.TestCase):
|
||||||
container.reload()
|
container.reload()
|
||||||
assert client.api.inspect_container.call_count == 2
|
assert client.api.inspect_container.call_count == 2
|
||||||
assert container.attrs['Name'] == "foobar"
|
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
|
||||||
|
|
Loading…
Reference in New Issue