from aws_cdk.aws_lambda import Function, Code, Runtime
from aws_cdk.core import Stack, Duration
from b_aws_testing_framework.tools.cdk_testing.testing_stack import TestingStack
from b_cfn_lambda_layer.package_version import PackageVersion

from b_lambda_layer_common.layer import Layer
from b_lambda_layer_common_test.unit import root


class FunctionWithUnitTests(Function):
    """
    Function that lets us run unit tests inside lambda function. We want to run unit
    tests both locally and remotely.
    """
    def __init__(self, scope: Stack):
        super().__init__(
            scope=scope,
            id=f'{TestingStack.global_prefix()}FunctionWithUnitTests',
            code=Code.from_asset(root),
            handler='handler.handler',
            runtime=Runtime.PYTHON_3_8,
            timeout=Duration.minutes(5),
            memory_size=512,
            layers=[
                Layer(
                    scope=scope,
                    name=f'{TestingStack.global_prefix()}TestingLayerWithUnitTests',
                    dependencies={
                        # These dependencies are required for running unit tests inside lambda functions.
                        # Pytest is used for running actual unit tests.
                        'pytest': PackageVersion.from_string_version('6.2.5'),
                        # Pook is used for HTTP mocking, therefore it is also needed here.
                        'pook': PackageVersion.from_string_version('1.0.1'),
                        # Not sure about this dependency. Lambda runtime throws errors if its missing.
                        'aws-cdk.core': PackageVersion.from_string_version('1.99.0'),
                        # This dependency should be installed with 'pook' since it depends on 'jsonschema' which depends on this.
                        # For some reason it doesn't.
                        # Tests would fail with import error otherwise.
                        'importlib-resources': PackageVersion.from_string_version('5.4.0')
                    }
                )
            ]
        )

[((19, 17, 19, 38), 'aws_cdk.aws_lambda.Code.from_asset', 'Code.from_asset', ({(19, 33, 19, 37): 'root'}, {}), '(root)', False, 'from aws_cdk.aws_lambda import Function, Code, Runtime\n'), ((22, 20, 22, 39), 'aws_cdk.core.Duration.minutes', 'Duration.minutes', ({(22, 37, 22, 38): '(5)'}, {}), '(5)', False, 'from aws_cdk.core import Stack, Duration\n'), ((18, 18, 18, 46), 'b_aws_testing_framework.tools.cdk_testing.testing_stack.TestingStack.global_prefix', 'TestingStack.global_prefix', ({}, {}), '()', False, 'from b_aws_testing_framework.tools.cdk_testing.testing_stack import TestingStack\n'), ((31, 34, 31, 77), 'b_cfn_lambda_layer.package_version.PackageVersion.from_string_version', 'PackageVersion.from_string_version', ({(31, 69, 31, 76): '"""6.2.5"""'}, {}), "('6.2.5')", False, 'from b_cfn_lambda_layer.package_version import PackageVersion\n'), ((33, 32, 33, 75), 'b_cfn_lambda_layer.package_version.PackageVersion.from_string_version', 'PackageVersion.from_string_version', ({(33, 67, 33, 74): '"""1.0.1"""'}, {}), "('1.0.1')", False, 'from b_cfn_lambda_layer.package_version import PackageVersion\n'), ((35, 40, 35, 84), 'b_cfn_lambda_layer.package_version.PackageVersion.from_string_version', 'PackageVersion.from_string_version', ({(35, 75, 35, 83): '"""1.99.0"""'}, {}), "('1.99.0')", False, 'from b_cfn_lambda_layer.package_version import PackageVersion\n'), ((39, 47, 39, 90), 'b_cfn_lambda_layer.package_version.PackageVersion.from_string_version', 'PackageVersion.from_string_version', ({(39, 82, 39, 89): '"""5.4.0"""'}, {}), "('5.4.0')", False, 'from b_cfn_lambda_layer.package_version import PackageVersion\n'), ((27, 28, 27, 56), 'b_aws_testing_framework.tools.cdk_testing.testing_stack.TestingStack.global_prefix', 'TestingStack.global_prefix', ({}, {}), '()', False, 'from b_aws_testing_framework.tools.cdk_testing.testing_stack import TestingStack\n')]