Best Python code snippet using localstack_python
transformer.py
Source:transformer.py
...17 self.replacements = []18 self.scoped_tokens = {}19 self._cache = {}20 @property21 def serialized_replacements(self) -> list[GlobalReplacementFn]: # TODO: naming22 return self.replacements23 def register_serialized_replacement(self, fn: GlobalReplacementFn): # TODO: naming24 self.replacements.append(fn)25 def new_scope(self, scope: str) -> int:26 """retrieve new enumeration value for a given scope key (e.g. for tokens such as <fn-name:1>"""27 current_counter = self.scoped_tokens.setdefault(scope, 1)28 self.scoped_tokens[scope] += 129 return current_counter30def _register_serialized_reference_replacement(31 transform_context: TransformContext, *, reference_value: str, replacement: str32):33 if '"' in reference_value:34 reference_value = reference_value.replace('"', '\\"')35 cache = transform_context._cache.setdefault("regexcache", set())...
test_transformer.py
Source:test_transformer.py
1import copy2import json3import pytest4from localstack.testing.snapshots.transformer import TransformContext5from localstack.testing.snapshots.transformer_utility import TransformerUtility6class TestTransformer:7 def test_key_value_replacement(self):8 input = {9 "hello": "world",10 "hello2": "again",11 "path": {"to": {"anotherkey": "hi", "inside": {"hello": "inside"}}},12 }13 key_value = TransformerUtility.key_value(14 "hello", "placeholder", reference_replacement=False15 )16 expected_key_value = {17 "hello": "placeholder",18 "hello2": "again",19 "path": {"to": {"anotherkey": "hi", "inside": {"hello": "placeholder"}}},20 }21 copied = copy.deepcopy(input)22 ctx = TransformContext()23 assert key_value.transform(copied, ctx=ctx) == expected_key_value24 assert ctx.serialized_replacements == []25 copied = copy.deepcopy(input)26 key_value = TransformerUtility.key_value("hello", "placeholder", reference_replacement=True)27 expected_key_value_reference = {28 "hello": "<placeholder:1>",29 "hello2": "again",30 "path": {"to": {"anotherkey": "hi", "<placeholder:2>": {"hello": "<placeholder:2>"}}},31 }32 assert key_value.transform(copied, ctx=ctx) == copied33 assert len(ctx.serialized_replacements) == 234 tmp = json.dumps(copied, default=str)35 for sr in ctx.serialized_replacements:36 tmp = sr(tmp)37 assert json.loads(tmp) == expected_key_value_reference38 @pytest.mark.parametrize("type", ["key_value", "jsonpath"])39 def test_replacement_with_reference(self, type):40 input = {41 "also-me": "b",42 "path": {43 "to": {"anotherkey": "hi", "test": {"hello": "replaceme"}},44 "another": {"key": "this/replaceme/hello"},45 },46 "b": {"a/b/replaceme.again": "bb"},47 "test": {"inside": {"path": {"to": {"test": {"hello": "also-me"}}}}},48 }49 expected = {50 "<MYVALUE:2>": "b",51 "path": {52 "to": {"anotherkey": "hi", "test": {"hello": "<MYVALUE:1>"}},53 "another": {"key": "this/<MYVALUE:1>/hello"},54 },55 "b": {"a/b/<MYVALUE:1>.again": "bb"},56 "test": {"inside": {"path": {"to": {"test": {"hello": "<MYVALUE:2>"}}}}},57 }58 replacement = "MYVALUE"59 if type == "key_value":60 transformer = TransformerUtility.key_value(61 "hello", replacement, reference_replacement=True62 )63 else:64 transformer = TransformerUtility.jsonpath(65 "$..path.to.test.hello", replacement, reference_replacement=True66 )67 copied = copy.deepcopy(input)68 ctx = TransformContext()69 assert transformer.transform(copied, ctx=ctx) == copied70 assert len(ctx.serialized_replacements) == 271 tmp = json.dumps(copied, default=str)72 for sr in ctx.serialized_replacements:73 tmp = sr(tmp)74 assert json.loads(tmp) == expected75 def test_regex(self):76 input = {77 "hello": "world",78 "hello2": "again",79 "path": {"to": {"anotherkey": "hi", "inside": {"hello": "inside"}}},80 }81 expected = {82 "new-value": "world",83 "new-value2": "again",84 "path": {"to": {"anotherkey": "hi", "inside": {"new-value": "inside"}}},85 }86 transformer = TransformerUtility.regex("hello", "new-value")87 ctx = TransformContext()88 output = transformer.transform(json.dumps(input), ctx=ctx)89 for sr in ctx.serialized_replacements:90 output = sr(output)91 assert json.loads(output) == expected92 def test_log_stream_name(self):93 input = {94 "Payload": {95 "context": {96 "functionVersion": "$LATEST",97 "functionName": "my-function",98 "memoryLimitInMB": "128",99 "logGroupName": "/aws/lambda/my-function",100 "logStreamName": "2022/05/31/[$LATEST]ced3cafaaf284d8199e02909ac87e2f5",101 "clientContext": {102 "custom": {"foo": "bar"},103 "client": {"snap": ["crackle", "pop"]},104 "env": {"fizz": "buzz"},105 },106 "invokedFunctionArn": "arn:aws:lambda:us-east-1:111111111111:function:my-function",107 }108 }109 }110 transformers = TransformerUtility.lambda_api()111 ctx = TransformContext()112 for t in transformers:113 t.transform(input, ctx=ctx)114 output = json.dumps(input)115 for sr in ctx.serialized_replacements:116 output = sr(output)117 expected = {118 "Payload": {119 "context": {120 "functionVersion": "$LATEST",121 "functionName": "<resource:1>",122 "memoryLimitInMB": "128",123 "logGroupName": "/aws/lambda/<resource:1>",124 "logStreamName": "<log-stream-name:1>",125 "clientContext": {126 "custom": {"foo": "bar"},127 "client": {"snap": ["crackle", "pop"]},128 "env": {"fizz": "buzz"},129 },130 "invokedFunctionArn": "arn:aws:lambda:us-east-1:111111111111:function:<resource:1>",131 }132 }133 }...
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!!