Python mock.patch.object() Examples
The following are 30
code examples of mock.patch.object().
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.patch
, or try the search function
.
![](https://www.programcreek.com/common/static/images/search.png)
Example #1
Source File: test_completion_refresher.py From pgcli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_refresh_called_once(refresher): """ :param refresher: :return: """ callbacks = Mock() pgexecute = Mock() special = Mock() with patch.object(refresher, "_bg_refresh") as bg_refresh: actual = refresher.refresh(pgexecute, special, callbacks) time.sleep(1) # Wait for the thread to work. assert len(actual) == 1 assert len(actual[0]) == 4 assert actual[0][3] == "Auto-completion refresh started in the background." bg_refresh.assert_called_with(pgexecute, special, callbacks, None, None)
Example #2
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 #3
Source File: alexa_test.py From threat_intel with MIT License | 6 votes |
def _test_api_call( self, call, request, expected_query_params, api_response, expected_result): """ Tests a AlexaRankingApi call by mocking out the HTTP request. Args: call: Function in AlexaRankingApi to call. endpoint: Endpoint of AlexaRanking API that is hit. request: Call arguments. expected_query_params: Parameters that should be passed to API. api_response: The expected response by the API. expected_result: What the call should return. """ with patch.object(self.ar, '_requests') as request_mock: request_mock.multi_get.return_value = api_response result = call(request) request_mock.multi_get.assert_called_with( self.ar.BASE_URL, to_json=False, query_params=expected_query_params) T.assert_equal(result, expected_result)
Example #4
Source File: opendns_test.py From threat_intel with MIT License | 6 votes |
def _test_api_call_get(self, call, endpoint, request, expected_url_params, api_response, expected_result, expected_query_params=None): """ Tests a OpenDNS call by mocking out the HTTP GET request. Args: call: function in OpenDNSApi to call. endpoint: endpoint of OpenDNS API that is hit (appended to base url) request: call arguments expected_url_params: URL parameters that should be passed to API api_response: the expected response by the API expected_result: what call should return (given the api response provided) expected_query_params: query parameters that should be passed to API """ with patch.object(self.opendns, '_requests') as request_mock: request_mock.multi_get.return_value = api_response result = call(request) url = self.opendns._to_url(endpoint.format(expected_url_params)) request_mock.multi_get.assert_called_with([url], expected_query_params) T.assert_equal(result, expected_result)
Example #5
Source File: virustotal_test.py From threat_intel with MIT License | 6 votes |
def _test_api_call(self, call, endpoint, request, expected_query_params, api_response, expected_result): """ Tests a VirusTotalApi call by mocking out the HTTP request. Args: call: function in VirusTotalApi to call. endpoint: endpoint of VirusTotal API that is hit (appended to base url) request: call arguments expected_query_params: query parameters that should be passed to API api_response: the expected response by the API expected_result: what call should return (given the api response provided) """ with patch.object(self.vt, '_requests') as request_mock: request_mock.multi_get.return_value = api_response result = call(request) param_list = [self.vt.BASE_DOMAIN + endpoint.format(param) for param in expected_query_params] request_mock.multi_get.assert_called_with(param_list, file_download=ANY) T.assert_equal(result, expected_result)
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: test_notifications.py From django-herald with MIT License | 6 votes |
def test_sending_to_multiple_numbers(self): class TestNotification(TwilioTextNotification): from_number = '1231231234' template_name = 'hello_world' def get_recipients(self): return ['1234567890', '0987654321'] with patch.object(MessageList, 'create') as mocked_create: notification = TestNotification() notification.send() self.assertEqual(mocked_create.call_count, 2) for recipient in notification.get_recipients(): mocked_create.assert_any_call( body='Hello World', to=recipient, from_=notification.get_sent_from() )
Example #8
Source File: test_version_store.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_prune_previous_versions_retries_find_calls(library, fw_pointers_cfg): with FwPointersCtx(fw_pointers_cfg): original_next = pymongo.cursor.Cursor.next callers = set() def _next(*args, **kwargs): vs_caller_name = next(c for c in inspect.stack() if c[1].endswith('arctic/store/version_store.py'))[3] if vs_caller_name not in callers: callers.add(vs_caller_name) raise OperationFailure(0) else: return original_next(*args, **kwargs) library.write(symbol, ts1, prune_previous_version=False) library.write(symbol, ts2, prune_previous_version=False) with patch.object(pymongo.cursor.Cursor, "next", autospec=True, side_effect=_next): library._prune_previous_versions(symbol, keep_mins=0) assert mongo_count(library._versions, filter={'symbol': symbol}) == 1
Example #9
Source File: test_kubernetes.py From kubeshift with GNU Lesser General Public License v3.0 | 6 votes |
def setUp(self): self.config = Config(helper.TEST_CONFIG) patched_test_connection = patch.object(KubernetesClient, '_test_connection', side_effect=helper.test_connection) self.addCleanup(patched_test_connection.stop) self.mock_tc = patched_test_connection.start() patched_get_groups = patch.object(KubernetesClient, '_get_groups', side_effect=helper.get_groups) self.addCleanup(patched_get_groups.stop) self.mock_groups = patched_get_groups.start() patched_get_resources = patch.object(KubernetesClient, '_get_resources', side_effect=helper.get_resources) self.addCleanup(patched_get_resources.stop) self.mock_resources = patched_get_resources.start()
Example #10
Source File: test_arctic.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_ArcticLibraryBinding_db(): arctic = create_autospec(Arctic) arctic._conn = create_autospec(MongoClient) alb = ArcticLibraryBinding(arctic, "sentinel.library") with patch.object(alb, '_auth') as _auth: # connection is cached during __init__ alb._db assert _auth.call_count == 0 # Change the arctic connection arctic._conn = create_autospec(MongoClient) alb._db assert _auth.call_count == 1 # connection is still cached alb._db assert _auth.call_count == 1
Example #11
Source File: test_ts_read.py From arctic with GNU Lesser General Public License v2.1 | 6 votes |
def test_date_range_end_not_in_range(tickstore_lib): DUMMY_DATA = [ {'a': 1., 'b': 2., 'index': dt(2013, 1, 1, tzinfo=mktz('Europe/London')) }, {'b': 3., 'c': 4., 'index': dt(2013, 1, 2, 10, 1, tzinfo=mktz('Europe/London')) }, ] tickstore_lib._chunk_size = 1 tickstore_lib.write('SYM', DUMMY_DATA) with patch.object(tickstore_lib._collection, 'find', side_effect=tickstore_lib._collection.find) as f: df = tickstore_lib.read('SYM', date_range=DateRange(20130101, dt(2013, 1, 2, 9, 0)), columns=None) assert_array_equal(df['b'].values, np.array([2.])) assert mongo_count(tickstore_lib._collection, filter=f.call_args_list[-1][0][0]) == 1
Example #12
Source File: test_pgexecute.py From pgcli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_exit_without_active_connection(executor): quit_handler = MagicMock() pgspecial = PGSpecial() pgspecial.register( quit_handler, "\\q", "\\q", "Quit pgcli.", arg_type=NO_QUERY, case_sensitive=True, aliases=(":q",), ) with patch.object(executor, "conn", BrokenConnection()): # we should be able to quit the app, even without active connection run(executor, "\\q", pgspecial=pgspecial) quit_handler.assert_called_once() # an exception should be raised when running a query without active connection with pytest.raises(psycopg2.InterfaceError): run(executor, "select 1", pgspecial=pgspecial)
Example #13
Source File: test_git.py From smother with MIT License | 6 votes |
def test_git_diff(): with patch.object(git, 'execute') as mock: mock.return_value = '\n'.join(diff) result = git.git_diff() expected_cmd = [ 'git', '-c', 'diff.mnemonicprefix=no', 'diff', '--no-color', '--no-ext-diff' ] mock.assert_called_once_with(expected_cmd) [[[a, b, c, d, e, f, g]]] = result assert a.is_context assert b.is_context assert c.is_context assert d.is_added assert e.is_context assert f.is_context assert g.is_context
Example #14
Source File: test_completion_refresher.py From pgcli with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_ctor(refresher): """ Refresher object should contain a few handlers :param refresher: :return: """ assert len(refresher.refreshers) > 0 actual_handlers = list(refresher.refreshers.keys()) expected_handlers = [ "schemata", "tables", "views", "types", "databases", "casing", "functions", ] assert expected_handlers == actual_handlers
Example #15
Source File: test_notifications.py From django-herald with MIT License | 5 votes |
def test_resend_error_raise(self): notification = SentNotification() with patch.object(NotificationBase, '_send') as mocked__send: mocked__send.side_effect = Exception self.assertRaises(Exception, NotificationBase.resend, notification, raise_exception=True)
Example #16
Source File: test_notifications.py From django-herald with MIT License | 5 votes |
def test_resend_error(self): notification = SentNotification() with patch.object(NotificationBase, '_send') as mocked__send: mocked__send.side_effect = Exception result = NotificationBase.resend(notification) self.assertFalse(result)
Example #17
Source File: test_enable_sharding.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_enable_sharding_already_on_db(mongo_host, arctic, mongo_server, user_library, user_library_name): c = mongo_server.api with patch.object(c, 'admin') as admin: admin.command = Mock(return_value=[OperationFailure("failed: already enabled"), None]) with patch('pymongo.MongoClient', return_value=c) as mc: run_as_main(mes.main, '--host', mongo_host, '--library', user_library_name) assert mc.call_args_list == [call(get_mongodb_uri(mongo_host))] assert len(admin.command.call_args_list) == 3 assert call('buildinfo', read_preference=Primary(), session=None) in admin.command.call_args_list or call('buildinfo', read_preference=Primary()) in admin.command.call_args_list assert call('shardCollection', 'arctic_' + user_library_name, key={'symbol': 'hashed'}) in admin.command.call_args_list assert call('enablesharding', 'arctic_' + getpass.getuser()) in admin.command.call_args_list
Example #18
Source File: test_ts_read.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_objects_fail(tickstore_lib): class Fake(object): def __init__(self, val): self.val = val def fake(self): return self.val df = pd.DataFrame(data={'data': [Fake(1), Fake(2)]}, index=pd.Index(data=[dt(2016, 1, 1, 00, tzinfo=mktz('UTC')), dt(2016, 1, 2, 00, tzinfo=mktz('UTC'))], name='date')) with pytest.raises(Exception) as e: tickstore_lib.write('test', df) assert('Casting object column to string failed' in str(e.value))
Example #19
Source File: test_ts_read.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_read_multiple_symbols(tickstore_lib): data1 = [{'ASK': 1545.25, 'ASKSIZE': 1002.0, 'BID': 1545.0, 'BIDSIZE': 55.0, 'CUMVOL': 2187387.0, 'DELETED_TIME': 0, 'INSTRTYPE': 'FUT', 'PRICE': 1545.0, 'SIZE': 1.0, 'TICK_STATUS': 0, 'TRADEHIGH': 1561.75, 'TRADELOW': 1537.25, 'index': 1185076787070}, ] data2 = [{'CUMVOL': 354.0, 'DELETED_TIME': 0, 'PRICE': 1543.75, 'SIZE': 354.0, 'TRADEHIGH': 1543.75, 'TRADELOW': 1543.75, 'index': 1185141600600}] tickstore_lib.write('BAR', data2) tickstore_lib.write('FOO', data1) df = tickstore_lib.read(['FOO', 'BAR'], columns=['BID', 'ASK', 'PRICE']) assert all(df['SYMBOL'].values == ['FOO', 'BAR']) assert_array_equal(df['ASK'].values, np.array([1545.25, np.nan])) assert_array_equal(df['BID'].values, np.array([1545, np.nan])) assert_array_equal(df['PRICE'].values, np.array([1545, 1543.75])) assert_array_equal(df.index.values.astype('object'), np.array([1185076787070000000, 1185141600600000000])) assert tickstore_lib._collection.find_one()['c'] == 1
Example #20
Source File: test_ts_read.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_read_allow_secondary(tickstore_lib): data = [{'ASK': 1545.25, 'ASKSIZE': 1002.0, 'BID': 1545.0, 'BIDSIZE': 55.0, 'CUMVOL': 2187387.0, 'DELETED_TIME': 0, 'INSTRTYPE': 'FUT', 'PRICE': 1545.0, 'SIZE': 1.0, 'TICK_STATUS': 0, 'TRADEHIGH': 1561.75, 'TRADELOW': 1537.25, 'index': 1185076787070}, {'CUMVOL': 354.0, 'DELETED_TIME': 0, 'PRICE': 1543.75, 'SIZE': 354.0, 'TRADEHIGH': 1543.75, 'TRADELOW': 1543.75, 'index': 1185141600600}] tickstore_lib.write('FEED::SYMBOL', data) with patch('pymongo.collection.Collection.find', side_effect=tickstore_lib._collection.find) as find: with patch('pymongo.collection.Collection.with_options', side_effect=tickstore_lib._collection.with_options) as with_options: with patch.object(tickstore_lib, '_read_preference', side_effect=tickstore_lib._read_preference) as read_pref: df = tickstore_lib.read('FEED::SYMBOL', columns=['BID', 'ASK', 'PRICE'], allow_secondary=True) assert read_pref.call_args_list == [call(True)] assert with_options.call_args_list == [call(read_preference=ReadPreference.NEAREST)] assert find.call_args_list == [call({'sy': 'FEED::SYMBOL'}, sort=[('s', 1)], projection={'s': 1, '_id': 0}), call({'sy': 'FEED::SYMBOL', 's': {'$lte': dt(2007, 8, 21, 3, 59, 47, 70000)}}, projection={'sy': 1, 'cs.PRICE': 1, 'i': 1, 'cs.BID': 1, 's': 1, 'im': 1, 'v': 1, 'cs.ASK': 1})] assert_array_equal(df['ASK'].values, np.array([1545.25, np.nan])) assert tickstore_lib._collection.find_one()['c'] == 2
Example #21
Source File: test_ts_read.py From arctic with GNU Lesser General Public License v2.1 | 5 votes |
def test_read(tickstore_lib): data = [{'ASK': 1545.25, 'ASKSIZE': 1002.0, 'BID': 1545.0, 'BIDSIZE': 55.0, 'CUMVOL': 2187387.0, 'DELETED_TIME': 0, 'INSTRTYPE': 'FUT', 'PRICE': 1545.0, 'SIZE': 1.0, 'TICK_STATUS': 0, 'TRADEHIGH': 1561.75, 'TRADELOW': 1537.25, 'index': 1185076787070}, {'CUMVOL': 354.0, 'DELETED_TIME': 0, 'PRICE': 1543.75, 'SIZE': 354.0, 'TRADEHIGH': 1543.75, 'TRADELOW': 1543.75, 'index': 1185141600600}] tickstore_lib.write('FEED::SYMBOL', data) df = tickstore_lib.read('FEED::SYMBOL', columns=['BID', 'ASK', 'PRICE']) assert_array_equal(df['ASK'].values, np.array([1545.25, np.nan])) assert_array_equal(df['BID'].values, np.array([1545, np.nan])) assert_array_equal(df['PRICE'].values, np.array([1545, 1543.75])) assert_array_equal(df.index.values.astype('object'), np.array([1185076787070000000, 1185141600600000000])) assert tickstore_lib._collection.find_one()['c'] == 2 assert df.index.tzinfo == mktz()
Example #22
Source File: test_serving.py From sagemaker-xgboost-container with Apache License 2.0 | 5 votes |
def test_predict_fn(np_array): mock_estimator = FakeEstimator() with patch.object(mock_estimator, 'predict') as mock: serving.default_predict_fn(np_array, mock_estimator) mock.assert_called_once()
Example #23
Source File: test_handler_service.py From sagemaker-xgboost-container with Apache License 2.0 | 5 votes |
def test_predict_fn(np_array): mock_estimator = FakeEstimator() with patch.object(mock_estimator, 'predict') as mock: handler.default_predict_fn(np_array, mock_estimator) mock.assert_called_once()
Example #24
Source File: test_incident.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def test_reporting_values(app_name, db_parameters): import snowflake.connector original_paramstyle = snowflake.connector.paramstyle snowflake.connector.paramstyle = 'qmark' original_blacklist = snowflake.connector.incident.CLS_BLACKLIST snowflake.connector.incident.CLS_BLACKLIST = frozenset() converter.PYTHON_TO_SNOWFLAKE_TYPE['nonetype'] = None db_parameters['internal_application_name'] = app_name con = None try: con = snowflake.connector.connect(**db_parameters) con.cursor().execute("alter session set SUPPRESS_INCIDENT_DUMPS=true") cursor = con.cursor() with patch.object(con.rest, 'request') as incident_report: cursor.execute("INSERT INTO foo VALUES (?)", [None]) fail("Shouldn't reach ths statement") except ProgrammingError: pass # ignore, should be thrown finally: converter.PYTHON_TO_SNOWFLAKE_TYPE['nonetype'] = 'ANY' snowflake.connector.paramstyle = original_paramstyle snowflake.connector.incident.CLS_BLACKLIST = original_blacklist for tag in incident_report.call_args[0][1]['Tags']: if tag['Name'] == 'driver': assert tag['Value'] == app_name if con is not None: con.close()
Example #25
Source File: test_put_get.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def test_put_overwrite(tmpdir, db_parameters): """Tests whether _force_put_overwrite and overwrite=true works as intended.""" import snowflake.connector cnx = snowflake.connector.connect( user=db_parameters['s3_user'], password=db_parameters['s3_password'], host=db_parameters['s3_host'], port=db_parameters['s3_port'], database=db_parameters['s3_database'], account=db_parameters['s3_account'], protocol=db_parameters['s3_protocol']) tmp_dir = str(tmpdir.mkdir('data')) test_data = os.path.join(tmp_dir, 'data.txt') with open(test_data, 'w') as f: f.write("test1,test2") f.write("test3,test4") cnx.cursor().execute("RM @~/test_put_overwrite") try: with cnx.cursor() as cur: with patch.object(cur, '_init_result_and_meta', wraps=cur._init_result_and_meta) as mock_result: cur.execute("PUT file://{} @~/test_put_overwrite".format(test_data)) assert mock_result.call_args[0][0]['rowset'][0][-2] == 'UPLOADED' with patch.object(cur, '_init_result_and_meta', wraps=cur._init_result_and_meta) as mock_result: cur.execute("PUT file://{} @~/test_put_overwrite".format(test_data)) assert mock_result.call_args[0][0]['rowset'][0][-2] == 'SKIPPED' with patch.object(cur, '_init_result_and_meta', wraps=cur._init_result_and_meta) as mock_result: cur.execute("PUT file://{} @~/test_put_overwrite OVERWRITE = TRUE".format(test_data)) assert mock_result.call_args[0][0]['rowset'][0][-2] == 'UPLOADED' ret = cnx.cursor().execute("LS @~/test_put_overwrite").fetchone() assert "test_put_overwrite/data.txt" in ret[0] assert "data.txt.gz" in ret[0] finally: cnx.cursor().execute("RM @~/test_put_overwrite")
Example #26
Source File: test_incident.py From snowflake-connector-python with Apache License 2.0 | 5 votes |
def test_reporting_values(app_name, db_parameters): import snowflake.connector original_paramstyle = snowflake.connector.paramstyle snowflake.connector.paramstyle = 'qmark' original_blacklist = snowflake.connector.incident.CLS_BLACKLIST snowflake.connector.incident.CLS_BLACKLIST = frozenset() converter.PYTHON_TO_SNOWFLAKE_TYPE['nonetype'] = None db_parameters['internal_application_name'] = app_name con = None try: con = snowflake.connector.connect(**db_parameters) con.cursor().execute("alter session set SUPPRESS_INCIDENT_DUMPS=true") cursor = con.cursor() with patch.object(con.rest, 'request') as incident_report: cursor.execute("INSERT INTO foo VALUES (?)", [None]) fail("Shouldn't reach ths statement") except ProgrammingError: pass # ignore, should be thrown finally: converter.PYTHON_TO_SNOWFLAKE_TYPE['nonetype'] = 'ANY' snowflake.connector.paramstyle = original_paramstyle snowflake.connector.incident.CLS_BLACKLIST = original_blacklist for tag in incident_report.call_args[0][1]['Tags']: if tag['Name'] == 'driver': assert tag['Value'] == app_name if con is not None: con.close()
Example #27
Source File: test_pgexecute.py From pgcli with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_short_host(executor): with patch.object(executor, "host", "localhost"): assert executor.short_host == "localhost" with patch.object(executor, "host", "localhost.example.org"): assert executor.short_host == "localhost" with patch.object( executor, "host", "localhost1.example.org,localhost2.example.org" ): assert executor.short_host == "localhost1"
Example #28
Source File: test_pushpull.py From datapackage-py with MIT License | 5 votes |
def storage(request): import_module = patch.object(module, 'import_module').start() storage = import_module.return_value.Storage.return_value request.addfinalizer(patch.stopall) return storage
Example #29
Source File: test_admin.py From django-herald with MIT License | 5 votes |
def test_resend(self): with patch.object(SentNotification, 'resend') as mocked_resend: response = self.client.get( reverse('admin:herald_sentnotification_resend', args=(self.notification.pk,)), follow=True ) mocked_resend.assert_called_once() self.assertEqual(response.status_code, 200) self.assertIn( 'The notification was resent successfully.', [m.message for m in list(response.context['messages'])] )
Example #30
Source File: test_base_station.py From python-arlo with GNU Lesser General Public License v3.0 | 5 votes |
def test_is_motion_detection_enabled(self, mock): """Test ArloBaseStation.is_motion_detection_enabled properties.""" with patch.object(ArloBaseStation, 'mode') as mocked_mode: mocked_mode.__get__ = Mock(return_value='armed') base = self.load_base_station(mock) self.assertTrue(base.is_motion_detection_enabled) with patch.object(ArloBaseStation, 'mode') as mocked_mode: mocked_mode.__get__ = Mock(return_value='disarmed') base = self.load_base_station(mock) self.assertFalse(base.is_motion_detection_enabled)