本文整理汇总了Python中unittest.mock.Mock.__iter__方法的典型用法代码示例。如果您正苦于以下问题:Python Mock.__iter__方法的具体用法?Python Mock.__iter__怎么用?Python Mock.__iter__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类unittest.mock.Mock
的用法示例。
在下文中一共展示了Mock.__iter__方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_validate_both
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_validate_both(self):
"""Verify an item can be checked against both."""
def mock_iter(seq):
"""Creates a mock __iter__ method."""
def _iter(self): # pylint: disable=W0613
"""Mock __iter__method."""
yield from seq
return _iter
mock_item = Mock()
mock_item.links = [self.item.uid]
mock_document = Mock()
mock_document.parent = 'BOTH'
mock_document.prefix = 'BOTH'
mock_document.__iter__ = mock_iter([mock_item])
mock_tree = Mock()
mock_tree.__iter__ = mock_iter([mock_document])
self.item.document = mock_document
self.item.tree = mock_tree
self.assertTrue(self.item.validate())
示例2: test_magic_methods_and_spec_set
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_magic_methods_and_spec_set(self):
class Iterable(object):
def __iter__(self):
pass
mock = Mock(spec_set=Iterable)
self.assertRaises(AttributeError, lambda: mock.__iter__)
mock.__iter__ = Mock(return_value=iter([]))
self.assertEqual(list(mock), [])
class NonIterable(object):
pass
mock = Mock(spec_set=NonIterable)
self.assertRaises(AttributeError, lambda: mock.__iter__)
def set_int():
mock.__int__ = Mock(return_value=iter([]))
self.assertRaises(AttributeError, set_int)
mock = MagicMock(spec_set=Iterable)
self.assertEqual(list(mock), [])
self.assertRaises(AttributeError, set_int)
示例3: test_magic_methods_isolated_between_mocks
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_magic_methods_isolated_between_mocks(self):
mock1 = Mock()
mock2 = Mock()
mock1.__iter__ = Mock(return_value=iter([]))
self.assertEqual(list(mock1), [])
self.assertRaises(TypeError, lambda: list(mock2))
示例4: test_find_rlinks
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_find_rlinks(self):
"""Verify an item's reverse links can be found."""
mock_document_p = Mock()
mock_document_p.prefix = 'RQ'
mock_document_c = Mock()
mock_document_c.parent = 'RQ'
mock_item = Mock()
mock_item.id = 'TST001'
mock_item.links = ['RQ001']
def mock_iter(self): # pylint: disable=W0613
"""Mock Tree.__iter__ to yield a mock Document."""
def mock_iter2(self): # pylint: disable=W0613
"""Mock Document.__iter__ to yield a mock Item."""
yield mock_item
mock_document_c.__iter__ = mock_iter2
yield mock_document_c
self.item.add_link('fake1')
mock_tree = Mock()
mock_tree.__iter__ = mock_iter
mock_tree.find_item = lambda identifier: Mock(id='fake1')
rlinks, childrem = self.item.find_rlinks(mock_document_p, mock_tree)
self.assertEqual(['TST001'], rlinks)
self.assertEqual([mock_document_c], childrem)
示例5: test_checkout_deliveries_with_shipping_method
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_checkout_deliveries_with_shipping_method(monkeypatch):
shipping_cost = Money(3, settings.DEFAULT_CURRENCY)
items_cost = Money(5, settings.DEFAULT_CURRENCY)
cost_with_shipping = items_cost + shipping_cost
items_price = TaxedMoney(net=items_cost, gross=items_cost)
partition = Mock(
is_shipping_required=MagicMock(return_value=True),
get_total=Mock(return_value=items_price),
get_price_per_item=Mock(return_value=items_price))
def f():
yield partition
partition.__iter__ = Mock(return_value=f())
cart = Mock(
partition=Mock(return_value=[partition]),
currency=settings.DEFAULT_CURRENCY)
shipping_price = TaxedMoney(net=shipping_cost, gross=shipping_cost)
shipping_method_mock = Mock(
get_total_price=Mock(return_value=shipping_price))
monkeypatch.setattr(Checkout, 'shipping_method', shipping_method_mock)
checkout = Checkout(cart, AnonymousUser(), 'tracking_code')
deliveries = list(checkout.deliveries)
assert deliveries[0][1] == shipping_price
assert deliveries[0][2] == TaxedMoney(
net=cost_with_shipping, gross=cost_with_shipping)
assert deliveries[0][0][0][0] == partition
示例6: test_valid_both_no_reverse_links
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_valid_both_no_reverse_links(self):
"""Verify an item can be checked against both (no reverse links)."""
def mock_iter(self): # pylint: disable=W0613
"""Mock Tree.__iter__ to yield a mock Document."""
mock_document = Mock()
mock_document.parent = 'RQ'
def mock_iter2(self): # pylint: disable=W0613
"""Mock Document.__iter__ to yield a mock Item."""
mock_item = Mock()
mock_item.id = 'TST001'
mock_item.links = []
yield mock_item
mock_document.__iter__ = mock_iter2
yield mock_document
self.item.add_link('fake1')
mock_document = Mock()
mock_document.prefix = 'RQ'
mock_tree = Mock()
mock_tree.__iter__ = mock_iter
mock_tree.find_item = lambda identifier: Mock(id='fake1')
self.assertTrue(self.item.valid(document=mock_document,
tree=mock_tree))
示例7: test_magic_methods_and_spec
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_magic_methods_and_spec(self):
class Iterable(object):
__qualname__ = 'TestMockingMagicMethods.test_magic_methods_and_spec.<locals>.Iterable'
def __iter__(self):
pass
mock = Mock(spec=Iterable)
self.assertRaises(AttributeError, lambda : mock.__iter__)
mock.__iter__ = Mock(return_value=iter([]))
self.assertEqual(list(mock), [])
class NonIterable(object):
__qualname__ = 'TestMockingMagicMethods.test_magic_methods_and_spec.<locals>.NonIterable'
mock = Mock(spec=NonIterable)
self.assertRaises(AttributeError, lambda : mock.__iter__)
def set_int():
mock.__int__ = Mock(return_value=iter([]))
self.assertRaises(AttributeError, set_int)
mock = MagicMock(spec=Iterable)
self.assertEqual(list(mock), [])
self.assertRaises(AttributeError, set_int)
示例8: test_validate_tree
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_validate_tree(self):
"""Verify an item can be checked against a tree."""
def mock_iter(self): # pylint: disable=W0613
"""Mock Tree.__iter__ to yield a mock Document."""
mock_document = Mock()
mock_document.parent = 'RQ'
def mock_iter2(self): # pylint: disable=W0613
"""Mock Document.__iter__ to yield a mock Item."""
mock_item = Mock()
mock_item.uid = 'TST001'
mock_item.links = ['RQ001']
yield mock_item
mock_document.__iter__ = mock_iter2
yield mock_document
self.item.link('fake1')
mock_tree = Mock()
mock_tree.__iter__ = mock_iter
mock_tree.find_item = lambda uid: Mock(uid='fake1')
self.item.tree = mock_tree
self.assertTrue(self.item.validate())
示例9: test_use_mock
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_use_mock(self):
m = Mock()
m.__str__ = Mock(return_value='mock')
m.__iter__ = Mock(return_value=iter([]))
self.assertEqual(str(m), 'mock')
self.assertEqual(list(m), [])
示例10: test_bound_methods
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_bound_methods(self):
m = Mock()
# XXXX should this be an expected failure instead?
# this seems like it should work, but is hard to do without introducing
# other api inconsistencies. Failure message could be better though.
m.__iter__ = [3].__iter__
self.assertRaises(TypeError, iter, m)
示例11: _make_backend
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def _make_backend(target_folder_path, template_path):
# type: (typing.Text, typing.Text) -> TSDTypesBackend
args = Mock()
args.__iter__ = Mock(return_value=iter([template_path, "-i=0"]))
return TSDTypesBackend(
target_folder_path=str(target_folder_path),
args=args
)
示例12: test_len_contains_iter
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_len_contains_iter(self):
mock = Mock()
self.assertRaises(TypeError, len, mock)
self.assertRaises(TypeError, iter, mock)
self.assertRaises(TypeError, lambda : 'foo' in mock)
mock.__len__ = lambda s: 6
self.assertEqual(len(mock), 6)
mock.__contains__ = lambda s, o: o == 3
self.assertIn(3, mock)
self.assertNotIn(6, mock)
mock.__iter__ = lambda s: iter('foobarbaz')
self.assertEqual(list(mock), list('foobarbaz'))
示例13: test_dir
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_dir(self):
mock = Mock()
attrs = set(dir(mock))
type_attrs = set([m for m in dir(Mock) if not m.startswith('_')])
self.assertEqual(set(), type_attrs - attrs)
(mock.a, mock.b)
self.assertIn('a', dir(mock))
self.assertIn('b', dir(mock))
mock.c = mock.d = None
self.assertIn('c', dir(mock))
self.assertIn('d', dir(mock))
mock.__iter__ = lambda s: iter([])
self.assertIn('__iter__', dir(mock))
示例14: mock_iter
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def mock_iter(self): # pylint: disable=W0613
"""Mock Tree.__iter__ to yield a mock Document."""
mock_document = Mock()
mock_document.parent = 'RQ'
def mock_iter2(self): # pylint: disable=W0613
"""Mock Document.__iter__ to yield a mock Item."""
mock_item = Mock()
mock_item.id = 'TST001'
mock_item.links = []
yield mock_item
mock_document.__iter__ = mock_iter2
yield mock_document
示例15: test_len_contains_iter
# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import __iter__ [as 别名]
def test_len_contains_iter(self):
mock = Mock()
self.assertRaises(TypeError, len, mock)
self.assertRaises(TypeError, iter, mock)
self.assertRaises(TypeError, lambda: "foo" in mock)
mock.__len__ = lambda s: 6
self.assertEqual(len(mock), 6)
mock.__contains__ = lambda s, o: o == 3
self.assertTrue(3 in mock)
self.assertFalse(6 in mock)
mock.__iter__ = lambda s: iter("foobarbaz")
self.assertEqual(list(mock), list("foobarbaz"))