Best Python code snippet using localstack_python
test_core.py
Source:test_core.py
1import pytest2import core3from boto3.exceptions import botocore4from botocore.exceptions import ClientError5def test_list_function_versions(mocker):6 mocker.patch.object(core.LAMBDA_CLIENT, 'list_versions_by_function')7 core.list_function_versions()8 core.LAMBDA_CLIENT.list_versions_by_function.assert_called_with(FunctionName='test function', MaxItems=100)9def test_list_function_versions_return_none(mocker):10 mocker.patch.object(core.LAMBDA_CLIENT, 'list_versions_by_function')11 core.LAMBDA_CLIENT.list_versions_by_function.side_effect = ClientError({'Error': {'Code': 'ResourceNotFoundException'}}, 'list_versions_by_function')12 assert core.list_function_versions() == None13def test_versions_to_delete_none(mocker):14 versions_response = {15 'Versions': [16 { 'Version': '$LATEST', 'LastModified': 100},17 { 'Version': 2, 'LastModified': 99}18 ]19 }20 assert core.versions_to_delete(versions_response) == None21def test_versions_to_delete_less_than_10(mocker):22 versions_response = {23 'Versions': [24 { 'Version': '$LATEST', 'LastModified': 100},25 { 'Version': 2, 'LastModified': 99},26 { 'Version': 3, 'LastModified': 98},27 { 'Version': 4, 'LastModified': 97},28 { 'Version': 5, 'LastModified': 96},29 { 'Version': 6, 'LastModified': 95},30 { 'Version': 7, 'LastModified': 94},31 { 'Version': 8, 'LastModified': 93},32 { 'Version': 9, 'LastModified': 92},33 { 'Version': 10, 'LastModified':91},34 { 'Version': 11, 'LastModified': 90},35 { 'Version': 12, 'LastModified': 89},36 { 'Version': 13, 'LastModified': 88},37 { 'Version': 14, 'LastModified': 87},38 { 'Version': 15, 'LastModified': 86}39 ]40 }41 assert core.versions_to_delete(versions_response) == [15, 14, 13, 12, 11 ]42def test_versions_to_delete_not_latest(mocker):43 versions_response = {44 'Versions': [45 { 'Version': 1, 'LastModified': 100},46 { 'Version': 2, 'LastModified': 99},47 { 'Version': 3, 'LastModified': 98},48 { 'Version': 4, 'LastModified': 97},49 { 'Version': 5, 'LastModified': 96},50 { 'Version': 6, 'LastModified': 95},51 { 'Version': 7, 'LastModified': 94},52 { 'Version': 8, 'LastModified': 93},53 { 'Version': 9, 'LastModified': 92},54 { 'Version': 10, 'LastModified':91},55 { 'Version': 11, 'LastModified': 90},56 { 'Version': 12, 'LastModified': 89},57 { 'Version': 13, 'LastModified': 88},58 { 'Version': 14, 'LastModified': 87},59 { 'Version': '$LATEST', 'LastModified': 86}60 ]61 }62 assert core.versions_to_delete(versions_response) == [14, 13, 12, 11, 10]63def test_delete_function_versions(mocker):64 mocker.patch.object(core.LAMBDA_CLIENT, 'delete_function')65 core.delete_function_version(100)...
lambdaapi.py
Source:lambdaapi.py
2class LambdaAPI(object):3 def __init__(self, session):4 self.client = session.client('lambda')5 @ensure_http_success6 def list_versions_by_function(self, function_name, **_):7 return self.client.list_versions_by_function(8 FunctionName=function_name,9 )10 @ensure_http_success11 def list_aliases(self, function_name, **_):12 return self.client.list_aliases(FunctionName=function_name)13 @ensure_http_success14 def update_alias(self, function_name, alias_name, function_version, alias_desc=None, **_):15 params = {16 "FunctionName": function_name,17 "Name": alias_name,18 "FunctionVersion": function_version,19 "Description": alias_desc,20 }21 return self.client.update_alias(**{k: v for k, v in params.items() if v})...
clean_lambda.py
Source:clean_lambda.py
...3def clean_old_lambda_versions():4 client = boto3.client('lambda')5 functions = client.list_functions()['Functions']6 for function in functions:7 versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']8 for version in versions:9 if version['Version'] != function['Version']:10 arn = version['FunctionArn']11 print('delete_function(FunctionName={})'.format(arn))12 try:13 client.delete_function(FunctionName=arn) # uncomment me once you've checked14 except:15 print ("Could not delete_function")16def clean_old_lambda_versions():17 client = boto3.client('lambda')18 functions = client.list_functions()['Functions']19 for function in functions:20 while True:21 versions = client.list_versions_by_function(FunctionName=function['FunctionArn'])['Versions']22 if len(versions) == 2:23 print('{}: done'.format(function['FunctionName']))24 break25 for version in versions:26 if version['Version'] != function['Version']:27 arn = version['FunctionArn']28 print('delete_function(FunctionName={})'.format(arn))29 try:30 client.delete_function(FunctionName=arn) # uncomment me once you've checked31 except:32 print ("Could not delete_function")33if __name__ == '__main__':...
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!!