Best Python code snippet using autotest_python
test_method.py
Source:test_method.py  
...27        curr_value += delta28def test_wrap_method_basic():29    method = mock.Mock(spec=["__call__"], return_value=42)30    wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)31    result = wrapped_method(1, 2, meep="moop")32    assert result == 4233    method.assert_called_once_with(1, 2, meep="moop", metadata=mock.ANY)34    # Check that the default client info was specified in the metadata.35    metadata = method.call_args[1]["metadata"]36    assert len(metadata) == 137    client_info = google.api_core.gapic_v1.client_info.DEFAULT_CLIENT_INFO38    user_agent_metadata = client_info.to_grpc_metadata()39    assert user_agent_metadata in metadata40def test_wrap_method_with_no_client_info():41    method = mock.Mock(spec=["__call__"])42    wrapped_method = google.api_core.gapic_v1.method.wrap_method(43        method, client_info=None44    )45    wrapped_method(1, 2, meep="moop")46    method.assert_called_once_with(1, 2, meep="moop")47def test_wrap_method_with_custom_client_info():48    client_info = google.api_core.gapic_v1.client_info.ClientInfo(49        python_version=1,50        grpc_version=2,51        api_core_version=3,52        gapic_version=4,53        client_library_version=5,54    )55    method = mock.Mock(spec=["__call__"])56    wrapped_method = google.api_core.gapic_v1.method.wrap_method(57        method, client_info=client_info58    )59    wrapped_method(1, 2, meep="moop")60    method.assert_called_once_with(1, 2, meep="moop", metadata=mock.ANY)61    # Check that the custom client info was specified in the metadata.62    metadata = method.call_args[1]["metadata"]63    assert client_info.to_grpc_metadata() in metadata64def test_invoke_wrapped_method_with_metadata():65    method = mock.Mock(spec=["__call__"])66    wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)67    wrapped_method(mock.sentinel.request, metadata=[("a", "b")])68    method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)69    metadata = method.call_args[1]["metadata"]70    # Metadata should have two items: the client info metadata and our custom71    # metadata.72    assert len(metadata) == 273    assert ("a", "b") in metadata74def test_invoke_wrapped_method_with_metadata_as_none():75    method = mock.Mock(spec=["__call__"])76    wrapped_method = google.api_core.gapic_v1.method.wrap_method(method)77    wrapped_method(mock.sentinel.request, metadata=None)78    method.assert_called_once_with(mock.sentinel.request, metadata=mock.ANY)79    metadata = method.call_args[1]["metadata"]80    # Metadata should have just one items: the client info metadata.81    assert len(metadata) == 182@mock.patch("time.sleep")83def test_wrap_method_with_default_retry_and_timeout(unusued_sleep):84    method = mock.Mock(85        spec=["__call__"], side_effect=[exceptions.InternalServerError(None), 42]86    )87    default_retry = retry.Retry()88    default_timeout = timeout.ConstantTimeout(60)89    wrapped_method = google.api_core.gapic_v1.method.wrap_method(90        method, default_retry, default_timeout91    )92    result = wrapped_method()93    assert result == 4294    assert method.call_count == 295    method.assert_called_with(timeout=60, metadata=mock.ANY)96@mock.patch("time.sleep")97def test_wrap_method_with_default_retry_and_timeout_using_sentinel(unusued_sleep):98    method = mock.Mock(99        spec=["__call__"], side_effect=[exceptions.InternalServerError(None), 42]100    )101    default_retry = retry.Retry()102    default_timeout = timeout.ConstantTimeout(60)103    wrapped_method = google.api_core.gapic_v1.method.wrap_method(104        method, default_retry, default_timeout105    )106    result = wrapped_method(107        retry=google.api_core.gapic_v1.method.DEFAULT,108        timeout=google.api_core.gapic_v1.method.DEFAULT,109    )110    assert result == 42111    assert method.call_count == 2112    method.assert_called_with(timeout=60, metadata=mock.ANY)113@mock.patch("time.sleep")114def test_wrap_method_with_overriding_retry_and_timeout(unusued_sleep):115    method = mock.Mock(spec=["__call__"], side_effect=[exceptions.NotFound(None), 42])116    default_retry = retry.Retry()117    default_timeout = timeout.ConstantTimeout(60)118    wrapped_method = google.api_core.gapic_v1.method.wrap_method(119        method, default_retry, default_timeout120    )121    result = wrapped_method(122        retry=retry.Retry(retry.if_exception_type(exceptions.NotFound)),123        timeout=timeout.ConstantTimeout(22),124    )125    assert result == 42126    assert method.call_count == 2127    method.assert_called_with(timeout=22, metadata=mock.ANY)128@mock.patch("time.sleep")129@mock.patch(130    "google.api_core.datetime_helpers.utcnow",131    side_effect=_utcnow_monotonic(),132    autospec=True,133)134def test_wrap_method_with_overriding_retry_deadline(utcnow, unused_sleep):135    method = mock.Mock(136        spec=["__call__"],137        side_effect=([exceptions.InternalServerError(None)] * 4) + [42],138    )139    default_retry = retry.Retry()140    default_timeout = timeout.ExponentialTimeout(deadline=60)141    wrapped_method = google.api_core.gapic_v1.method.wrap_method(142        method, default_retry, default_timeout143    )144    # Overriding only the retry's deadline should also override the timeout's145    # deadline.146    result = wrapped_method(retry=default_retry.with_deadline(30))147    assert result == 42148    timeout_args = [call[1]["timeout"] for call in method.call_args_list]149    assert timeout_args == [5.0, 10.0, 20.0, 26.0, 25.0]150    assert utcnow.call_count == (151        1152        + 5  # First to set the deadline.153        + 5  # One for each min(timeout, maximum, (DEADLINE - NOW).seconds)154    )155def test_wrap_method_with_overriding_timeout_as_a_number():156    method = mock.Mock(spec=["__call__"], return_value=42)157    default_retry = retry.Retry()158    default_timeout = timeout.ConstantTimeout(60)159    wrapped_method = google.api_core.gapic_v1.method.wrap_method(160        method, default_retry, default_timeout161    )162    result = wrapped_method(timeout=22)163    assert result == 42...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
