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


Python Mock.assert_called_once方法代码示例

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


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

示例1: test_write_script

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
def test_write_script(task, monkeypatch):
    mock_script_helper = Mock(name='IGVScriptHelper_instance',
                              spec=IGVScriptHelper)
    mock_script_helper_class = Mock(name='IGVScriptHelper',
                                    return_value=mock_script_helper)

    monkeypatch.setattr(paip.pipelines.report.take_igv_snapshots,
                        'IGVScriptHelper', mock_script_helper_class)

    script_path = '/path/to/script.txt'

    task.write_script(script_path=script_path)

    # Test how IGVScriptHelper class was initialized
    mock_script_helper_class.assert_called_once()
    init = mock_script_helper_class.call_args[1]
    init_tpl = init['template_data']
    assert init_tpl['sample_igv_snapshots_dir'].endswith('/igv_snapshots_PAT')
    assert init_tpl['cohort_variants'].endswith('.filt.geno_filt.vcf')
    assert init_tpl['sample_alignment'].endswith('.dupmarked_alignment.bam')
    assert init_tpl['sample_alignment_trackname'].endswith('.dupmarked_alignment.bam')
    assert init_tpl['sample_all_variants'].endswith('.with_filters.vcf')
    #assert init_tpl['sample_reportable_variants'].endswith('.reportable.vcf')
    #assert init_tpl['sample_reportable_variants'].endswith('.reportable.vcf')
    assert init['template_path'].endswith('igv_batch_template')
    assert init['variants_json'].endswith('report_data.min-cat-PAT.max-freq-1.json')

    # Test how the script helper was used to write the IGV script
    mock_script_helper.write_script.assert_called_once()
    out_path = mock_script_helper.write_script.call_args[1]['out_path']
    assert out_path == script_path
开发者ID:biocodices,项目名称:paip,代码行数:33,代码来源:test_take_igv_snapshots.py

示例2: TestComponent

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
class TestComponent(unittest.TestCase):

  def setUp(self):
    self.component_under_test = Listener()
    self.on_test_mock = Mock(return_value=None)
    self.on_second_event_mock = Mock(return_value=None)
    Component._emitter._events['test'] = [self.on_test_mock]
    Component._emitter._events['test_1'] = [self.on_test_mock, self.on_second_event_mock]

  def tearDown(self):
    self.on_test_mock.reset_mock()
    self.on_second_event_mock.reset_mock()

  def test_on_test_is_called(self):
    self.component_under_test.emit(Test())
    self.on_test_mock.assert_called_once()

  def test_with_multiple_decorators(self):
    self.component_under_test.emit(Test_1())
    self.component_under_test.emit(Test())
    self.assertEqual(self.on_test_mock.call_count, 2)

  def test_with_multiple_methods(self):
    self.component_under_test.emit(Test_1())
    self.assertEqual(self.on_test_mock.call_count, 1)
    self.assertEqual(self.on_second_event_mock.call_count, 1)

  def test_case_insentive_event(self):
    class test_1(Event):
      pass
    self.component_under_test.emit(test_1())
    self.assertEqual(self.on_second_event_mock.call_count, 1)
开发者ID:giuse88,项目名称:forex,代码行数:34,代码来源:test_component.py

示例3: testURL_patternMillisecondsZeroPadded

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
def testURL_patternMillisecondsZeroPadded():
    # Asserts solution to ticket #1954.
    # Milliseconds must be zero-padded in order to match URL lengths.
    now_mock = Mock(return_value=datetime.datetime(2019, 4, 19, 0, 0, 0, 4009))
    with patch('datetime.datetime', now=now_mock):
        s = Scraper('fd_%Y%m%d_%H%M%S_%e.fts')
    now_mock.assert_called_once()
    assert s.now == 'fd_20190419_000000_004.fts'
开发者ID:PritishC,项目名称:sunpy,代码行数:10,代码来源:test_scraper.py

示例4: test_quitter

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
def test_quitter(loop_count):
    quitter = testutil.Quitter(loop_count=loop_count)
    signal_mock = Mock()
    for i in range(loop_count):
        quitter.__event__(Idle(.01), signal_mock)
    signal_mock.assert_called_once()
    assert len(signal_mock.call_args[0]) == 1
    assert len(signal_mock.call_args[1]) == 0
    assert isinstance(signal_mock.call_args[0][0], Quit)
开发者ID:pathunstrom,项目名称:pursuedpybear,代码行数:11,代码来源:test_testutil.py

示例5: test_resolve

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
 def test_resolve(self):
     ph_io_loop = 'ioloop placeholder'
     resolver_obj = Mock()
     resolver_obj.resolve = MagicMock(return_value=gen.maybe_future(('a', 'b', 'c')))
     resolver = MagicMock(return_value=resolver_obj)
     patch('zookeeper_monitor.zk.host.Resolver', resolver).start()
     host = zk.Host('localhost', 2181)
     res = yield host._resolve(ph_io_loop)
     resolver.assert_called_once_with(io_loop=ph_io_loop)
     resolver_obj.assert_called_once()
     self.assertEqual(res, 'a')
开发者ID:robbwagoner,项目名称:zookeeper_monitor,代码行数:13,代码来源:test_host.py

示例6: TestEpochLambda

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
class TestEpochLambda(unittest.TestCase):
    def setUp(self):
        self._metric_function = Mock(return_value='test')
        self._metric = EpochLambda('test', self._metric_function, step_size=3)
        self._metric.reset({torchbearer.DEVICE: 'cpu', torchbearer.DATA_TYPE: torch.float32})
        self._states = [{torchbearer.BATCH: 0, torchbearer.Y_TRUE: torch.LongTensor([0]), torchbearer.Y_PRED: torch.FloatTensor([0.0]), torchbearer.DEVICE: 'cpu'},
                        {torchbearer.BATCH: 1, torchbearer.Y_TRUE: torch.LongTensor([1]), torchbearer.Y_PRED: torch.FloatTensor([0.1]), torchbearer.DEVICE: 'cpu'},
                        {torchbearer.BATCH: 2, torchbearer.Y_TRUE: torch.LongTensor([2]), torchbearer.Y_PRED: torch.FloatTensor([0.2]), torchbearer.DEVICE: 'cpu'},
                        {torchbearer.BATCH: 3, torchbearer.Y_TRUE: torch.LongTensor([3]), torchbearer.Y_PRED: torch.FloatTensor([0.3]), torchbearer.DEVICE: 'cpu'},
                        {torchbearer.BATCH: 4, torchbearer.Y_TRUE: torch.LongTensor([4]), torchbearer.Y_PRED: torch.FloatTensor([0.4]), torchbearer.DEVICE: 'cpu'}]

    def test_train(self):
        self._metric.train()
        calls = [[torch.FloatTensor([0.0]), torch.LongTensor([0])],
                 [torch.FloatTensor([0.0, 0.1, 0.2, 0.3]), torch.LongTensor([0, 1, 2, 3])]]
        for i in range(len(self._states)):
            self._metric.process(self._states[i])
        self.assertEqual(2, len(self._metric_function.call_args_list))
        for i in range(len(self._metric_function.call_args_list)):
            self.assertTrue(torch.eq(self._metric_function.call_args_list[i][0][0], calls[i][0]).all)
            self.assertTrue(torch.lt(torch.abs(torch.add(self._metric_function.call_args_list[i][0][1], -calls[i][1])), 1e-12).all)
        self._metric_function.reset_mock()
        self._metric.process_final({})

        self._metric_function.assert_called_once()
        self.assertTrue(torch.eq(self._metric_function.call_args_list[0][0][1], torch.LongTensor([0, 1, 2, 3, 4])).all)
        self.assertTrue(torch.lt(torch.abs(torch.add(self._metric_function.call_args_list[0][0][0], -torch.FloatTensor([0.0, 0.1, 0.2, 0.3, 0.4]))), 1e-12).all)

    def test_validate(self):
        self._metric.eval()
        for i in range(len(self._states)):
            self._metric.process(self._states[i])
        self._metric_function.assert_not_called()
        self._metric.process_final_validate({})

        self._metric_function.assert_called_once()
        self.assertTrue(torch.eq(self._metric_function.call_args_list[0][0][1], torch.LongTensor([0, 1, 2, 3, 4])).all)
        self.assertTrue(torch.lt(torch.abs(torch.add(self._metric_function.call_args_list[0][0][0], -torch.FloatTensor([0.0, 0.1, 0.2, 0.3, 0.4]))), 1e-12).all)

    def test_not_running(self):
        metric = EpochLambda('test', self._metric_function, running=False, step_size=6)
        metric.reset({torchbearer.DEVICE: 'cpu', torchbearer.DATA_TYPE: torch.float32})
        metric.train()

        for i in range(12):
            metric.process(self._states[0])

        self._metric_function.assert_not_called()
开发者ID:little1tow,项目名称:torchbearer,代码行数:50,代码来源:test_wrappers.py

示例7: test_do_callback

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
    def test_do_callback(self):

        callback_mock = Mock()

        # Setup - handler for Elec and fallback for the rest.
        parser = BaseTransport(device=self.device, callbacks={
            Elec: callback_mock,
            '*': _callback,

        })

        # create a valid elec packet to call with.
        elec_packet = bytearray(self.elec_packet)

        parser.do_callback(elec_packet)

        callback_mock.assert_called_once()
开发者ID:dennisvanderhave,项目名称:python-rfxcom,代码行数:19,代码来源:test_base.py

示例8: test_run

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
def test_run(task, monkeypatch):
    mock_makedirs = Mock(name='makedirs')
    monkeypatch.setattr(os, 'makedirs', mock_makedirs)
    monkeypatch.setattr(paip.pipelines.cnv_calling.VisualizeCNVs,
                        'copy_and_edit_R_script', Mock())

    task.run()

    mock_makedirs.assert_called_once()
    assert mock_makedirs.call_args[0][0].endswith('xhmm_run/plots')

    task.run_command.assert_called_once()

    (command, ), kwargs = task.run_command.call_args

    assert 'Rscript' in command
    assert command.endswith('make_XHMM_plots.R')
开发者ID:biocodices,项目名称:paip,代码行数:19,代码来源:test_visualize_cnvs.py

示例9: test_middleware_calls_get_response

# 需要导入模块: from unittest.mock import Mock [as 别名]
# 或者: from unittest.mock.Mock import assert_called_once [as 别名]
def test_middleware_calls_get_response(cache_versions, user):
    get_response = Mock()
    request = Mock(user=user, cache_versions=cache_versions)
    middleware = user_acl_middleware(get_response)
    middleware(request)
    get_response.assert_called_once()
开发者ID:rafalp,项目名称:Misago,代码行数:8,代码来源:test_user_acl_middleware.py


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