Best Python code snippet using localstack_python
test_aws_service.py
Source:test_aws_service.py  
1# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.2# SPDX-License-Identifier: Apache-2.03"""4Unit tests for aws_service.py.5"""6import boto37from botocore.exceptions import ClientError8import pytest9from aws_service import ApiGatewayToService10@pytest.mark.parametrize('error_code,stop_on_method', [11    (None, None),12    ('TestException', 'stub_create_rest_api'),13    ('TestException', 'stub_get_resources')])14def test_create_rest_api(make_stubber, stub_runner, error_code, stop_on_method):15    apigateway_client = boto3.client('apigateway')16    apigateway_stubber = make_stubber(apigateway_client)17    api_gtos = ApiGatewayToService(apigateway_client)18    api_name = 'test-api_name'19    api_id = 'test-api-id'20    resources = [{'path': '/', 'id': 'resource-id'}]21    with stub_runner(error_code, stop_on_method) as runner:22        runner.add(apigateway_stubber.stub_create_rest_api, api_name, api_id)23        runner.add(apigateway_stubber.stub_get_resources, api_id, resources)24    if error_code is None:25        got_api_id = api_gtos.create_rest_api(api_name)26        assert got_api_id == api_id27        assert api_gtos.root_id == resources[0]['id']28    else:29        with pytest.raises(ClientError) as exc_info:30            api_gtos.create_rest_api(api_name)31        assert exc_info.value.response['Error']['Code'] == error_code32@pytest.mark.parametrize('error_code', [None, 'TestException'])33def test_add_rest_resource(make_stubber, error_code):34    apigateway_client = boto3.client('apigateway')35    apigateway_stubber = make_stubber(apigateway_client)36    api_gtos = ApiGatewayToService(apigateway_client)37    api_gtos.api_id = 'test-api-id'38    parent_id = 'test-parent_id'39    resource_path = '/test'40    resource_id = 'resource-id'41    apigateway_stubber.stub_create_resource(42        api_gtos.api_id, parent_id, resource_path, resource_id, error_code=error_code)43    if error_code is None:44        got_resource_id = api_gtos.add_rest_resource(parent_id, resource_path)45        assert got_resource_id == resource_id46    else:47        with pytest.raises(ClientError) as exc_info:48            api_gtos.add_rest_resource(parent_id, resource_path)49        assert exc_info.value.response['Error']['Code'] == error_code50@pytest.mark.parametrize('error_code,stop_on_method', [51    (None, None),52    ('TestException', 'stub_put_method'),53    ('TestException', 'stub_put_method_response'),54    ('TestException', 'stub_put_integration'),55    ('TestException', 'stub_put_integration_response')])56def test_add_integration_method(make_stubber, stub_runner, error_code, stop_on_method):57    apigateway_client = boto3.client('apigateway')58    apigateway_stubber = make_stubber(apigateway_client)59    api_gtos = ApiGatewayToService(apigateway_client)60    api_gtos.api_id = 'test-api-id'61    resource_id = 'test-resource_id'62    rest_method = 'GET'63    service_endpoint_prefix = 'testservice'64    service_action = 'TestTheThing'65    service_method = 'POST'66    role_arn = 'arn:aws:iam:REGION:123456789012:role/test-role'67    mapping_template = {'test_name': 'test_value'}68    service_uri = (f'arn:aws:apigateway:{apigateway_client.meta.region_name}'69                   f':{service_endpoint_prefix}:action/{service_action}')70    with stub_runner(error_code, stop_on_method) as runner:71        runner.add(72            apigateway_stubber.stub_put_method, api_gtos.api_id, resource_id,73            http_method=rest_method)74        runner.add(75            apigateway_stubber.stub_put_method_response, api_gtos.api_id, resource_id,76            {'application/json': 'Empty'}, http_method=rest_method)77        runner.add(78            apigateway_stubber.stub_put_integration, api_gtos.api_id, resource_id,79            service_uri, http_method=rest_method, integ_type='AWS',80            integ_method=service_method, integ_role_arn=role_arn,81            integ_templates=mapping_template, passthrough='WHEN_NO_TEMPLATES')82        runner.add(83            apigateway_stubber.stub_put_integration_response, api_gtos.api_id,84            resource_id, {'application/json': ''}, http_method=rest_method)85    if error_code is None:86        api_gtos.add_integration_method(87            resource_id, rest_method, service_endpoint_prefix, service_action,88            service_method, role_arn, mapping_template)89    else:90        with pytest.raises(ClientError) as exc_info:91            api_gtos.add_integration_method(92                resource_id, rest_method, service_endpoint_prefix, service_action,93                service_method, role_arn, mapping_template)94        assert exc_info.value.response['Error']['Code'] == error_code95@pytest.mark.parametrize('error_code', [None, 'TestException'])96def test_deploy_api(make_stubber, monkeypatch, error_code):97    apigateway_client = boto3.client('apigateway')98    apigateway_stubber = make_stubber(apigateway_client)99    api_gtos = ApiGatewayToService(apigateway_client)100    api_gtos.api_id = 'test-api-id'101    api_url = 'https://test-url'102    monkeypatch.setattr(api_gtos, 'api_url', lambda: api_url)103    stage = 'test-stage'104    apigateway_stubber.stub_create_deployment(105        api_gtos.api_id, stage, error_code=error_code)106    if error_code is None:107        got_api_url = api_gtos.deploy_api(stage)108        assert got_api_url == api_url109    else:110        with pytest.raises(ClientError) as exc_info:111            api_gtos.deploy_api(stage)112        assert exc_info.value.response['Error']['Code'] == error_code113def test_api_url():114    apigateway_client = boto3.client('apigateway')115    api_gtos = ApiGatewayToService(apigateway_client)116    api_gtos.api_id = 'test-api-id'117    api_gtos.stage = 'test'118    url = (f'https://{api_gtos.api_id}.execute-api.{apigateway_client.meta.region_name}'119           f'.amazonaws.com/{api_gtos.stage}')120    assert api_gtos.api_url('thing') == f'{url}/thing'121@pytest.mark.parametrize('error_code', [None, 'TestException'])122def test_get_rest_api_id(make_stubber, error_code):123    apigateway_client = boto3.client('apigateway')124    apigateway_stubber = make_stubber(apigateway_client)125    api_gtos = ApiGatewayToService(apigateway_client)126    api_name = 'test-api_name-2'127    api_ids = [{'id': f'test-api-id-{index}', 'name': f'test-api_name-{index}'}128               for index in range(4)]129    apigateway_stubber.stub_get_rest_apis(api_ids, error_code=error_code)130    if error_code is None:131        got_api_id = api_gtos.get_rest_api_id(api_name)132        assert got_api_id == 'test-api-id-2'133    else:134        with pytest.raises(ClientError) as exc_info:135            api_gtos.get_rest_api_id(api_name)136        assert exc_info.value.response['Error']['Code'] == error_code137@pytest.mark.parametrize('error_code', [None, 'TestException'])138def test_delete_rest_api(make_stubber, error_code):139    apigateway_client = boto3.client('apigateway')140    apigateway_stubber = make_stubber(apigateway_client)141    api_gtos = ApiGatewayToService(apigateway_client)142    api_gtos.api_id = 'test-api-id'143    apigateway_stubber.stub_delete_rest_api(api_gtos.api_id, error_code=error_code)144    if error_code is None:145        api_gtos.delete_rest_api()146    else:147        with pytest.raises(ClientError) as exc_info:148            api_gtos.delete_rest_api()...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!!
