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


Python MonkeyPatcher.patch方法代码示例

本文整理汇总了Python中twisted.python.monkey.MonkeyPatcher.patch方法的典型用法代码示例。如果您正苦于以下问题:Python MonkeyPatcher.patch方法的具体用法?Python MonkeyPatcher.patch怎么用?Python MonkeyPatcher.patch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.python.monkey.MonkeyPatcher的用法示例。


在下文中一共展示了MonkeyPatcher.patch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_error_logging

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_error_logging(self, logger):
     """
     Failures while applying a diff emit a log message containing the full
     diff.
     """
     o1 = DiffTestObjInvariant(
         a=1,
         b=2,
     )
     patcher = MonkeyPatcher()
     patcher.addPatch(
         DiffTestObjInvariant,
         '_perform_invariant_check',
         False
     )
     patcher.patch()
     try:
         o2 = o1.set('b', 1)
     finally:
         patcher.restore()
     diff = create_diff(o1, o2)
     self.assertRaises(
         InvariantException,
         diff.apply,
         o1,
     )
开发者ID:332054781,项目名称:flocker,代码行数:28,代码来源:test_diffing.py

示例2: test_delete_works_on_valid_credentials

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def test_delete_works_on_valid_credentials(self):

        from hashlib import sha512

        ctuple = namedtuple('named_user', 'email key')
        user_tuple = ctuple('[email protected]', sha512('key').hexdigest())

        def delete(fles, user):
            self.assertEqual(user.email, '[email protected]')
            self.assertEqual(user, user_tuple)

        monkey_patcher = MonkeyPatcher((user.User, 'delete', delete))
        monkey_patcher.patch()

        url = '/delete/key'
        request = self.generate_request_and_session(
            url, auth=lambda: True, uuid='73s7b33f'
        )
        request.session.user = user_tuple
        request.session.expire = lambda: True

        result = yield self.account.render(request)

        self.assertEqual(result.code, http.OK)
        self.assertEqual(result.subject['success'], True)
开发者ID:DamnWidget,项目名称:BlackMamba,代码行数:27,代码来源:test_controller_account.py

示例3: test_put_verifyProperRemoval

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_put_verifyProperRemoval(self):
     # Replace the time function of the datastore module
     # so that we can artificially speed up time
     monkey_patcher = MonkeyPatcher()
     c = clock()
     c.set(0)
     monkey_patcher.addPatch(datastore, "time", c)
     # Replace the peer_timeout to 5 seconds
     monkey_patcher.addPatch(constants, "peer_timeout", 5)
     monkey_patcher.patch()
     # Insert a node and verify it is within the datastore
     m = self.datastore(self.reactor)
     infohash = 5
     expected_peer = ("127.0.0.1", 5151)
     m.put(infohash, expected_peer)
     peers = m.get(infohash)
     # Iterate over a 1 element list
     for peer in peers:
         self.assertEqual(expected_peer, peer)
     self.assertEquals(1, len(peers))
     # Change the time and verify that the cleaning function
     # actually removes the peer
     c.set(5)
     # TODO hackish, shouldnt reach into object
     m._cleanup(infohash, peer)
     peers = m.get(infohash)
     self.assertEqual(0, len(peers))
     monkey_patcher.restore()
开发者ID:aburan28,项目名称:DHTBot,代码行数:30,代码来源:test_datastore.py

示例4: __init__

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def __init__(self, model):
        if '__pypy__' in sys.modules:
            # try to use psycopg2ct if we are on PyPy
            try:
                from psycopg2ct import compat
                compat.register()

                # monkey patch to dont let Storm crash on register type
                import psycopg2
                psycopg2._psycopg = object

                class _psycopg:
                    UNICODEARRAY = psycopg2.extensions.UNICODEARRAY

                from twisted.python.monkey import MonkeyPatcher
                monkey_patcher = MonkeyPatcher(
                    (psycopg2, '_psycopg', _psycopg))
                monkey_patcher.patch()

            except ImportError:
                raise RuntimeError(
                    'You are trying to use PostgreSQL with PyPy. Regular '
                    'psycopg2 module don\'t work with PyPy, you may install '
                    'psycopg2ct in order to can use psycopg2 with PyPy'
                )

        self.model = model
开发者ID:aroshni,项目名称:mamba,代码行数:29,代码来源:postgres.py

示例5: TestingBase

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
class TestingBase(object):
    def setUp(self):
        self.clock = Clock()
        self.monkey_patcher = MonkeyPatcher()
        self.monkey_patcher.addPatch(rate_limiter.time, "time", self.clock)
        self.monkey_patcher.patch()

    def tearDown(self):
        self.monkey_patcher.restore()
开发者ID:aburan28,项目名称:DHTBot,代码行数:11,代码来源:test_rate_limiter.py

示例6: test_constructWithPatches

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_constructWithPatches(self):
     """
     Constructing a L{MonkeyPatcher} with patches should add all of the
     given patches to the patch list.
     """
     patcher = MonkeyPatcher((self.testObject, "foo", "haha"), (self.testObject, "bar", "hehe"))
     patcher.patch()
     self.assertEqual("haha", self.testObject.foo)
     self.assertEqual("hehe", self.testObject.bar)
     self.assertEqual(self.originalObject.baz, self.testObject.baz)
开发者ID:wangdayoux,项目名称:OpenSignals,代码行数:12,代码来源:test_monkey.py

示例7: test_constructWithPatches

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_constructWithPatches(self):
     """
     Constructing a L{MonkeyPatcher} with patches should add all of the
     given patches to the patch list.
     """
     patcher = MonkeyPatcher((self.testObject, 'foo', 'haha'),
                             (self.testObject, 'bar', 'hehe'))
     patcher.patch()
     self.assertEqual('haha', self.testObject.foo)
     self.assertEqual('hehe', self.testObject.bar)
     self.assertEqual(self.originalObject.baz, self.testObject.baz)
开发者ID:BillAndersan,项目名称:twisted,代码行数:13,代码来源:test_monkey.py

示例8: __init__

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def __init__(self, model):
        if '__pypy__' in sys.modules:
            # try to use psycopg2ct if we are on PyPy
            try:
                from psycopg2ct import compat
                compat.register()

                # monkey patch to dont let Storm crash on register type
                import psycopg2
                psycopg2._psycopg = object

                class _psycopg:
                    UNICODEARRAY = psycopg2.extensions.UNICODEARRAY

                from twisted.python.monkey import MonkeyPatcher
                monkey_patcher = MonkeyPatcher(
                    (psycopg2, '_psycopg', _psycopg))
                monkey_patcher.patch()

            except ImportError:
                raise RuntimeError(
                    'You are trying to use PostgreSQL with PyPy. Regular '
                    'psycopg2 module don\'t work with PyPy, you may install '
                    'psycopg2ct in order to can use psycopg2 with PyPy'
                )

        self.model = model

        self._columns_mapping = {
            properties.Bool: 'bool',
            properties.UUID: 'uuid',
            properties.RawStr: 'bytea',
            properties.Pickle: 'bytea',
            properties.JSON: 'json',
            properties.DateTime: 'timestamp',
            properties.Date: 'date',
            properties.Time: 'time',
            properties.TimeDelta: 'interval',
            properties.Enum: 'integer',
            properties.Decimal: 'decimal'
        }

        self.parse = singledispatch(self.parse)
        self.parse.register(properties.Int, self._parse_int)
        self.parse.register(properties.Unicode, self._parse_unicode)
        self.parse.register(properties.Float, self._parse_float)
        self.parse.register(properties.List, self._parse_list)
        self.parse.register(NativeEnum, self._parse_enum)
开发者ID:nicholasamorim,项目名称:mamba,代码行数:50,代码来源:postgres.py

示例9: _monkey_patch

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def _monkey_patch(self):
        """
        Monkeypatch some parts of the twisted library that are waiting
        for bugfix inclussion in the trunk
        """

        if not self.monkey_patched:
            # add new method
            setattr(http.Request, 'getClientProxyIP', getClientProxyIP)

            # patch getClientIP
            monkey_patcher = MonkeyPatcher(
                (http.Request, 'getClientIP', getClientIPPatch)
            )
            monkey_patcher.patch()
            self.monkey_patched = True
开发者ID:DamnWidget,项目名称:mamba,代码行数:18,代码来源:app.py

示例10: test_regsiter_works_when_provided_information_is_valid

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def test_regsiter_works_when_provided_information_is_valid(self):

        def create(fles):

            self.assertEqual(fles.name, 'Someone')
            self.assertEqual(fles.email, '[email protected]')

        monkey_patcher = MonkeyPatcher((user.User, 'create', create))
        monkey_patcher.patch()
        request = self.generate_request(['/register'], '''{
            "name": "Someone",
            "email": "[email protected]"
        }''')

        result = yield self.account.render(request)

        self.assertEqual(result.code, http.OK)
        self.assertEqual(type(result.subject), dict)
        self.assertEqual(result.headers['content-type'], 'application/json')
        self.assertTrue(result.subject['success'])
开发者ID:DamnWidget,项目名称:BlackMamba,代码行数:22,代码来源:test_controller_account.py

示例11: test_regsiter_works_when_provided_information_is_valid

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def test_regsiter_works_when_provided_information_is_valid(self):
        def create(fles):

            self.assertEqual(fles.name, "Someone")
            self.assertEqual(fles.email, "[email protected]")

        monkey_patcher = MonkeyPatcher((user.User, "create", create))
        monkey_patcher.patch()
        request = self.generate_request(
            ["/register"],
            """{
            "name": "Someone",
            "email": "[email protected]"
        }""",
        )

        result = yield self.account.render(request)

        self.assertEqual(result.code, http.OK)
        self.assertEqual(type(result.subject), dict)
        self.assertEqual(result.headers["content-type"], "application/json")
        self.assertTrue(result.subject["success"])
开发者ID:PyMamba,项目名称:BlackMamba,代码行数:24,代码来源:test_controller_account.py

示例12: test_unknown_instance_id

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_unknown_instance_id(self):
     """
     ``UnknownInstanceID`` is raised if all node UUID lookup mechanisms
     fail.
     """
     patch = MonkeyPatcher()
     # Use non-existent config drive label.
     # Mount will fail.
     patch.addPatch(
         self.api,
         '_config_drive_label',
         filesystem_label_for_test(self)
     )
     # Use an unreachable metadata service endpoint address.
     # TCP connections will fail.
     patch.addPatch(
         self.api,
         '_metadata_service_endpoint',
         find_free_port()
     )
     self.addCleanup(patch.restore)
     patch.patch()
     self.assertRaises(UnknownInstanceID, self.api.compute_instance_id)
开发者ID:ClusterHQ,项目名称:flocker,代码行数:25,代码来源:test_cinder.py

示例13: test_delete_works_on_valid_credentials

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def test_delete_works_on_valid_credentials(self):

        from hashlib import sha512

        ctuple = namedtuple("named_user", "email key")
        user_tuple = ctuple("[email protected]", sha512("key").hexdigest())

        def delete(fles, user):
            self.assertEqual(user.email, "[email protected]")
            self.assertEqual(user, user_tuple)

        monkey_patcher = MonkeyPatcher((user.User, "delete", delete))
        monkey_patcher.patch()

        url = "/delete/key"
        request = self.generate_request_and_session(url, auth=lambda: True, uid="73s7b33f")
        request.session.user = user_tuple
        request.session.expire = lambda: True

        result = yield self.account.render(request)

        self.assertEqual(result.code, http.OK)
        self.assertEqual(result.subject["success"], True)
开发者ID:PyMamba,项目名称:BlackMamba,代码行数:25,代码来源:test_controller_account.py

示例14: __init__

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
    def __init__(self, pool=None, testing=False):
        if pool is not None:
            self.pool = pool

        self.started = False
        self.__testing = testing

        if not self.zstorm_configured:
            provideUtility(global_zstorm, IZStorm)
            zstorm = getUtility(IZStorm)
            zstorm.set_default_uri('mamba', config.Database().uri)

        SQLite.register()
        MySQL.register()
        PostgreSQL.register()

        # MonkeyPatch Storm
        if not self.monkey_patched:
            monkey_patcher = MonkeyPatcher(
                (properties, 'PropertyColumn', PropertyColumnMambaPatch)
            )
            monkey_patcher.patch()
            self.monkey_patched = True
开发者ID:aroshni,项目名称:mamba,代码行数:25,代码来源:database.py

示例15: test_put_reannounceResetsTimer

# 需要导入模块: from twisted.python.monkey import MonkeyPatcher [as 别名]
# 或者: from twisted.python.monkey.MonkeyPatcher import patch [as 别名]
 def test_put_reannounceResetsTimer(self):
     # Replace the time function of the datastore module
     # so that we can artificially speed up time
     monkey_patcher = MonkeyPatcher()
     c = clock()
     c.set(0)
     monkey_patcher.addPatch(datastore, "time", c)
     # Replace the peer_timeout to 5 seconds
     monkey_patcher.addPatch(constants, "peer_timeout", 5)
     monkey_patcher.patch()
     # Insert a node and verify it is within the datastore
     m = self.datastore(self.reactor)
     infohash = 5
     expected_peer = ("127.0.0.1", 5151)
     m.put(infohash, expected_peer)
     peers = m.get(infohash)
     # Iterate over a 1 element list
     self.assertEquals(1, len(peers))
     for peer in peers:
         self.assertEqual(expected_peer, peer)
     # Change the time and reannounce the peer
     # (make sure the cleanup function doesnt
     #  remove the peer yet)
     c.set(4)
     m.put(infohash, expected_peer)
     peers = m.get(infohash)
     self.assertEqual(1, len(peers))
     m._cleanup(infohash, expected_peer)
     c.set(8)
     m._cleanup(infohash, expected_peer)
     peers = m.get(infohash)
     self.assertEqual(1, len(peers))
     c.set(9)
     m._cleanup(infohash, expected_peer)
     peers = m.get(infohash)
     self.assertEqual(0, len(peers))
     monkey_patcher.restore()
开发者ID:aburan28,项目名称:DHTBot,代码行数:39,代码来源:test_datastore.py


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