Python unittest.mock.patch.object() Examples
The following are 30
code examples of unittest.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
unittest.mock.patch
, or try the search function
.
Example #1
Source File: test_shell.py From jira with BSD 2-Clause "Simplified" License | 6 votes |
def test_unicode(requests_mock, capsys, testargs): """This functions tests that CLI tool does not throw an UnicodeDecodeError when it attempts to display some Unicode error message, which can happen when printing exceptions received from the remote HTTP server. """ requests_mock.register_uri( "GET", "http://localhost/rest/api/2/serverInfo", text="Δεν βρέθηκε", status_code=404, ) with patch.object(sys, "argv", testargs): jirashell.main() captured = capsys.readouterr() assert captured.err.startswith("JiraError HTTP 404") assert captured.out == ""
Example #2
Source File: test_stream.py From peony-twitter with MIT License | 6 votes |
def test_stream_eof_reconnect(): async def dummy(*args, **kwargs): pass turn = -1 async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_eof): with patch.object(peony.stream.asyncio, 'sleep', side_effect=dummy): async for data in stream: turn += 1 if turn == 0: assert data == {'connected': True} elif turn % 2 == 1: assert stream._state == EOF assert data == {'reconnecting_in': 0, 'error': None} else: assert data == {'stream_restart': True} break
Example #3
Source File: test_utils.py From peony-twitter with MIT License | 6 votes |
def test_error_handler_service_unavailable(event_loop): global tries tries = 4 async def service_unavailable(**kwargs): global tries tries -= 1 if tries > 0: response = MockResponse(status=503) raise await exceptions.throw(response) else: return MockResponse() with patch.object(asyncio, 'sleep', side_effect=dummy) as sleep: try: fut = create_future(event_loop) coro = utils.DefaultErrorHandler(service_unavailable)(future=fut) event_loop.create_task(coro) await fut except exceptions.ServiceUnavailable: assert sleep.called else: pytest.fail("ServiceUnavailable not raised")
Example #4
Source File: test_stream.py From peony-twitter with MIT License | 6 votes |
def test_stream_reconnection_enhance_your_calm(): async def dummy(*args, **kwargs): pass turn = -1 async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_calm): with patch.object(peony.stream.asyncio, 'sleep', side_effect=dummy): async for data in stream: assert stream._state == ENHANCE_YOUR_CALM turn += 1 if turn >= 100: break if turn == 0: assert data == {'connected': True} elif turn % 2 == 1: timeout = ENHANCE_YOUR_CALM_TIMEOUT * 2**(turn // 2) assert data == {'reconnecting_in': timeout, 'error': None} else: assert data == {'stream_restart': True}
Example #5
Source File: test_models.py From resolwe with Apache License 2.0 | 6 votes |
def setUp(self): super().setUp() DescriptorSchema.objects.create( name="Sample", slug="sample", contributor=self.contributor ) self.process = Process.objects.create( name="Test process", type="data:test:", contributor=self.contributor, entity_type="sample", entity_descriptor_schema="sample", ) # Entity is created automatically when Data object is created self.data = Data.objects.create( name="Test data", contributor=self.contributor, process=self.process )
Example #6
Source File: test_base_client.py From peony-twitter with MIT License | 6 votes |
def test_request_proxy(): def raise_proxy(*args, proxy=None, **kwargs): raise RuntimeError(proxy) async with BasePeonyClient("", "", proxy="http://some.proxy.com") as dummy_client: async with aiohttp.ClientSession() as session: with patch.object(dummy_client.headers, 'prepare_request', side_effect=raise_proxy): try: await dummy_client.request(method='get', url="http://hello.com", session=session, future=None) except RuntimeError as e: assert str(e) == "http://some.proxy.com" else: pytest.fail("Could not check proxy")
Example #7
Source File: test_models.py From resolwe with Apache License 2.0 | 6 votes |
def test_change_size(self, path_mock): """Size is not changed after object is done.""" data = self.create_data(path_mock, self.contributor, self.process) path_mock.return_value = MagicMock( stat=lambda: MagicMock(st_size=42000), is_file=lambda: True ) hydrate_size(data) self.assertEqual(data.output["test_file"]["size"], 42000) path_mock.return_value = MagicMock( stat=lambda: MagicMock(st_size=43000), is_file=lambda: True ) hydrate_size(data) self.assertEqual(data.output["test_file"]["size"], 43000) data.status = Data.STATUS_DONE path_mock.return_value = MagicMock( stat=lambda: MagicMock(st_size=44000), is_file=lambda: True ) hydrate_size(data) self.assertEqual(data.output["test_file"]["size"], 43000)
Example #8
Source File: test_models.py From resolwe with Apache License 2.0 | 6 votes |
def test_save_storage_file(self): """File is loaded and saved to storage""" data = Data.objects.create( name="Test data", contributor=self.contributor, process=self.proc, ) data_location = create_data_location() data_location.data.add(data) data.output = {"json_field": "json.txt"} data.status = Data.STATUS_DONE json_file = io.StringIO(json.dumps({"foo": "bar"})) isfile_mock = MagicMock(return_value=True) open_mock = MagicMock(return_value=json_file) with patch.object(os.path, "isfile", isfile_mock): with patch.object(builtins, "open", open_mock): data.save() self.assertEqual(Storage.objects.count(), 1) storage = Storage.objects.first() self.assertEqual(data.output["json_field"], storage.pk) self.assertEqual(storage.json, {"foo": "bar"})
Example #9
Source File: test_base_client.py From peony-twitter with MIT License | 6 votes |
def test_streaming_apis(): async with DummyClient() as dummy_client: with patch.object(dummy_client, 'request', side_effect=dummy)\ as request: await dummy_client.api.test.get() assert request.called with patch.object(dummy_client, 'stream_request') as request: dummy_client.stream.test.get() assert request.called async with DummyClient(streaming_apis={'api'}) as client: with patch.object(client, 'stream_request') as request: client.api.test.get() assert request.called with patch.object(client, 'request', side_effect=dummy) as request: await client.stream.test.get() assert request.called
Example #10
Source File: test_oauth.py From peony-twitter with MIT License | 6 votes |
def post(self, _data=None, _headers=None, **kwargs): self.count += 1 if _data is not None: with patch.object(oauth.aiohttp.payload, 'BytesPayload', side_effect=dummy_func): assert _data._gen_form_urlencoded() == b"access_token=abc" if _headers is not None: key = "1234567890:0987654321" auth = base64.b64encode(key.encode('utf-8')).decode('utf-8') assert _headers['Authorization'] == 'Basic ' + auth # This is needed to run `test_oauth2_concurrent_refreshes` # without that, refresh tasks would be executed sequentially # In a sense it is a simulation of a request being fetched await asyncio.sleep(0.001) return {'access_token': "abc"}
Example #11
Source File: test_utils.py From zdict with GNU General Public License v3.0 | 6 votes |
def test_platform_readline(): ''' Check the imported readline module on different platforms ''' with patch.object(sys, 'platform', new='linux'): readline = import_readline() assert readline.__name__ == 'readline' with patch.object(sys, 'platform', new='darwin'): readline = import_readline() expect = 'gnureadline' if sys.version_info <= (3, 5) else 'readline' assert readline.__name__ == expect with patch.object(sys, 'platform', new='foo'): readline = import_readline() assert readline.__name__ == 'readline'
Example #12
Source File: test_requests.py From peony-twitter with MIT License | 6 votes |
def test_request_error_handler(api_path, _error_handler=True): client = api_path.client with patch.object(client, 'request', side_effect=dummy) as client_request: with patch.object(client, 'error_handler', side_effect=dummy_error_handler) as error_handler: client.loop.run_until_complete( requests.Request(api_path, 'get', _error_handling=_error_handler, test=1, _test=2) ) assert client_request.called_with(method='get', url=url, skip_params=False, test=2, params={'test': 1}) assert error_handler.called is _error_handler
Example #13
Source File: test_arguments.py From zdict with GNU General Public License v3.0 | 6 votes |
def test_multiprocessing(self): testargs = ['', '-j', '2', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f1 = StringIO() with redirect_stdout(f1): main() testargs = ['', '-j', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f2 = StringIO() with redirect_stdout(f2): main() testargs = ['', '-d', '-dt', 'yahoo', 'test'] with patch.object(sys, 'argv', new=testargs): f3 = StringIO() with redirect_stdout(f3): main() result1 = f1.getvalue().strip() result2 = f2.getvalue().strip() result3 = f3.getvalue().strip() assert result1 == result2 == result3
Example #14
Source File: test_upload.py From peony-twitter with MIT License | 6 votes |
def test_chunked_upload_fail(medias): async with DummyPeonyClient() as client: media = medias['video'] media_data = await media.download() chunk_size = 1024**2 dummy_request = DummyRequest(client, media, chunk_size, True) with patch.object(client, 'request', side_effect=dummy_request): with patch.object(asyncio, 'sleep', side_effect=dummy) as sleep: with pytest.raises(peony.exceptions.MediaProcessingError): await client.upload_media( media_data, chunk_size=chunk_size, chunked=True ) sleep.assert_called_with(5)
Example #15
Source File: test_qna.py From botbuilder-python with MIT License | 6 votes |
def test_call_train(self): feedback_records = [] feedback1 = FeedbackRecord( qna_id=1, user_id="test", user_question="How are you?" ) feedback2 = FeedbackRecord(qna_id=2, user_id="test", user_question="What up??") feedback_records.extend([feedback1, feedback2]) with patch.object( QnAMaker, "call_train", return_value=None ) as mocked_call_train: qna = QnAMaker(QnaApplicationTest.tests_endpoint) qna.call_train(feedback_records) mocked_call_train.assert_called_once_with(feedback_records)
Example #16
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def test_stream_reconnection_error_on_reconnection(): async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_disconnection): await stream.connect() assert stream._state == DISCONNECTION data = {'reconnecting_in': DISCONNECTION_TIMEOUT, 'error': None} assert data == await stream.__anext__() assert stream._reconnecting with patch.object(stream, '_connect', side_effect=response_calm): stream._error_timeout = 0 assert {'stream_restart': True} == await stream.__anext__() assert stream._state == ENHANCE_YOUR_CALM data = {'reconnecting_in': ENHANCE_YOUR_CALM_TIMEOUT, 'error': None} assert data == await stream.__anext__()
Example #17
Source File: test_transfer.py From resolwe with Apache License 2.0 | 5 votes |
def test_ok(self): t = Transfer(self.local, self.local) with patch.object(Transfer, "_transfer_chunk") as transfer_mock: t.transfer_objects("base", [{}]) transfer_mock.assert_called_once_with(Path("base"), [{}])
Example #18
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def test_stream_reconnection_stream_limit(): async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_stream_limit): assert stream._state == NORMAL data = await stream.__anext__() assert 'connected' in data data = await stream.__anext__() assert stream.state == ERROR assert data['reconnecting_in'] == ERROR_TIMEOUT assert isinstance(data['error'], exceptions.StreamLimit)
Example #19
Source File: test_transfer.py From resolwe with Apache License 2.0 | 5 votes |
def test_exception(self): t = Transfer(self.local, self.local) with patch.object(Transfer, "_transfer_chunk") as transfer_mock: transfer_mock.side_effect = [None, DataTransferError] with self.assertRaises(DataTransferError): t.transfer_objects("test_url", [{}, {}])
Example #20
Source File: test_upload.py From peony-twitter with MIT License | 5 votes |
def chunked_upload(media, file): async with DummyPeonyClient() as dummy_peony_client: chunk_size = 1024 ** 2 dummy_request = DummyRequest(dummy_peony_client, media, chunk_size) with patch.object(dummy_peony_client, 'request', side_effect=dummy_request): with patch.object(asyncio, 'sleep', side_effect=dummy) as sleep: await dummy_peony_client.upload_media(file, chunk_size=chunk_size, chunked=True) if media.filename == 'video': sleep.assert_called_with(5) elif media.filename == 'bloom': sleep.assert_called_with(1) dummy_request.reset() with patch.object(utils, 'get_media_metadata') as metadata: with patch.object(utils, 'get_category') as category: await dummy_peony_client.upload_media( file, chunk_size=chunk_size, chunked=True, media_category=media.category, media_type=media.type ) assert not metadata.called assert not category.called dummy_request.reset() with patch.object(utils, 'get_media_metadata') as metadata: await dummy_peony_client.upload_media( file, chunk_size=chunk_size, media_type=media.type, chunked=True ) assert not metadata.called
Example #21
Source File: test_transfer.py From resolwe with Apache License 2.0 | 5 votes |
def test_max_thread(self): t = Transfer(self.local, self.local) with patch.object(Transfer, "_transfer_chunk") as transfer_mock: t.transfer_objects("base", [{}] * 10) self.assertEqual(len(transfer_mock.call_args_list), 10) with patch.object(Transfer, "_transfer_chunk") as transfer_mock: t.transfer_objects("base", [{}] * 20) self.assertEqual(len(transfer_mock.call_args_list), 10) with patch.object(Transfer, "_transfer_chunk") as transfer_mock: t.transfer_objects("base", [{}] * 20, max_threads=20) self.assertEqual(len(transfer_mock.call_args_list), 20)
Example #22
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def test_stream_reconnection_reconnect(): async def dummy(*args, **kwargs): pass turn = -1 async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_reconnection): with patch.object(peony.stream.asyncio, 'sleep', side_effect=dummy): async for data in stream: assert stream._state == RECONNECTION turn += 1 if turn == 0: assert data == {'connected': True} elif turn % 2 == 1: timeout = RECONNECTION_TIMEOUT * 2**(turn // 2) if timeout > MAX_RECONNECTION_TIMEOUT: actual = data['reconnecting_in'] assert actual == MAX_RECONNECTION_TIMEOUT break assert data == {'reconnecting_in': timeout, 'error': None} else: assert data == {'stream_restart': True}
Example #23
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def test_stream_reconnection_disconnection(): async def dummy(*args, **kwargs): pass turn = -1 async with Stream() as stream: with patch.object(stream, '_connect', side_effect=response_disconnection): with patch.object(peony.stream.asyncio, 'sleep', side_effect=dummy): async for data in stream: assert stream._state == DISCONNECTION turn += 1 if turn == 0: assert data == {'connected': True} elif turn % 2 == 1: timeout = DISCONNECTION_TIMEOUT * (turn + 1) / 2 if timeout > MAX_DISCONNECTION_TIMEOUT: actual = data['reconnecting_in'] assert actual == MAX_DISCONNECTION_TIMEOUT break assert data == {'reconnecting_in': timeout, 'error': None} else: assert data == {'stream_restart': True}
Example #24
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def _stream_iteration(stream): async def stop(*args, **kwargs): raise StopAsyncIteration with patch.object(stream, 'init_restart', side_effect=stop): i = 0 connected = False async for line in stream: if connected is False: connected = True assert 'connected' in line else: assert line['text'] == MockResponse.message + " #%d" % i i += 1
Example #25
Source File: test_stream.py From peony-twitter with MIT License | 5 votes |
def __aenter__(self): self.session = aiohttp.ClientSession() self.client = peony.client.BasePeonyClient("", "", session=self.session) self.patch = patch.object(self.session, 'request', side_effect=stream_content) self.patch.__enter__() return peony.stream.StreamResponse( client=self.client, method='get', url="http://whatever.com/stream" )
Example #26
Source File: test_tasks.py From peony-twitter with MIT License | 5 votes |
def test_run(): client = TasksClientTest() with patch.object(client, 'request', side_effect=dummy) as request: client.run() base_url = twitter_base_api_url.format(api='api', version=twitter_api_version) assert request.called_with(method='get', url=base_url + '/test.json') assert request.called_with(method='get', url=base_url + '/endpoint.json') assert all(client.tasks_tests)
Example #27
Source File: test_tasks.py From peony-twitter with MIT License | 5 votes |
def test_tasks(): async with TasksClientTest() as client: with patch.object(client, 'request', side_effect=dummy) as request: await client.run_tasks() base_url = twitter_base_api_url.format(api='api', version=twitter_api_version) assert request.called_with(method='get', url=base_url + '/test.json') assert request.called_with(method='get', url=base_url + '/endpoint.json') assert all(client.tasks_tests)
Example #28
Source File: test_upload.py From peony-twitter with MIT License | 5 votes |
def test_upload_type_error(url): async with DummyPeonyClient() as client: async with MediaRequest(url) as media_request: def fail(*args, **kwargs): pytest.fail("Did not raise TypeError") with pytest.raises(TypeError): with patch.object(client, 'request', side_effect=fail): await client.upload_media(media_request.content, chunked=True)
Example #29
Source File: test_upload.py From peony-twitter with MIT License | 5 votes |
def test_upload_from_request(url): async with MediaPeonyClient("", "") as dummy_peony_client: async with MediaRequest(url) as media_request: async def dummy_request(url, method, future, data=None, skip_params=None): assert url == dummy_peony_client.upload.media.upload.url() assert method.lower() == 'post' assert data['media'] == media_request.content assert skip_params future.set_result(None) with patch.object(dummy_peony_client, 'request', side_effect=dummy_request): await dummy_peony_client.upload_media(media_request)
Example #30
Source File: test_upload.py From peony-twitter with MIT License | 5 votes |
def __enter__(self): self.original = getattr(self.object, self.attr) setattr(self.object, self.attr, self.val)