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


Python MagicMock.write方法代码示例

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


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

示例1: FileIO

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
    def FileIO(self, name, mode):
        """Proxy for tensorflow.python.lib.io.file_io.FileIO class. Mocks the class
        if a real GCS bucket is not available for testing.
        """
        self._check_started()
        if not self.mock_gcs:
            return tf_file_io.FileIO(name, mode)

        filepath = name
        if filepath.startswith(self._gcs_prefix):
            mock_fio = MagicMock()
            mock_fio.__enter__ = Mock(return_value=mock_fio)
            if mode == 'rb':
                if filepath not in self.local_objects:
                    raise IOError('{} does not exist'.format(filepath))
                self.local_objects[filepath].seek(0)
                mock_fio.read = self.local_objects[filepath].read
            elif mode == 'wb':
                self.local_objects[filepath] = BytesIO()
                mock_fio.write = self.local_objects[filepath].write
            else:
                raise ValueError(
                    '{} only supports wrapping of FileIO for `mode` "rb" or "wb"')
            return mock_fio

        return open(filepath, mode)
开发者ID:ZhangXinNan,项目名称:keras,代码行数:28,代码来源:test_utils.py

示例2: test_two_modules

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
    def test_two_modules(self):
        logger = MagicMock()
        handler2 = BusHandler(logger)
        handler1 = BusHandler(logger, out={'raw':[(handler2.queue, 42)]})

        logger.write = MagicMock(return_value=123)
        handler1.publish('raw', b"Hello!")

        self.assertEqual(handler2.listen(), (123, 42, b"Hello!"))
开发者ID:robotika,项目名称:osgar,代码行数:11,代码来源:test_bus.py

示例3: test_bad_terminal

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_bad_terminal(self):
     stdout = MagicMock()
     stdout.write = MagicMock(
         side_effect=[
             UnicodeEncodeError('codec', u'foo', 100, 105, 'artificial UnicodeEncodeError')
         ] + list(range(25))
     )
     sync_report = SyncReport(stdout, False)
     sync_report.print_completion('transferred: 123.txt')
开发者ID:Backblaze,项目名称:B2_Command_Line_Tool,代码行数:11,代码来源:test_sync_report.py

示例4: test_publish_status

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_publish_status(self):
     q = MagicMock()
     logger=MagicMock()
     logger.write = MagicMock(return_value=135)
     bus = BusHandler(logger=logger, out={'status':[(q, 'status'),], 'can':[]})
     spider = Spider(config={}, bus=bus)
     spider.can_bridge_initialized = True  # skip initialization
     self.assertEqual(CAN_packet(0x200, [0, 0x80]), b'@\x02\x00\x80')
     bus.queue.put((123, 'raw', b'@\x02\x00\x80'))
     bus.shutdown()
     spider.run()
     q.put.assert_called_once_with((135, 'status', ([0x8000, None])))
开发者ID:robotika,项目名称:osgar,代码行数:14,代码来源:test_spider.py

示例5: test_sync

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_sync(self):
     q = MagicMock()
     logger = MagicMock()
     logger.write = MagicMock(return_value=135)
     bus = BusHandler(logger=logger,
             out={'can': [], 'encoders': [], 'emergency_stop': [],
                  'pose2d': [(q, 'pose2d'),], 'buttons': []})
     eduro = Eduro(config={}, bus=bus)
     sync = CAN_packet(0x80, [])
     bus.queue.put((123, 'can', sync))
     bus.shutdown()
     eduro.run()
     q.put.assert_called_once_with((135, 'pose2d', [0, 0, 0]))
开发者ID:robotika,项目名称:osgar,代码行数:15,代码来源:test_eduro.py

示例6: test_buttons

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_buttons(self):
     # is there a simpler way without starting the Thread??
     q = MagicMock()
     logger = MagicMock()
     logger.write = MagicMock(return_value=42)
     bus = BusHandler(logger=logger,
             out={'can': [], 'encoders': [], 'emergency_stop': [],
                  'pose2d': [], 'buttons': [(q, 'buttons')]})
     eduro = Eduro(config={}, bus=bus)
     buttons_msg = CAN_packet(0x28A, [0, 0])
     bus.queue.put((42, 'can', buttons_msg))
     bus.shutdown()
     eduro.run()
     q.put.assert_called_once_with((42, 'buttons', {'blue_selected': True, 'cable_in': False}))
开发者ID:robotika,项目名称:osgar,代码行数:16,代码来源:test_eduro.py

示例7: test_usage

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_usage(self):
     q = MagicMock()
     logger = MagicMock()
     logger.write = MagicMock(return_value=135)
     bus = BusHandler(logger=logger,
             out={'raw': [(q, 'raw')], 'encoders': [], 'emergency_stop': [],
                  'pose2d': [], 'buttons': []})
     robot = Cortexpilot(config={}, bus=bus)
     robot.start()
     robot.request_stop()
     robot.join()
     # at first request firmware version
     q.put.assert_called_once_with((135, 'raw', 
         b'\x00\x00\x03\x01\x01\xfb'))
开发者ID:robotika,项目名称:osgar,代码行数:16,代码来源:test_cortexpilot.py

示例8: test_2nd_loop

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
    def test_2nd_loop(self):
        q = MagicMock()
        logger = MagicMock()
        logger.write = MagicMock(return_value=135)
        bus = BusHandler(logger=logger,
                out={'raw': [(q, 'raw')], 'encoders': [], 'emergency_stop': [],
                     'pose2d': [], 'buttons': []})
        robot = Cortexpilot(config={}, bus=bus)
        bus.queue.put((123, 'raw', b'\x00\x00\x10\x01\x01Robik V4.0.2\x00\x8f'))
        robot.start()
        robot.request_stop()
        robot.join()

        self.assertEqual(q.put.call_args_list, [
            call((135, 'raw', b'\x00\x00\x03\x01\x01\xfb')),  # request version
            call((135, 'raw', bytes.fromhex('00000f010d000000000000000040010000a2'))) # cmd
            ])
开发者ID:robotika,项目名称:osgar,代码行数:19,代码来源:test_cortexpilot.py

示例9: test_instantiate

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
def test_instantiate(monkeypatch, fake_relay_actor):
    fake_serial = MagicMock()
    write_mock = Mock()
    fake_serial.write = write_mock
    # fake_serial.__init__ = Mock()
    monkeypatch.setattr("batterytester.components.actors.relay_actor.Serial",
                        fake_serial)

    async def setup():
        await fake_relay_actor.setup(None, None)

    loop = asyncio.get_event_loop()
    loop.run_until_complete(setup())
    fake_serial.assert_called_once_with(port=MOCK_SERIAL_PORT,
                                        baudrate=fake_relay_actor._speed)

    async def activate():
        await fake_relay_actor.activate(pin=2)

    loop.run_until_complete(activate())
    fake_relay_actor._serial.write.assert_called_once_with(
        '{a:02:01}\n'.encode('utf-8'))
开发者ID:sander76,项目名称:batterytester,代码行数:24,代码来源:test_relay_actor.py

示例10: test_encoders_overflow

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
    def test_encoders_overflow(self):
        q = MagicMock()
        logger = MagicMock()
        logger.write = MagicMock(return_value=22)
        bus = BusHandler(logger=logger,
                out={'can': [], 'encoders': [(q, 'encoders')], 'emergency_stop': [],
                     'pose2d': [], 'buttons': []})
        eduro = Eduro(config={}, bus=bus)
        sync = CAN_packet(0x80, [])

        enc_left = CAN_packet(0x181, [0xff, 0xff, 0xff, 0x7f])
        bus.queue.put((42, 'can', enc_left))
        bus.queue.put((123, 'can', sync))

        enc_left = CAN_packet(0x181, [0x01, 0x00, 0x00, 0x80])
        bus.queue.put((44, 'can', enc_left))
        sync = CAN_packet(0x80, [])
        bus.queue.put((123, 'can', sync))

        bus.shutdown()
        eduro.run()
        self.assertEqual(q.put.call_args_list, [call((22, 'encoders', [0, 0])),
                                                call((22, 'encoders', [2, 0]))])
开发者ID:robotika,项目名称:osgar,代码行数:25,代码来源:test_eduro.py

示例11: test_publish_time

# 需要导入模块: from unittest.mock import MagicMock [as 别名]
# 或者: from unittest.mock.MagicMock import write [as 别名]
 def test_publish_time(self):
     logger = MagicMock()
     logger.register = MagicMock(return_value=1)
     logger.write = MagicMock(return_value=timedelta(123))
     bus = BusHandler(logger, out={'stdout':[]})
     self.assertEqual(bus.publish('stdout', 'hello world!'), timedelta(123))
开发者ID:robotika,项目名称:osgar,代码行数:8,代码来源:test_bus.py


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