Best Python code snippet using localstack_python
test_templating.py
Source:test_templating.py  
...79            "input": {80                "body": body,81            },82        }83        result = velocity_template.render_vtl(template, variables)84        assert result == " 2"85    def test_message_transformation(self, velocity_template):86        template = APIGW_TEMPLATE_TRANSFORM_KINESIS87        records = [88            {"data": {"foo": "foo1", "bar": "bar2"}},89            {"data": {"foo": "foo1", "bar": "bar2"}, "partitionKey": "key123"},90        ]91        variables = {"input": {"body": {"records": records}}}92        def do_test(variables):93            result = velocity_template.render_vtl(template, variables, as_json=True)94            result_decoded = json.loads(to_str(base64.b64decode(result["Records"][0]["Data"])))95            assert result_decoded == records[0]["data"]96            assert result["Records"][0]["PartitionKey"] == "$elem.partitionKey"97            assert result["Records"][1]["PartitionKey"] == "key123"98        # try rendering the template99        do_test(variables)100        # test with empty array101        records = []102        variables = {"input": {"body": {"records": records}}}103        # try rendering the template104        result = velocity_template.render_vtl(template, variables, as_json=True)105        assert result["Records"] == []106    def test_array_in_set_expr(self, velocity_template):107        template = "#set ($bar = $input.path('$.foo')[1]) \n $bar"108        variables = {"input": {"body": {"foo": ["e1", "e2", "e3", "e4"]}}}109        result = velocity_template.render_vtl(template, variables).strip()110        assert result == "e2"111        template = "#set ($bar = $input.path('$.foo')[1][1][1]) $bar"112        variables = {"input": {"body": {"foo": [["e1"], ["e2", ["e3", "e4"]]]}}}113        result = velocity_template.render_vtl(template, variables).strip()114        assert result == "e4"115    def test_string_methods(self, velocity_template):116        context = {"foo": {"bar": "BAZ baz"}}117        variables = {"input": {"body": context}}118        template1 = "${foo.bar.strip().lower().replace(' ','-')}"119        template2 = "${foo.bar.trim().toLowerCase().replace(' ','-')}"120        for template in [template1, template2]:121            result = velocity_template.render_vtl(template, variables=variables)122            assert result == "baz-baz"123    def test_render_urlencoded_string_data(self, velocity_template):124        template = "MessageBody=$util.base64Encode($input.json('$'))"125        variables = {"input": {"body": {"spam": "eggs"}}}126        result = velocity_template.render_vtl(template, variables)127        assert result == "MessageBody=eyJzcGFtIjogImVnZ3MifQ=="128    def test_construct_json_using_define(self, velocity_template):129        template = APIGW_TEMPLATE_CONSTRUCT_JSON130        data = {"p1": {"test": 123}, "p2": {"foo": "bar", "foo2": False}}131        variables = {"input": {"body": data}}132        result = velocity_template.render_vtl(template, variables).strip()133        result = json.loads(result)134        assert result == {"p0": True, **data}135    def test_keyset_functions(self, velocity_template):136        template = "#set($list = $input.path('$..var1[1]').keySet()) #foreach($e in $list)$e#end"137        body = {"var1": [{"a": 1}, {"b": 2}]}138        variables = {"input": {"body": body}}139        result = velocity_template.render_vtl(template, variables)...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!!
