Best Python code snippet using localstack_python
test_templating.py
Source:test_templating.py  
...25class TestMessageTransformation(unittest.TestCase):26    def test_array_size(self):27        template = "#set($list = $input.path('$.records')) $list.size()"28        context = {"records": [{"data": {"foo": "bar1"}}, {"data": {"foo": "bar2"}}]}29        result = render_velocity_template(template, context)30        self.assertEqual(" 2", result)31        result = render_velocity_template(template, json.dumps(context))32        self.assertEqual(" 2", result)33    def test_message_transformation(self):34        template = APIGATEWAY_TRANSFORMATION_TEMPLATE35        records = [36            {"data": {"foo": "foo1", "bar": "bar2"}},37            {"data": {"foo": "foo1", "bar": "bar2"}, "partitionKey": "key123"},38        ]39        context = {"records": records}40        def do_test(context):41            result = render_velocity_template(template, context, as_json=True)42            result_decoded = json.loads(to_str(base64.b64decode(result["Records"][0]["Data"])))43            self.assertEqual(records[0]["data"], result_decoded)44            self.assertGreater(len(result["Records"][0]["PartitionKey"]), 0)45            self.assertEqual("key123", result["Records"][1]["PartitionKey"])46        # try rendering the template47        do_test(context)48        # try again with context as string49        do_test(json.dumps(context))50        # test with empty array51        records = []52        context = {"records": records}53        # try rendering the template54        result = render_velocity_template(template, context, as_json=True)55        self.assertEqual([], result["Records"])56    def test_array_in_set_expr(self):57        template = "#set ($bar = $input.path('$.foo')[1]) \n $bar"58        context = {"foo": ["e1", "e2", "e3", "e4"]}59        result = render_velocity_template(template, context).strip()60        self.assertEqual("e2", result)61        template = "#set ($bar = $input.path('$.foo')[1][1][1]) $bar"62        context = {"foo": [["e1"], ["e2", ["e3", "e4"]]]}63        result = render_velocity_template(template, context).strip()64        self.assertEqual("e4", result)65    def test_string_methods(self):66        context = {"foo": {"bar": "BAZ baz"}}67        template1 = "${foo.bar.strip().lower().replace(' ','-')}"68        template2 = "${foo.bar.trim().toLowerCase().replace(' ','-')}"69        for template in [template1, template2]:70            result = render_velocity_template(template, {}, variables=context)71            self.assertEqual("baz-baz", result)72    def test_render_urlencoded_string_data(self):73        template = "MessageBody=$util.base64Encode($input.json('$'))"74        result = render_velocity_template(template, b'{"spam": "eggs"}')...test_message_transformation.py
Source:test_message_transformation.py  
...29        }, {30            'data': {'foo': 'bar2'}31        }]32    }33    result = render_velocity_template(template, context)34    assert(result == ' 2')35    result = render_velocity_template(template, json.dumps(context))36    assert(result == ' 2')37def test_message_transformation():38    template = APIGATEWAY_TRANSFORMATION_TEMPLATE39    records = [40        {41            'data': {42                'foo': 'foo1',43                'bar': 'bar2'44            }45        },46        {47            'data': {48                'foo': 'foo1',49                'bar': 'bar2'50            },51            'partitionKey': 'key123'52        }53    ]54    context = {55        'records': records56    }57    # try rendering the template58    result = render_velocity_template(template, context, as_json=True)59    result_decoded = json.loads(to_str(base64.b64decode(result['Records'][0]['Data'])))60    assert result_decoded == records[0]['data']61    assert len(result['Records'][0]['PartitionKey']) > 062    assert result['Records'][1]['PartitionKey'] == 'key123'63    # try again with context as string64    context = json.dumps(context)65    result = render_velocity_template(template, context, as_json=True)66    result_decoded = json.loads(to_str(base64.b64decode(result['Records'][0]['Data'])))67    assert result_decoded == records[0]['data']68    assert len(result['Records'][0]['PartitionKey']) > 069    assert result['Records'][1]['PartitionKey'] == 'key123'70    # test with empty array71    records = []72    context = {73        'records': records74    }75    # try rendering the template76    result = render_velocity_template(template, context, as_json=True)...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!!
