Python mock.mock_open() Examples
The following are 30
code examples of mock.mock_open().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
mock
, or try the search function
.
Example #1
Source File: test_uflash.py From uflash with MIT License | 7 votes |
def test_flash_with_path_to_runtime(): """ Use the referenced runtime hex file when building the hex file to be flashed onto the device. """ mock_o = mock.mock_open(read_data=b'script') with mock.patch.object(builtins, 'open', mock_o) as mock_open: with mock.patch('uflash.embed_hex', return_value='foo') as em_h: with mock.patch('uflash.find_microbit', return_value='bar'): with mock.patch('uflash.save_hex') as mock_save: uflash.flash('tests/example.py', path_to_runtime='tests/fake.hex') assert mock_open.call_args[0][0] == 'tests/fake.hex' assert em_h.call_args[0][0] == b'script' expected_hex_path = os.path.join('bar', 'micropython.hex') mock_save.assert_called_once_with('foo', expected_hex_path)
Example #2
Source File: test_repokid_cli.py From repokid with Apache License 2.0 | 7 votes |
def test_remove_permissions_from_roles(self, mock_hooks): import json cli = repokid.cli.repokid_cli arns = [role["Arn"] for role in ROLES] arns = json.dumps(arns) class Hooks: def call_hooks(hooks, tag, role_dict): assert tag == "AFTER_REPO" mock_hooks = Hooks() with patch("builtins.open", mock_open(read_data=arns)) as mock_file: assert open("somefile.json").read() == arns mock_file.assert_called_with("somefile.json") cli.remove_permissions_from_roles( ["s3:putobjectacl"], "somefile.json", None, None, mock_hooks, commit=False, )
Example #3
Source File: fuzzer_stats_test.py From clusterfuzz with Apache License 2.0 | 6 votes |
def test_testcase_run_read_from_disk(self, mock_path_exists): """Tests TestcaseRun deserialization.""" read_data = ('{"stat": 1000, "timestamp": 1472846341.017923, ' '"kind": "TestcaseRun", "job": "job", "fuzzer": "fuzzer", ' '"build_revision": 123}') mock_path_exists.return_value = True m = mock.mock_open(read_data=read_data) with mock.patch('metrics.fuzzer_stats.open', m): testcase_run = fuzzer_stats.TestcaseRun.read_from_disk('fake_path') self.assertIsNotNone(testcase_run) self.assertEqual(testcase_run.kind, 'TestcaseRun') self.assertEqual(testcase_run.fuzzer, 'fuzzer') self.assertEqual(testcase_run.job, 'job') self.assertEqual(testcase_run.build_revision, 123) self.assertEqual(testcase_run.timestamp, 1472846341.017923) self.assertEqual(testcase_run['stat'], 1000)
Example #4
Source File: api_cache_test.py From threat_intel with MIT License | 6 votes |
def _open_cache(self, initial_contents=None, update_cache=True): """Creates an ApiCache object, mocking the contents of the cache on disk. Args: initial_contents: A dict containing the initial contents of the cache update_cache: Specifies whether ApiCache should write out the cache file when closing it Returns: ApiCache """ if not initial_contents: initial_contents = {} file_contents = simplejson.dumps(initial_contents) mock_read = mock_open(read_data=file_contents) with patch.object(builtins, 'open', mock_read, create=True): api_cache = ApiCache(self._file_name, update_cache=update_cache) return api_cache
Example #5
Source File: test_hints.py From flashre with GNU Lesser General Public License v3.0 | 6 votes |
def test_load_hints(self): """ Test the load_hints() function """ from flashre.hints import load_hints from flashre.binaries_helpers import ReverseFlashairBinary # Build a fake ReverseFlashairBinary object # Note: get_r2pipe() could be mocked too! rfb = ReverseFlashairBinary("", 0) rfb.strings = mock.MagicMock(return_value=list()) # Some bad inputs bad_inputs = list() bad_inputs.append("x\n") bad_inputs.append("x:\n") bad_inputs.append(" 2dc6e0: d0 01 mov $1,$tp\n") bad_inputs.append(" 100: 00 70 di\n") # Mock the open() builtin m_open = mock.mock_open() m_open.return_value = bad_inputs with mock.patch("flashre.hints.open", m_open): assert load_hints(rfb, "") is None
Example #6
Source File: opendns_test.py From threat_intel with MIT License | 6 votes |
def test_categorization_response_error(self): """Tests whether the ResponseError is raised when the response returned from the actual API call is empty. """ domains = ['yosemite.gov', 'joushuatree.gov', 'deathvalley.gov'] # empty responses should raise an error all_responses = [{}] # mock cache file mock_read = mock_open(read_data="{}") with patch.object( builtins, 'open', mock_read, create=True ), patch.object( ApiCache, 'bulk_lookup', autospec=True, return_value={} ), patch.object( MultiRequest, 'multi_post', autospec=True, return_value=all_responses ): i = InvestigateApi('hocus pocus', 'cache.json') with T.assert_raises(ResponseError): i.categorization(domains)
Example #7
Source File: api_cache_test.py From threat_intel with MIT License | 6 votes |
def _close_cache(self, api_cache, cache_written=True): """Closes an ApiCache and reads the final contents that were written to disk. Args: api_cache: An ApiCache instance cache_written: Specifies whether it should test that the cache was written out to the cache file or whether to test that it was not written out Returns: A dict representing the contents of the cache that was written out to the cache file or `None` in case cache was not expected to be written out """ mock_write = mock_open() with patch.object(builtins, 'open', mock_write, create=True) as patched_open: api_cache.close() if cache_written: return assert_cache_written(mock_write, patched_open) return assert_cache_not_written(mock_write)
Example #8
Source File: report.py From ripe-atlas-tools with GNU General Public License v3.0 | 6 votes |
def test_user_agent(self): standard = "RIPE Atlas Tools (Magellan) {}".format(__version__) tests = { standard: standard, "Some custom agent": "Some custom agent", "Some custom agent\nwith a second line": "Some custom agent", "x" * 3000: "x" * 128, "Πράκτορας χρήστη": "Πράκτορας χρήστη", "이것은 테스트 요원": "이것은 테스트 요원", } self.assertEqual(self.cmd.user_agent, standard) for in_string, out_string in tests.items(): path = "ripe.atlas.tools.commands.base.open" content = mock.mock_open(read_data=in_string) with mock.patch(path, content, create=True): self.assertEqual(Command().user_agent, out_string)
Example #9
Source File: test_util.py From pex with Apache License 2.0 | 6 votes |
def test_access_zipped_assets( mock_resource_string, mock_resource_isdir, mock_resource_listdir, mock_safe_mkdir, mock_safe_mkdtemp): mock_open = mock.mock_open() mock_safe_mkdtemp.side_effect = iter(['tmpJIMMEH', 'faketmpDir']) mock_resource_listdir.side_effect = iter([['./__init__.py', './directory/'], ['file.py']]) mock_resource_isdir.side_effect = iter([False, True, False]) mock_resource_string.return_value = 'testing' with mock.patch('%s.open' % python_builtins.__name__, mock_open, create=True): temp_dir = DistributionHelper.access_zipped_assets('twitter.common', 'dirutil') assert mock_resource_listdir.call_count == 2 assert mock_open.call_count == 2 file_handle = mock_open.return_value.__enter__.return_value assert file_handle.write.call_count == 2 assert mock_safe_mkdtemp.mock_calls == [mock.call()] assert temp_dir == 'tmpJIMMEH' assert mock_safe_mkdir.mock_calls == [mock.call(os.path.join('tmpJIMMEH', 'directory'))]
Example #10
Source File: test_session.py From sagemaker-python-sdk with Apache License 2.0 | 6 votes |
def test_user_agent_injected_with_nbi(boto_session): assert ( "AWS-SageMaker-Python-SDK" not in boto_session.client("sagemaker")._client_config.user_agent ) with patch("six.moves.builtins.open", mock_open(read_data="120.0-0")) as mo: sess = Session(boto_session) mo.assert_called_with("/etc/opt/ml/sagemaker-notebook-instance-version.txt") assert "AWS-SageMaker-Python-SDK" in sess.sagemaker_client._client_config.user_agent assert "AWS-SageMaker-Python-SDK" in sess.sagemaker_runtime_client._client_config.user_agent assert "AWS-SageMaker-Notebook-Instance" in sess.sagemaker_client._client_config.user_agent assert ( "AWS-SageMaker-Notebook-Instance" in sess.sagemaker_runtime_client._client_config.user_agent )
Example #11
Source File: test_default_inference_handler.py From sagemaker-mxnet-inference-toolkit with Apache License 2.0 | 6 votes |
def test_default_model_fn(path_exists, mx_load_checkpoint, mx_module, mx_cpu): sym = Mock() args = Mock() aux = Mock() mx_load_checkpoint.return_value = [sym, args, aux] mx_context = Mock() mx_cpu.return_value = mx_context data_name = 'foo' data_shape = [1] signature = json.dumps([{'name': data_name, 'shape': data_shape}]) with patch('six.moves.builtins.open', mock_open(read_data=signature)): DefaultMXNetInferenceHandler().default_model_fn(MODEL_DIR) mx_load_checkpoint.assert_called_with(os.path.join(MODEL_DIR, 'model'), 0) init_call = call(symbol=sym, context=mx_context, data_names=[data_name], label_names=None) assert init_call in mx_module.mock_calls model = mx_module.return_value model.bind.assert_called_with(for_training=False, data_shapes=[(data_name, data_shape)]) model.set_params.assert_called_with(args, aux, allow_missing=True)
Example #12
Source File: test_default_inference_handler.py From sagemaker-mxnet-inference-toolkit with Apache License 2.0 | 6 votes |
def test_default_model_fn_with_accelerator(path_exists, mx_load_checkpoint, mx_module, mx_eia): sym = Mock() args = Mock() aux = Mock() mx_load_checkpoint.return_value = [sym, args, aux] eia_context = Mock() mx_eia.return_value = eia_context data_name = 'foo' data_shape = [1] signature = json.dumps([{'name': data_name, 'shape': data_shape}]) with patch('six.moves.builtins.open', mock_open(read_data=signature)): DefaultMXNetInferenceHandler().default_model_fn(MODEL_DIR) mx_load_checkpoint.assert_called_with(os.path.join(MODEL_DIR, 'model'), 0) init_call = call(symbol=sym, context=eia_context, data_names=[data_name], label_names=None) assert init_call in mx_module.mock_calls model = mx_module.return_value model.bind.assert_called_with(for_training=False, data_shapes=[(data_name, data_shape)]) model.set_params.assert_called_with(args, aux, allow_missing=True)
Example #13
Source File: test_ssh.py From pyinfra with MIT License | 6 votes |
def test_put_file(self, fake_sftp_client, fake_ssh_client): inventory = make_inventory(hosts=('anotherhost',)) state = State(inventory, Config()) host = inventory.get_host('anotherhost') host.connect(state) fake_open = mock_open(read_data='test!') with patch('pyinfra.api.util.open', fake_open, create=True): status = host.put_file( state, 'not-a-file', 'not-another-file', print_output=True, ) assert status is True fake_sftp_client.from_transport().putfo.assert_called_with( fake_open(), 'not-another-file', )
Example #14
Source File: test_ssh.py From pyinfra with MIT License | 6 votes |
def test_get_file(self, fake_sftp_client, fake_ssh_client): inventory = make_inventory(hosts=('somehost',)) state = State(inventory, Config()) host = inventory.get_host('somehost') host.connect(state) fake_open = mock_open(read_data='test!') with patch('pyinfra.api.util.open', fake_open, create=True): status = host.get_file( state, 'not-a-file', 'not-another-file', print_output=True, ) assert status is True fake_sftp_client.from_transport().getfo.assert_called_with( 'not-a-file', fake_open(), )
Example #15
Source File: test_osinfo.py From agentless-system-crawler with Apache License 2.0 | 6 votes |
def test_get_osinfo_from_os_release(self): data = ['NAME="Ubuntu"', 'VERSION="14.04.4 LTS, Trusty Tahr"', 'ID=ubuntu', 'ID_LIKE=debian', 'PRETTY_NAME="Ubuntu 14.04.4 LTS"', 'VERSION_ID="14.04"', 'HOME_URL="http://www.ubuntu.com/"', 'SUPPORT_URL="http://help.ubuntu.com/"', 'BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"' ] with mock.patch( '__builtin__.open', mock.mock_open(read_data="\n".join(data)), create=True) as m: m.return_value.__iter__.return_value = data result = get_osinfo_from_os_release() self.assertEqual(result['os'], 'ubuntu') self.assertEqual(result['version'], '14.04')
Example #16
Source File: zap_helper_test.py From zap-cli with MIT License | 6 votes |
def test_start_extra_options(self, isfile_mock, popen_mock, platform_mock): """Test starting the ZAP daemon with extra commandline options.""" def is_running_result(): """Used to mock the result of ZAPHelper.is_running.""" if self.zap_helper.is_running.call_count > 1: return True return False self.zap_helper.is_running = Mock(side_effect=is_running_result) platform_mock.return_value = 'Linux' isfile_mock.return_value = True extra_options = '-config api.key=12345 -config connection.timeoutInSecs=60' file_open_mock = mock_open() with patch('zapcli.zap_helper.open', file_open_mock, create=True): self.zap_helper.start(options=extra_options) expected_command = shlex.split('zap.sh -daemon -port 8090 {0}'.format(extra_options)) popen_mock.assert_called_with(expected_command, cwd='', stderr=subprocess.STDOUT, stdout=file_open_mock())
Example #17
Source File: zap_helper_test.py From zap-cli with MIT License | 6 votes |
def test_start_successful(self, platform_string, executable, isfile_mock, popen_mock, platform_mock): """Test starting the ZAP daemon.""" def is_running_result(): """Used to mock the result of ZAPHelper.is_running.""" if self.zap_helper.is_running.call_count > 1: return True return False self.zap_helper.is_running = Mock(side_effect=is_running_result) platform_mock.return_value = platform_string isfile_mock.return_value = True file_open_mock = mock_open() with patch('zapcli.zap_helper.open', file_open_mock, create=True): self.zap_helper.start() expected_command = shlex.split('{} -daemon -port 8090'.format(executable)) popen_mock.assert_called_with(expected_command, cwd='', stderr=subprocess.STDOUT, stdout=file_open_mock())
Example #18
Source File: testwith.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_readline_data(self): # Check that readline will return all the lines from the fake file mock = mock_open(read_data='foo\nbar\nbaz\n') with patch('%s.open' % __name__, mock, create=True): h = open('bar') line1 = h.readline() line2 = h.readline() line3 = h.readline() self.assertEqual(line1, 'foo\n') self.assertEqual(line2, 'bar\n') self.assertEqual(line3, 'baz\n') # Check that we properly emulate a file that doesn't end in a newline mock = mock_open(read_data='foo') with patch('%s.open' % __name__, mock, create=True): h = open('bar') result = h.readline() self.assertEqual(result, 'foo')
Example #19
Source File: testwith.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_readlines_data(self): # Test that emulating a file that ends in a newline character works mock = mock_open(read_data='foo\nbar\nbaz\n') with patch('%s.open' % __name__, mock, create=True): h = open('bar') result = h.readlines() self.assertEqual(result, ['foo\n', 'bar\n', 'baz\n']) # Test that files without a final newline will also be correctly # emulated mock = mock_open(read_data='foo\nbar\nbaz') with patch('%s.open' % __name__, mock, create=True): h = open('bar') result = h.readlines() self.assertEqual(result, ['foo\n', 'bar\n', 'baz'])
Example #20
Source File: test_hive.py From marvin-python-toolbox with Apache License 2.0 | 6 votes |
def test_hive_generateconf_write_file_with_json(mocked_json): default_conf = [{ "origin_host": "xxx_host_name", "origin_db": "xxx_db_name", "origin_queue": "marvin", "target_table_name": "xxx_table_name", "sample_sql": "SELECT * FROM XXX", "sql_id": "1" }] mocked_open = mock.mock_open() with mock.patch('marvin_python_toolbox.management.hive.open', mocked_open, create=True): hive.hive_generateconf(None) mocked_open.assert_called_once_with('hive_dataimport.conf', 'w') mocked_json.dump.assert_called_once_with(default_conf, mocked_open(), indent=2)
Example #21
Source File: test_engine_base_action.py From marvin-python-toolbox with Apache License 2.0 | 6 votes |
def test__serializer_dump_metrics(self, mocked_dump): obj = {"key", 1} object_reference = "_metrics" class _EAction(EngineBaseAction): _metrics = None def execute(self, params, **kwargs): pass mocked_open = mock.mock_open() with mock.patch('marvin_python_toolbox.engine_base.engine_base_action.open', mocked_open, create=False): _EAction(default_root_path="/tmp/.marvin", persistence_mode="local")._save_obj(object_reference, obj) mocked_dump.assert_called_once_with(obj, ANY, indent=4, separators=(u',', u': '), sort_keys=True) mocked_open.assert_called_once()
Example #22
Source File: testwith.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def test_interleaved_reads(self): # Test that calling read, readline, and readlines pulls data # sequentially from the data we preload with mock = mock_open(read_data='foo\nbar\nbaz\n') with patch('%s.open' % __name__, mock, create=True): h = open('bar') line1 = h.readline() rest = h.readlines() self.assertEqual(line1, 'foo\n') self.assertEqual(rest, ['bar\n', 'baz\n']) mock = mock_open(read_data='foo\nbar\nbaz\n') with patch('%s.open' % __name__, mock, create=True): h = open('bar') line1 = h.readline() rest = h.read() self.assertEqual(line1, 'foo\n') self.assertEqual(rest, 'bar\nbaz\n')
Example #23
Source File: test_config_client.py From orion-server with MIT License | 6 votes |
def test_all_required_in_env(self, mock_open): os.environ.update({ 'DATABASE_HOST': 'env_localhost', 'DATABASE_PORT': '3306', 'DATABASE_NAME': 'env_orion', 'DATABASE_USER': 'env_orion', 'DATABASE_PASSWORD': 'env_password', }) instance = ConfigClient() self.assertFalse(mock_open.called) self.assertEqual(instance.get_value('database.host'), 'env_localhost') self.assertEqual(instance.get_value('database.port'), 3306) self.assertEqual(instance.get_value('database.name'), 'env_orion') self.assertEqual(instance.get_value('database.user'), 'env_orion') self.assertEqual(instance.get_value('database.password'), 'env_password') self.assertEqual(instance.get_value('frontend_url'), '*')
Example #24
Source File: test_uflash.py From uflash with MIT License | 6 votes |
def test_extract_paths(): """ Test the different paths of the extract() function. It should open and extract the contents of the file (input arg) When called with only an input it should print the output of extract_script When called with two arguments it should write the output to the output arg """ mock_e = mock.MagicMock(return_value=b'print("hello, world!")') mock_o = mock.mock_open(read_data='script') with mock.patch('uflash.extract_script', mock_e) as mock_extract_script, \ mock.patch.object(builtins, 'print') as mock_print, \ mock.patch.object(builtins, 'open', mock_o) as mock_open: uflash.extract('foo.hex') mock_open.assert_called_once_with('foo.hex', 'r') mock_extract_script.assert_called_once_with('script') mock_print.assert_called_once_with(b'print("hello, world!")') uflash.extract('foo.hex', 'out.py') assert mock_open.call_count == 3 mock_open.assert_called_with('out.py', 'w') assert mock_open.return_value.write.call_count == 1
Example #25
Source File: test_parse.py From tldr.py with MIT License | 6 votes |
def _assert_mock_read(self, page_content): mock_config = { 'colors': { 'command': 'cyan', 'description': 'blue', 'usage': 'green' }, 'platform': 'linux', 'repo_directory': '/tmp/tldr' } expected_result = ( '\n' '\x1b[0m\x1b[34m Main node command\n' '\x1b[0m\n\x1b[0m\x1b[32m- Call an interactive node shell' '\n\x1b[0m\n\x1b[0m\x1b[36m node\n' '\x1b[0m\n\x1b[0m\x1b[32m- Execute node on a JS file\n' '\x1b[0m\n\x1b[0m\x1b[36m node {{FILENAME}}.js\n' '\x1b[0m\n\x1b[0m' ) with mock.patch('tldr.parser.get_config', return_value=mock_config): with mock.patch('io.open', mock.mock_open(read_data=page_content)): result = parse_page('/repo_directory/pages/common/node.md') assert ''.join(result) == expected_result
Example #26
Source File: test_data.py From marvin-python-toolbox with Apache License 2.0 | 5 votes |
def test_load_data_from_filesystem_exception(data_path_key, data_path): with mock.patch('marvin_python_toolbox.common.data.open') as mock_open: mock_open.side_effect = IOError # load_data should propagate IOError with pytest.raises(IOError): MarvinData.load_data(os.path.join('named_features', 'brands.json'))
Example #27
Source File: test_vault_client.py From airflow with Apache License 2.0 | 5 votes |
def test_kubernetes_default_path(self, mock_hvac): mock_client = mock.MagicMock() mock_hvac.Client.return_value = mock_client vault_client = _VaultClient(auth_type="kubernetes", kubernetes_role="kube_role", url="http://localhost:8180") with patch("builtins.open", mock_open(read_data="data")) as mock_file: client = vault_client.client mock_file.assert_called_with("/var/run/secrets/kubernetes.io/serviceaccount/token") mock_hvac.Client.assert_called_with(url='http://localhost:8180') client.auth_kubernetes.assert_called_with( role="kube_role", jwt="data") client.is_authenticated.assert_called_with() self.assertEqual(2, vault_client.kv_engine_version)
Example #28
Source File: test_hive.py From marvin-python-toolbox with Apache License 2.0 | 5 votes |
def test_read_config_with_not_existing_path(path_mocked, json_mocked): path_mocked.exists.return_value = False path_mocked.join.return_value = 'test.conf' mocked_open = mock.mock_open() with mock.patch('marvin_python_toolbox.management.hive.open', mocked_open, create=True): hive.read_config("test.conf") mocked_open.assert_not_called() json_mocked.load.assert_not_called()
Example #29
Source File: test_hive.py From marvin-python-toolbox with Apache License 2.0 | 5 votes |
def test_read_config_with_existing_path(path_mocked, json_mocked): path_mocked.exists.return_value = True path_mocked.join.return_value = 'test.conf' mocked_open = mock.mock_open() with mock.patch('marvin_python_toolbox.management.hive.open', mocked_open, create=True): hive.read_config("test.conf") mocked_open.assert_called_once_with('test.conf', 'r') json_mocked.load.assert_called_once_with(mocked_open())
Example #30
Source File: test_uflash.py From uflash with MIT License | 5 votes |
def test_extract_command_source_only(): """ If there is no target file the extract command should write to stdout. """ mock_e = mock.MagicMock(return_value=b'print("hello, world!")') mock_o = mock.mock_open(read_data='script') with mock.patch('uflash.extract_script', mock_e) as mock_extract_script, \ mock.patch.object(builtins, 'open', mock_o) as mock_open, \ mock.patch.object(builtins, 'print') as mock_print: uflash.main(argv=['-e', 'hex.hex']) mock_open.assert_any_call('hex.hex', 'r') mock_extract_script.assert_called_once_with('script') mock_print.assert_any_call(b'print("hello, world!")')