当前位置: 首页>>代码示例>>Python>>正文


Python Mock.__iter__方法代码示例

本文整理汇总了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())
开发者ID:PeterDaveHello,项目名称:doorstop,代码行数:27,代码来源:test_item.py

示例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)
开发者ID:40123107,项目名称:2014cda,代码行数:27,代码来源:testmagicmethods.py

示例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))
开发者ID:AlexHorlenko,项目名称:ironpython3,代码行数:9,代码来源:testmagicmethods.py

示例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)
开发者ID:jacebrowning,项目名称:doorstop-demo,代码行数:32,代码来源:test_item.py

示例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
开发者ID:artursmet,项目名称:saleor,代码行数:33,代码来源:test_checkout.py

示例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))
开发者ID:jacebrowning,项目名称:doorstop-demo,代码行数:31,代码来源:test_item.py

示例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)
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:28,代码来源:testmagicmethods.py

示例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())
开发者ID:PeterDaveHello,项目名称:doorstop,代码行数:29,代码来源:test_item.py

示例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), [])
开发者ID:fungbo,项目名称:PythonMockSample,代码行数:9,代码来源:test_magic_method.py

示例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)
开发者ID:AlexHorlenko,项目名称:ironpython3,代码行数:11,代码来源:testmagicmethods.py

示例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
    )
开发者ID:,项目名称:,代码行数:12,代码来源:

示例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'))
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:14,代码来源:testmagicmethods.py

示例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))
开发者ID:johndpope,项目名称:sims4-ai-engine,代码行数:15,代码来源:testmock.py

示例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
开发者ID:jacebrowning,项目名称:doorstop-demo,代码行数:16,代码来源:test_item.py

示例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"))
开发者ID:40123107,项目名称:2014cda,代码行数:18,代码来源:testmagicmethods.py


注:本文中的unittest.mock.Mock.__iter__方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。