Best Python code snippet using localstack_python
test_moto.py
Source:test_moto.py
...6from localstack.utils.common import short_uid, to_str7def test_call_with_sqs_creates_state_correctly():8 qname = f"queue-{short_uid()}"9 response = moto.call_moto(10 moto.create_aws_request_context("sqs", "CreateQueue", {"QueueName": qname}),11 include_response_metadata=True,12 )13 url = response["QueueUrl"]14 try:15 assert response["ResponseMetadata"]["HTTPStatusCode"] == 20016 assert response["QueueUrl"].endswith(f"/{qname}")17 response = moto.call_moto(moto.create_aws_request_context("sqs", "ListQueues"))18 assert url in response["QueueUrls"]19 finally:20 moto.call_moto(moto.create_aws_request_context("sqs", "DeleteQueue", {"QueueUrl": url}))21 response = moto.call_moto(moto.create_aws_request_context("sqs", "ListQueues"))22 assert url not in response.get("QueueUrls", [])23def test_call_sqs_invalid_call_raises_http_exception():24 with pytest.raises(ServiceException) as e:25 moto.call_moto(26 moto.create_aws_request_context(27 "sqs",28 "DeleteQueue",29 {30 "QueueUrl": "http://0.0.0.0/nonexistingqueue",31 },32 )33 )34 e.match("The specified queue does not exist")35def test_call_non_implemented_operation():36 with pytest.raises(NotImplementedError):37 # we'll need to keep finding methods that moto doesn't implement ;-)38 moto.call_moto(39 moto.create_aws_request_context("athena", "DeleteDataCatalog", {"Name": "foo"})40 )41def test_proxy_non_implemented_operation():42 with pytest.raises(NotImplementedError):43 moto.proxy_moto(44 moto.create_aws_request_context("athena", "DeleteDataCatalog", {"Name": "foo"})45 )46def test_call_with_sqs_modifies_state_in_moto_backend():47 """Whitebox test to check that moto backends are populated correctly"""48 from moto.sqs.models import sqs_backends49 qname = f"queue-{short_uid()}"50 response = moto.call_moto(51 moto.create_aws_request_context("sqs", "CreateQueue", {"QueueName": qname})52 )53 url = response["QueueUrl"]54 assert qname in sqs_backends.get(config.AWS_REGION_US_EAST_1).queues55 moto.call_moto(moto.create_aws_request_context("sqs", "DeleteQueue", {"QueueUrl": url}))56 assert qname not in sqs_backends.get(config.AWS_REGION_US_EAST_1).queues57def test_call_include_response_metadata():58 ctx = moto.create_aws_request_context("sqs", "ListQueues")59 response = moto.call_moto(ctx)60 assert "ResponseMetadata" not in response61 response = moto.call_moto(ctx, include_response_metadata=True)62 assert "ResponseMetadata" in response63def test_call_with_modified_request():64 from moto.sqs.models import sqs_backends65 qname1 = f"queue-{short_uid()}"66 qname2 = f"queue-{short_uid()}"67 context = moto.create_aws_request_context("sqs", "CreateQueue", {"QueueName": qname1})68 response = moto.call_moto_with_request(context, {"QueueName": qname2}) # overwrite old request69 url = response["QueueUrl"]70 assert qname2 in sqs_backends.get(config.AWS_REGION_US_EAST_1).queues71 assert qname1 not in sqs_backends.get(config.AWS_REGION_US_EAST_1).queues72 moto.call_moto(moto.create_aws_request_context("sqs", "DeleteQueue", {"QueueUrl": url}))73def test_call_with_es_creates_state_correctly():74 domain_name = f"domain-{short_uid()}"75 response = moto.call_moto(76 moto.create_aws_request_context(77 "es",78 "CreateElasticsearchDomain",79 {80 "DomainName": domain_name,81 "ElasticsearchVersion": "7.10",82 },83 ),84 include_response_metadata=True,85 )86 try:87 assert response["ResponseMetadata"]["HTTPStatusCode"] == 20088 assert response["DomainStatus"]["DomainName"] == domain_name89 assert response["DomainStatus"]["ElasticsearchVersion"] == "7.10"90 finally:91 response = moto.call_moto(92 moto.create_aws_request_context(93 "es", "DeleteElasticsearchDomain", {"DomainName": domain_name}94 ),95 include_response_metadata=True,96 )97 assert response["ResponseMetadata"]["HTTPStatusCode"] == 20098def test_call_multi_region_backends():99 from moto.sqs.models import sqs_backends100 qname_us = f"queue-us-{short_uid()}"101 qname_eu = f"queue-eu-{short_uid()}"102 moto.call_moto(103 moto.create_aws_request_context(104 "sqs", "CreateQueue", {"QueueName": qname_us}, region="us-east-1"105 )106 )107 moto.call_moto(108 moto.create_aws_request_context(109 "sqs", "CreateQueue", {"QueueName": qname_eu}, region="eu-central-1"110 )111 )112 assert qname_us in sqs_backends.get("us-east-1").queues113 assert qname_eu not in sqs_backends.get("us-east-1").queues114 assert qname_us not in sqs_backends.get("eu-central-1").queues115 assert qname_eu in sqs_backends.get("eu-central-1").queues116 del sqs_backends.get("us-east-1").queues[qname_us]117 del sqs_backends.get("eu-central-1").queues[qname_eu]118def test_proxy_with_sqs_invalid_call_returns_error():119 response = moto.proxy_moto(120 moto.create_aws_request_context(121 "sqs",122 "DeleteQueue",123 {124 "QueueUrl": "http://0.0.0.0/nonexistingqueue",125 },126 )127 )128 assert response.status_code == 400129 assert "NonExistentQueue" in to_str(response.data)130def test_proxy_with_sqs_returns_http_response():131 qname = f"queue-{short_uid()}"132 response = moto.proxy_moto(133 moto.create_aws_request_context("sqs", "CreateQueue", {"QueueName": qname})134 )135 assert response.status_code == 200136 assert f"{qname}</QueueUrl>" in to_str(response.data)137 assert "x-amzn-requestid" in response.headers138class FakeSqsApi:139 @handler("ListQueues", expand=False)140 def list_queues(self, context, request):141 raise NotImplementedError142 @handler("CreateQueue", expand=False)143 def create_queue(self, context, request):144 raise NotImplementedError145class FakeSqsProvider(FakeSqsApi):146 def __init__(self) -> None:147 super().__init__()148 self.calls = []149 @handler("ListQueues", expand=False)150 def list_queues(self, context, request):151 self.calls.append(context)152 return moto.call_moto(context)153def test_moto_fallback_dispatcher():154 provider = FakeSqsProvider()155 dispatcher = MotoFallbackDispatcher(provider)156 assert "ListQueues" in dispatcher157 assert "CreateQueue" in dispatcher158 def _dispatch(action, params):159 context = moto.create_aws_request_context("sqs", action, params)160 return dispatcher[action](context, params)161 qname = f"queue-{short_uid()}"162 # when falling through the dispatcher returns an HTTP response163 http_response = _dispatch("CreateQueue", {"QueueName": qname})164 assert http_response.status_code == 200165 # this returns an166 response = _dispatch("ListQueues", None)167 assert len(provider.calls) == 1...
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!!