|
|
@ -21,6 +21,8 @@ __metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
from collections.abc import Container
|
|
|
|
from collections.abc import Container
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
from units.compat import unittest
|
|
|
|
from units.compat import unittest
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
|
|
|
|
|
|
|
@ -42,12 +44,9 @@ class TestHashParams(unittest.TestCase):
|
|
|
|
self._assert_set(res)
|
|
|
|
self._assert_set(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
|
|
|
|
def _assert_hashable(self, res):
|
|
|
|
@staticmethod
|
|
|
|
a_dict = {}
|
|
|
|
def _assert_hashable(res):
|
|
|
|
try:
|
|
|
|
hash(res)
|
|
|
|
a_dict[res] = res
|
|
|
|
|
|
|
|
except TypeError as e:
|
|
|
|
|
|
|
|
self.fail('%s is not hashable: %s' % (res, e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _assert_set(self, res):
|
|
|
|
def _assert_set(self, res):
|
|
|
|
self.assertIsInstance(res, frozenset)
|
|
|
|
self.assertIsInstance(res, frozenset)
|
|
|
@ -87,36 +86,28 @@ class TestHashParams(unittest.TestCase):
|
|
|
|
|
|
|
|
|
|
|
|
def test_generator(self):
|
|
|
|
def test_generator(self):
|
|
|
|
def my_generator():
|
|
|
|
def my_generator():
|
|
|
|
for i in ['a', 1, None, {}]:
|
|
|
|
yield
|
|
|
|
yield i
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params = my_generator()
|
|
|
|
params = my_generator()
|
|
|
|
res = hash_params(params)
|
|
|
|
res = hash_params(params)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
self._assert_hashable(res)
|
|
|
|
|
|
|
|
assert list(params)
|
|
|
|
|
|
|
|
|
|
|
|
def test_container_but_not_iterable(self):
|
|
|
|
def test_container_but_not_iterable(self):
|
|
|
|
# This is a Container that is not iterable, which is unlikely but...
|
|
|
|
# This is a Container that is not iterable, which is unlikely but...
|
|
|
|
class MyContainer(Container):
|
|
|
|
class MyContainer(Container):
|
|
|
|
def __init__(self, some_thing):
|
|
|
|
def __init__(self, _some_thing):
|
|
|
|
self.data = []
|
|
|
|
pass
|
|
|
|
self.data.append(some_thing)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.data
|
|
|
|
"""Implementation omitted, since it will never be called."""
|
|
|
|
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
|
|
|
|
return hash(self.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
|
|
|
|
return len(self.data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
params = MyContainer('foo bar')
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foo = MyContainer('foo bar')
|
|
|
|
with pytest.raises(TypeError) as ex:
|
|
|
|
params = foo
|
|
|
|
hash_params(params)
|
|
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, hash_params, params)
|
|
|
|
assert ex.value.args == ("'MyContainer' object is not iterable",)
|
|
|
|
|
|
|
|
|
|
|
|
def test_param_dict_dupe_values(self):
|
|
|
|
def test_param_dict_dupe_values(self):
|
|
|
|
params1 = {'foo': False}
|
|
|
|
params1 = {'foo': False}
|
|
|
|