Best Python code snippet using localstack_python
test_python_aws_ssm.py
Source:test_python_aws_ssm.py  
...56                {"Name": "/bar/env/foo_ssm_key_1", "Value": "foo_ssm_value_1"},57                {"Name": "/bar/env/foo_ssm_key_2", "Value": "foo_ssm_value_2"},58            ]59        }60        secrets = self.parameter_store.get_parameters_by_path("/bar/env/")61        self.assertEqual(62            {"foo_ssm_key_1": "foo_ssm_value_1", "foo_ssm_key_2": "foo_ssm_value_2"},63            secrets,64        )65        self.parameter_store.client.get_parameters_by_path.assert_called_once_with(66            Path="/bar/env/", Recursive=False, WithDecryption=True67        )68    def test_get_parameters_by_path_are_stripped_of_leading_slashes(self):69        """70        Leading slashes of parameters are stripped consistently.71        When requesting parameters by a path that is not recursive and nested,72        the leading slashes should be consistently stripped. In versions <= 0.1.2,73        the leading slashes were not included if the parameter path ended in a74        trailing slash, but not if the parameter path ended without a trailing75        slashâ¦76        """77        self.parameter_store.client.get_parameters_by_path.return_value = {78            "Parameters": [79                {"Name": "/bar/env/foo_ssm_key_1", "Value": "foo_ssm_value_1"},80                {"Name": "/bar/env/foo_ssm_key_2", "Value": "foo_ssm_value_2"},81            ]82        }83        # Note that the requested path has no trailing slash.84        parameters_path = "/bar/env"85        secrets = self.parameter_store.get_parameters_by_path(parameters_path)86        self.assertEqual(87            {"foo_ssm_key_1": "foo_ssm_value_1", "foo_ssm_key_2": "foo_ssm_value_2"},88            secrets,89        )90        self.parameter_store.client.get_parameters_by_path.assert_called_once_with(91            Path=parameters_path, Recursive=False, WithDecryption=True92        )93    def test_get_parameters_by_path_recursive_not_nested(self):94        self.parameter_store.client.get_parameters_by_path.return_value = {95            "Parameters": [96                {"Name": "/bar/env/foo_ssm_key_1", "Value": "foo_ssm_value_1"},97                {"Name": "/bar/env/foo_ssm_key_2", "Value": "foo_ssm_value_2"},98            ]99        }100        secrets = self.parameter_store.get_parameters_by_path(101            "/bar/", recursive=True, nested=False102        )103        self.assertEqual(104            {105                "env/foo_ssm_key_1": "foo_ssm_value_1",106                "env/foo_ssm_key_2": "foo_ssm_value_2",107            },108            secrets,109        )110        self.parameter_store.client.get_parameters_by_path.assert_called_once_with(111            Path="/bar/", Recursive=True, WithDecryption=True112        )113    def test_get_parameters_by_path_recursive_nested(self):114        self.parameter_store.client.get_parameters_by_path.return_value = {115            "Parameters": [116                {"Name": "/bar/env/foo_ssm_key_1", "Value": "foo_ssm_value_1"},117                {"Name": "/bar/env/foo_ssm_key_2", "Value": "foo_ssm_value_2"},118            ]119        }120        secrets = self.parameter_store.get_parameters_by_path(121            "/bar/", recursive=True, nested=True122        )123        self.assertEqual(124            {125                "env": {126                    "foo_ssm_key_1": "foo_ssm_value_1",127                    "foo_ssm_key_2": "foo_ssm_value_2",128                }129            },130            secrets,131        )132        self.parameter_store.client.get_parameters_by_path.assert_called_once_with(133            Path="/bar/", Recursive=True, WithDecryption=True134        )135    def test_get_parameter_by_path_aws_errors_are_not_caught(self):136        expected_error = Exception("Unexpected AWS error!")137        self.parameter_store.client.get_parameters_by_path.side_effect = expected_error138        with self.assertRaises(Exception, msg="Unexpected AWS error!"):139            self.parameter_store.get_parameters_by_path(["/key"])140    def test_get_required_parameters_by_path_can_be_asserted(self) -> None:141        """142        Required parameters that are missing from a path result in an error.143        """144        self.parameter_store.client.get_parameters_by_path.return_value = {145            "Parameters": [146                # Only one of the required parameters is returned.147                {"Name": "/path/sub/key", "Value": "foo_ssm_value_1"},148                {"Name": "/path/sub/key2", "Value": "foo_ssm_value_2"},149            ]150        }151        expected_msg = "Missing parameters [baz, foo/bar] on path /path/sub/"152        with self.assertRaises(MissingParameterError, msg=expected_msg) as exc_info:153            self.parameter_store.get_parameters_by_path(154                "/path/sub/", required_parameters={"baz", "foo/bar", "key"}155            )156        assert exc_info.exception.parameter_path == "/path/sub/"157        assert len(exc_info.exception.parameter_names) == 2158        assert sorted(exc_info.exception.parameter_names) == sorted(["baz", "foo/bar"])159    def test_required_parameters_by_path_are_checked_before_recursive_nested(self):160        self.parameter_store.client.get_parameters_by_path.return_value = {161            "Parameters": [162                {"Name": "/bar/env/foo_ssm_key_1", "Value": "foo_ssm_value_1"},163                {"Name": "/bar/env/foo_ssm_key_2", "Value": "foo_ssm_value_2"},164            ]165        }166        secrets = self.parameter_store.get_parameters_by_path(167            "/bar/",168            recursive=True,169            nested=True,170            required_parameters={"env/foo_ssm_key_1", "env/foo_ssm_key_2"},171        )172        self.assertEqual(173            {174                "env": {175                    "foo_ssm_key_1": "foo_ssm_value_1",176                    "foo_ssm_key_2": "foo_ssm_value_2",177                }178            },179            secrets,180        )...test_main.py
Source:test_main.py  
1import unittest2from unittest.mock import call, patch3import aws_paramstore_py as paramstore4@patch('aws_paramstore_py.main.boto3')5class TestMain(unittest.TestCase):6    def test_get(self, mock):7        method = mock.client('ssm').get_parameters_by_path8        method.return_value = {'Parameters': [9            {'Name': '/path/to/params/key1', 'Value': "value1"},10            {'Name': '/path/to/params/key2', 'Value': "value2"}11        ]}12        params = paramstore.get('path', 'to', 'params')13        method.assert_called_with(Path='/path/to/params/', Recursive=True, WithDecryption=False)14        self.assertDictEqual({"key1": "value1", "key2": "value2"}, params)15    def test_get_root(self, mock):16        method = mock.client('ssm').get_parameters_by_path17        paramstore.get()18        method.assert_called_with(Path='/', Recursive=True, WithDecryption=False)19    def test_get_slash(self, mock):20        method = mock.client('ssm').get_parameters_by_path21        method.return_value = {'Parameters': [22            {'Name': '/path/to/params/key1', 'Value': "value1"},23            {'Name': '/path/to/params/key2', 'Value': "value2"},24            {'Name': 'root-key3', 'Value': "value3"}25        ]}26        params = paramstore.get('/')27        method.assert_called_with(Path='/', Recursive=True, WithDecryption=False)28        self.assertDictEqual({29            "path/to/params/key1": "value1",30            "path/to/params/key2": "value2",31            "root-key3": "value3"32        }, params)33    def test_get_leading_slash(self, mock):34        method = mock.client('ssm').get_parameters_by_path35        paramstore.get('/path/to')36        method.assert_called_with(Path='/path/to/', Recursive=True, WithDecryption=False)37    def test_get_following_slash(self, mock):38        method = mock.client('ssm').get_parameters_by_path39        paramstore.get('path/to/')40        method.assert_called_with(Path='/path/to/', Recursive=True, WithDecryption=False)41    def test_get_with_decryption(self, mock):42        method = mock.client('ssm').get_parameters_by_path43        paramstore.get('path/to/params', decryption=True)44        method.assert_called_with(Path='/path/to/params/', Recursive=True, WithDecryption=True)45    def test_get_with_next_token(self, mock):46        method = mock.client('ssm').get_parameters_by_path47        method.side_effect = [{48            'Parameters': [49                {'Name': '/path/to/params/key1', 'Value': "value1"}50            ],51            'NextToken': 'ThisIsNextToken1'52        }, {53            'Parameters': [54                {'Name': '/path/to/params/key2', 'Value': "value2"}55            ],56            'NextToken': 'ThisIsNextToken2'57        }, {58            'Parameters': [59                {'Name': '/path/to/params/key3', 'Value': "value3"}60            ]61        }]62        params = paramstore.get('path/to/params')63        method.assert_has_calls([64            call(Path='/path/to/params/', Recursive=True, WithDecryption=False),65            call(Path='/path/to/params/', Recursive=True, WithDecryption=False, NextToken='ThisIsNextToken1'),66            call(Path='/path/to/params/', Recursive=True, WithDecryption=False, NextToken='ThisIsNextToken2')67        ])68        self.assertDictEqual({69            "key1": "value1",70            "key2": "value2",71            "key3": "value3"...test_parameterstore.py
Source:test_parameterstore.py  
1import importlib2import sys3from unittest import mock4import pytest5from botocore.exceptions import BotoCoreError6from prettyconf.loaders import AwsParameterStore7PARAMETER_RESPONSE = {8    "Parameters": [9        {10            "Name": "DEBUG",11            "Type": "String",12            "Value": "false",13        },14        {15            "Name": "HOST",16            "Type": "String",17            "Value": "host_url",18        },19    ],20}21PARAMETER_RESPONSE_FIRST_PAGE = {22    "Parameters": [23        {24            "Name": "/api/DEBUG",25            "Type": "String",26            "Value": "false",27        },28    ],29    "NextToken": "token",30}31PARAMETER_RESPONSE_LAST_PAGE = {32    "Parameters": [33        {34            "Name": "/api/HOST",35            "Type": "String",36            "Value": "host_url",37        },38    ],39}40@pytest.fixture41def boto_not_installed():42    sys.modules['boto3'] = None43    importlib.reload(sys.modules['prettyconf.loaders'])44    yield45    sys.modules.pop('boto3')46    importlib.reload(sys.modules['prettyconf.loaders'])47def test_create_loader_boto_not_installed(boto_not_installed):48    with pytest.raises(RuntimeError):49        AwsParameterStore()50@mock.patch("prettyconf.loaders.boto3")51def test_basic_config(mock_boto):52    mock_boto.client.return_value.get_parameters_by_path.return_value = PARAMETER_RESPONSE53    config = AwsParameterStore()54    assert "HOST" in config55    assert config["HOST"] == "host_url"56    assert repr(config).startswith("AwsParameterStore(path=")57    mock_boto.client.return_value.get_parameters_by_path.assert_called_with(Path="/")58@mock.patch("prettyconf.loaders.boto3")59def test_basic_config_response_paginated(mock_boto):60    mock_boto.client.return_value.get_parameters_by_path.side_effect = [61        PARAMETER_RESPONSE_FIRST_PAGE, PARAMETER_RESPONSE_LAST_PAGE,62    ]63    config = AwsParameterStore(path="/api")64    assert "HOST" in config65    assert "DEBUG" in config66    assert config["HOST"] == "host_url"67    assert config["DEBUG"] == "false"68    assert mock_boto.client.return_value.get_parameters_by_path.call_count == 269    mock_boto.client.return_value.get_parameters_by_path.assert_called_with(NextToken="token", Path="/api")70@mock.patch("prettyconf.loaders.boto3")71def test_fail_missing_config(mock_boto):72    mock_boto.client.return_value.get_parameters_by_path.return_value = PARAMETER_RESPONSE73    config = AwsParameterStore()74    assert "DATABASE_URL" not in config75    with pytest.raises(KeyError):76        config["DATABASE_URL"]77@mock.patch("prettyconf.loaders.boto3")78def test_parameter_store_access_fail(mock_boto):79    mock_boto.client.return_value.get_parameters_by_path.side_effect = BotoCoreError80    config = AwsParameterStore()81    assert "DATABASE_URL" not in config82    with pytest.raises(KeyError):...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!!
