From dbc63935613c78ba1c9d33c962cdf639e47a2ead Mon Sep 17 00:00:00 2001 From: Roman Mogylatov Date: Sat, 6 Mar 2021 10:12:59 -0500 Subject: [PATCH] Implement boto3 session use case and add tests --- src/dependency_injector/schema.py | 27 ++++++++++--------- .../schemasample/container-boto3-session.yml | 26 ++++++++++++++++++ tests/unit/schema/test_integration_py36.py | 10 +++++++ 3 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 tests/unit/samples/schemasample/container-boto3-session.yml diff --git a/src/dependency_injector/schema.py b/src/dependency_injector/schema.py index b1971f08..8608a816 100644 --- a/src/dependency_injector/schema.py +++ b/src/dependency_injector/schema.py @@ -150,18 +150,21 @@ class SchemaProcessorV1: return None for segment in segments[1:]: - if segment == 'as_int()': - provider = provider.as_int() - elif segment == 'as_float()': - provider = provider.as_float() - elif segment.startswith('is_'): # TODO - provider = provider.as_(str) - ... - else: - try: - provider = getattr(provider, segment) - except AttributeError: - return None + parentheses = '' + if '(' in segment and ')' in segment: + parentheses = segment[segment.find('('):segment.rfind(')')+1] + segment = segment.replace(parentheses, '') + + try: + provider = getattr(provider, segment) + except AttributeError: + # TODO + return None + + if parentheses: + # TODO + provider = provider() + return provider diff --git a/tests/unit/samples/schemasample/container-boto3-session.yml b/tests/unit/samples/schemasample/container-boto3-session.yml new file mode 100644 index 00000000..7f530a4e --- /dev/null +++ b/tests/unit/samples/schemasample/container-boto3-session.yml @@ -0,0 +1,26 @@ +version: "1" + +container: + + config: + provider: Configuration + + session: + provider: Singleton + provides: boto3.session.Session + kwargs: + aws_access_key_id: container.config.aws_access_key_id + aws_secret_access_key: container.config.aws_secret_access_key + aws_session_token: container.config.aws_session_token + + s3_client: + provider: Factory + provides: container.session.provided.client.call() + kwargs: + service_name: s3 + + sqs_client: + provider: Factory + provides: container.session.provided.client.call() + kwargs: + service_name: sqs diff --git a/tests/unit/schema/test_integration_py36.py b/tests/unit/schema/test_integration_py36.py index 3360e742..38a48a04 100644 --- a/tests/unit/schema/test_integration_py36.py +++ b/tests/unit/schema/test_integration_py36.py @@ -273,3 +273,13 @@ class TestSchemaMultipleContainersWithInlineProviders(unittest.TestCase): self.assertIs(photo_service1.s3, photo_service2.s3) self.assertIs(photo_service1.s3, container.gateways.s3_client()) self.assertIs(photo_service2.s3, container.gateways.s3_client()) + + +class TestSchemaBoto3Session(unittest.TestCase): + + def test(self): + container = containers.DynamicContainer() + container.from_yaml_schema(f'{_SAMPLES_DIR}/schemasample/container-boto3-session.yml') + + self.assertEqual(container.s3_client().__class__.__name__, 'S3') + self.assertEqual(container.sqs_client().__class__.__name__, 'SQS')