How to use the mock.MagicMock function in mock

To help you get started, we’ve selected a few mock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack-charmers / zaza / unit_tests / utilities / test_zaza_utilities_openstack.py View on Github external
def test__resource_reaches_status_bespoke(self):
        resource_mock = mock.MagicMock()
        resource_mock.get.return_value = mock.MagicMock(status='readyish')
        openstack_utils._resource_reaches_status(
            resource_mock,
            'e01df65a',
            'readyish')
github softlayer / jumpgate / tests / jumpgate-tests / compute / test_extra_specs.py View on Github external
def perform_extra_detail_key(self, tenant_id, flavor_id, key_id):
        env = get_client_env()
        self.req = falcon.Request(env)
        self.resp = falcon.Response()
        flavors = flavor_list_loader.Flavors.get_flavors(app=mock.MagicMock())
        instance = extra_specs.ExtraSpecsFlavorKeyV2(app=mock.MagicMock(),
                                                     flavors=flavors)
        instance.on_get(self.req, self.resp, tenant_id, flavor_id, key_id)
github koji-project / koji / tests / test_plugins / test_save_failed_tree_builder.py View on Github external
def testNonExistentBuildroot(self, tarfile, os_unlink):
        tfile = mock.MagicMock(name='tfile')
        tarfile.return_value = tfile
        self.session.getBuildroot.return_value = None

        with self.assertRaises(koji.GenericError) as cm:
            self.t.handler(1)
        self.assertTrue('Nonexistent buildroot' in str(cm.exception))

        tarfile.assert_not_called()
        tfile.add.assert_not_called()
        tfile.close.assert_not_called()
        os_unlink.assert_not_called()
github zalando-zmon / zmon-worker / tests / test_elasticsearch.py View on Github external
def resp_mock(failure=False):
    resp = MagicMock()
    resp.ok = True if not failure else False
    json_res = {'hits': []} if not failure else {'error': {}, 'status': 400}
    resp.json.return_value = json_res

    return resp
github peplin / pygatt / tests / test_device.py View on Github external
def _subscribe(self):
        callback = MagicMock()
        with patch.object(self.device, 'char_write_handle') as char_write:
            self.device.subscribe(self.device.CHAR_UUID, callback=callback)
            ok_(char_write.called)
            eq_(self.device.EXPECTED_HANDLE + 1, char_write.call_args[0][0])
            eq_(bytearray([1, 0]), char_write.call_args[0][1])
        return callback
github horovod / horovod / test / test_run.py View on Github external
def test_mpi_run_on_large_cluster(self):
        if _get_mpi_implementation_flags() is None:
            self.skipTest("MPI is not available")

        cmd = ['cmd']
        settings = copy.copy(self.minimal_settings)
        settings.num_hosts = large_cluster_threshold
        run_func = MagicMock(return_value=0)

        mpi_run(settings, None, {}, cmd, run_func=run_func)

        mpi_flags = _get_mpi_implementation_flags()
        self.assertIsNotNone(mpi_flags)
        mpi_flags.append('-mca plm_rsh_no_tree_spawn true')
        mpi_flags.append('-mca plm_rsh_num_concurrent 2')
        expected_cmd = ('mpirun '
                        '--allow-run-as-root --tag-output '
                        '-np 2 -H host '
                        '-bind-to none -map-by slot '
                        '{mpi_flags}       '
                        'cmd').format(mpi_flags=' '.join(mpi_flags))
        expected_env = {}
        run_func.assert_called_once_with(command=expected_cmd, env=expected_env, stdout=None, stderr=None)
github jaysw / ipydb / tests / test_plugin.py View on Github external
'host': 'zing',
                'database': 'db'
            }
        }
        self.mengine.getconfigs.return_value = ('con1', configs)
        self.mock_db_url = 'mysql://barry:xyz@zing.com/db'
        self.mengine.make_connection_url.return_value = self.mock_db_url
        self.sa_engine = mock.MagicMock()
        self.sa_engine.url.host = 'zing.com'
        self.sa_engine.url.database = 'db'
        self.mengine.from_url.return_value = self.sa_engine
        plugin.SqlPlugin.metadata_accessor = self.md_accessor
        self.ipython = mock.MagicMock(spec=TerminalInteractiveShell)
        self.ipython.config = None
        self.ipython.register_magics = mock.MagicMock()
        self.ipython.Completer = mock.MagicMock()
        self.ip = plugin.SqlPlugin(shell=self.ipython)
github koji-project / koji / tests / test_plugins / test_runroot_builder.py View on Github external
def test_bad_path_sub(self, safe_config_parser):
        session = mock.MagicMock()
        options = mock.MagicMock()
        options.workdir = '/tmp/nonexistentdirectory'
        config = copy.deepcopy(CONFIG2)
        config['paths']['path_subs'] += 'incorrect:format'
        safe_config_parser.return_value = FakeConfigParser(config)
        with self.assertRaises(koji.GenericError):
            runroot.RunRootTask(123, 'runroot', {}, session, options)
github glue-viz / glue / tests / test_data.py View on Github external
def test_primary_components(self):
        compid = glue.core.data.ComponentID('virtual')
        link = MagicMock(spec_set = glue.core.component_link.ComponentLink)
        comp = glue.core.data.DerivedComponent(self.data, link)

        self.data.add_component(comp, compid)

        pricomps = self.data.primary_components
        self.assertIn(self.comp_id, pricomps)
        self.assertNotIn(compid, pricomps)
github man-group / PyBloqs / tests / unit / block / test_table.py View on Github external
def test_HTMLJinjaTableBlock_insert_additional_html_concat():
    dummy_html1 = '<style></style>'
    formatter1 = abtf.TableFormatter()
    formatter1.insert_additional_html = MagicMock(return_value=dummy_html1)
    dummy_html2 = '<div></div>'
    formatter2 = abtf.TableFormatter()
    formatter2.insert_additional_html = MagicMock(return_value=dummy_html2)
    table = abt.HTMLJinjaTableBlock(df, formatters=[formatter1, formatter2], use_default_formatters=False)
    res = table.insert_additional_html()
    assert res == dummy_html1 + dummy_html2