mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-31 07:57:43 +03:00 
			
		
		
		
	Add integration test for inline providers
This commit is contained in:
		
							parent
							
								
									b882c7cdf3
								
							
						
					
					
						commit
						6f41372b68
					
				|  | @ -0,0 +1,61 @@ | ||||||
|  | version: "1" | ||||||
|  | 
 | ||||||
|  | providers: | ||||||
|  | 
 | ||||||
|  |   core: | ||||||
|  |     provider: Container | ||||||
|  |     providers: | ||||||
|  |       config: | ||||||
|  |         provider: Configuration | ||||||
|  | 
 | ||||||
|  |   gateways: | ||||||
|  |     provider: Container | ||||||
|  |     providers: | ||||||
|  |       database_client: | ||||||
|  |         provider: Singleton | ||||||
|  |         provides: sqlite3.connect | ||||||
|  |         args: | ||||||
|  |           - core.config.database.dsn | ||||||
|  | 
 | ||||||
|  |       s3_client: | ||||||
|  |         provider: Singleton | ||||||
|  |         provides: boto3.client | ||||||
|  |         kwargs: | ||||||
|  |           service_name: s3 | ||||||
|  |           aws_access_key_id: core.config.aws.access_key_id | ||||||
|  |           aws_secret_access_key: core.config.aws.secret_access_key | ||||||
|  | 
 | ||||||
|  |   services: | ||||||
|  |     provider: Container | ||||||
|  |     providers: | ||||||
|  |       user: | ||||||
|  |         provider: Factory | ||||||
|  |         provides: schemasample.services.UserService | ||||||
|  |         kwargs: | ||||||
|  |           db: | ||||||
|  |             provider: Callable | ||||||
|  |             provides: schemasample.utils.return_ | ||||||
|  |             args: | ||||||
|  |               - gateways.database_client | ||||||
|  | 
 | ||||||
|  |       auth: | ||||||
|  |         provider: Factory | ||||||
|  |         provides: schemasample.services.AuthService | ||||||
|  |         kwargs: | ||||||
|  |           db: | ||||||
|  |             provider: Callable | ||||||
|  |             provides: schemasample.utils.return_ | ||||||
|  |             args: | ||||||
|  |               - gateways.database_client | ||||||
|  |           token_ttl: core.config.auth.token_ttl.as_int() | ||||||
|  | 
 | ||||||
|  |       photo: | ||||||
|  |         provider: Factory | ||||||
|  |         provides: schemasample.services.PhotoService | ||||||
|  |         kwargs: | ||||||
|  |           db: | ||||||
|  |             provider: Callable | ||||||
|  |             provides: schemasample.utils.return_ | ||||||
|  |             args: | ||||||
|  |               - gateways.database_client | ||||||
|  |           s3: gateways.s3_client | ||||||
							
								
								
									
										2
									
								
								tests/unit/samples/schemasample/utils.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								tests/unit/samples/schemasample/utils.py
									
									
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,2 @@ | ||||||
|  | def return_(instance): | ||||||
|  |     return instance | ||||||
|  | @ -162,3 +162,140 @@ class TestSchemaMultipleContainers(unittest.TestCase): | ||||||
|         self.assertIs(photo_service1.s3, photo_service2.s3) |         self.assertIs(photo_service1.s3, photo_service2.s3) | ||||||
|         self.assertIs(photo_service1.s3, container.gateways.s3_client()) |         self.assertIs(photo_service1.s3, container.gateways.s3_client()) | ||||||
|         self.assertIs(photo_service2.s3, container.gateways.s3_client()) |         self.assertIs(photo_service2.s3, container.gateways.s3_client()) | ||||||
|  | 
 | ||||||
|  | class TestSchemaMultipleContainers(unittest.TestCase): | ||||||
|  | 
 | ||||||
|  |     def test(self): | ||||||
|  |         container = containers.DynamicContainer() | ||||||
|  | 
 | ||||||
|  |         schema_path = pathlib.Path(__file__).parent.parent / 'samples' / 'schemasample' / 'container-multiple.yml' | ||||||
|  |         # TODO: from_yaml_schema() | ||||||
|  |         with open(schema_path) as file: | ||||||
|  |             schema = yaml.load(file, yaml.Loader) | ||||||
|  |         container.from_schema(schema) | ||||||
|  | 
 | ||||||
|  |         container.core.config.from_dict({ | ||||||
|  |             'database': { | ||||||
|  |                 'dsn': ':memory:', | ||||||
|  |             }, | ||||||
|  |             'aws': { | ||||||
|  |                 'access_key_id': 'KEY', | ||||||
|  |                 'secret_access_key': 'SECRET', | ||||||
|  |             }, | ||||||
|  |             'auth': { | ||||||
|  |                 'token_ttl': 3600, | ||||||
|  |             }, | ||||||
|  |         }) | ||||||
|  | 
 | ||||||
|  |         # User service | ||||||
|  |         user_service1 = container.services.user() | ||||||
|  |         user_service2 = container.services.user() | ||||||
|  |         self.assertIsInstance(user_service1, UserService) | ||||||
|  |         self.assertIsInstance(user_service2, UserService) | ||||||
|  |         self.assertIsNot(user_service1, user_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(user_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(user_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(user_service1.db, user_service2.db) | ||||||
|  | 
 | ||||||
|  |         # Auth service | ||||||
|  |         auth_service1 = container.services.auth() | ||||||
|  |         auth_service2 = container.services.auth() | ||||||
|  |         self.assertIsInstance(auth_service1, AuthService) | ||||||
|  |         self.assertIsInstance(auth_service2, AuthService) | ||||||
|  |         self.assertIsNot(auth_service1, auth_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(auth_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(auth_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(auth_service1.db, auth_service2.db) | ||||||
|  |         self.assertIs(auth_service1.db, container.gateways.database_client()) | ||||||
|  |         self.assertIs(auth_service2.db, container.gateways.database_client()) | ||||||
|  | 
 | ||||||
|  |         self.assertEqual(auth_service1.token_ttl, 3600) | ||||||
|  |         self.assertEqual(auth_service2.token_ttl, 3600) | ||||||
|  | 
 | ||||||
|  |         # Photo service | ||||||
|  |         photo_service1 = container.services.photo() | ||||||
|  |         photo_service2 = container.services.photo() | ||||||
|  |         self.assertIsInstance(photo_service1, PhotoService) | ||||||
|  |         self.assertIsInstance(photo_service2, PhotoService) | ||||||
|  |         self.assertIsNot(photo_service1, photo_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(photo_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(photo_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(photo_service1.db, photo_service2.db) | ||||||
|  |         self.assertIs(photo_service1.db, container.gateways.database_client()) | ||||||
|  |         self.assertIs(photo_service2.db, container.gateways.database_client()) | ||||||
|  | 
 | ||||||
|  |         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 TestSchemaMultipleContainersWithInlineProviders(unittest.TestCase): | ||||||
|  | 
 | ||||||
|  |     def test(self): | ||||||
|  |         container = containers.DynamicContainer() | ||||||
|  | 
 | ||||||
|  |         schema_path = pathlib.Path(__file__).parent.parent / 'samples' / 'schemasample' / 'container-multiple-inline.yml' | ||||||
|  |         # TODO: from_yaml_schema() | ||||||
|  |         with open(schema_path) as file: | ||||||
|  |             schema = yaml.load(file, yaml.Loader) | ||||||
|  |         container.from_schema(schema) | ||||||
|  | 
 | ||||||
|  |         container.core.config.from_dict({ | ||||||
|  |             'database': { | ||||||
|  |                 'dsn': ':memory:', | ||||||
|  |             }, | ||||||
|  |             'aws': { | ||||||
|  |                 'access_key_id': 'KEY', | ||||||
|  |                 'secret_access_key': 'SECRET', | ||||||
|  |             }, | ||||||
|  |             'auth': { | ||||||
|  |                 'token_ttl': 3600, | ||||||
|  |             }, | ||||||
|  |         }) | ||||||
|  | 
 | ||||||
|  |         # User service | ||||||
|  |         user_service1 = container.services.user() | ||||||
|  |         user_service2 = container.services.user() | ||||||
|  |         self.assertIsInstance(user_service1, UserService) | ||||||
|  |         self.assertIsInstance(user_service2, UserService) | ||||||
|  |         self.assertIsNot(user_service1, user_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(user_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(user_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(user_service1.db, user_service2.db) | ||||||
|  | 
 | ||||||
|  |         # Auth service | ||||||
|  |         auth_service1 = container.services.auth() | ||||||
|  |         auth_service2 = container.services.auth() | ||||||
|  |         self.assertIsInstance(auth_service1, AuthService) | ||||||
|  |         self.assertIsInstance(auth_service2, AuthService) | ||||||
|  |         self.assertIsNot(auth_service1, auth_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(auth_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(auth_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(auth_service1.db, auth_service2.db) | ||||||
|  |         self.assertIs(auth_service1.db, container.gateways.database_client()) | ||||||
|  |         self.assertIs(auth_service2.db, container.gateways.database_client()) | ||||||
|  | 
 | ||||||
|  |         self.assertEqual(auth_service1.token_ttl, 3600) | ||||||
|  |         self.assertEqual(auth_service2.token_ttl, 3600) | ||||||
|  | 
 | ||||||
|  |         # Photo service | ||||||
|  |         photo_service1 = container.services.photo() | ||||||
|  |         photo_service2 = container.services.photo() | ||||||
|  |         self.assertIsInstance(photo_service1, PhotoService) | ||||||
|  |         self.assertIsInstance(photo_service2, PhotoService) | ||||||
|  |         self.assertIsNot(photo_service1, photo_service2) | ||||||
|  | 
 | ||||||
|  |         self.assertIsInstance(photo_service1.db, sqlite3.Connection) | ||||||
|  |         self.assertIsInstance(photo_service2.db, sqlite3.Connection) | ||||||
|  |         self.assertIs(photo_service1.db, photo_service2.db) | ||||||
|  |         self.assertIs(photo_service1.db, container.gateways.database_client()) | ||||||
|  |         self.assertIs(photo_service2.db, container.gateways.database_client()) | ||||||
|  | 
 | ||||||
|  |         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()) | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user