python-dependency-injector/tests/unit/schema/test_integration_py36.py

274 lines
9.8 KiB
Python
Raw Permalink Normal View History

Pytest migration (#519) * Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
2021-10-18 23:19:03 +03:00
"""Container tests for building containers from configuration files."""
import os
Schemas (#429) * Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
2021-03-20 20:16:51 +03:00
import sqlite3
from dependency_injector import containers
Pytest migration (#519) * Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
2021-10-18 23:19:03 +03:00
from pytest import mark
Schemas (#429) * Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
2021-03-20 20:16:51 +03:00
Pytest migration (#519) * Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
2021-10-18 23:19:03 +03:00
from samples.schema.services import UserService, AuthService, PhotoService
SAMPLES_DIR = os.path.abspath(
Schemas (#429) * Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
2021-03-20 20:16:51 +03:00
os.path.sep.join((
os.path.dirname(__file__),
2021-10-01 03:09:42 +03:00
"../samples/",
Schemas (#429) * Add single container prototype * Add multiple containers prototype * Add integration tests * Implement from_*() methods and add tests * Prototype inline injections * Add integration test for inline providers * Refactor integration tests * Add integration test for reordered schema * Remove unused imports from tests * Refactor schema module * Update tests to match latest schemas * Add mypy_boto3_s3 to the test requirements * Add boto3 to the test requirements * Add set_provides for Callable, Factory, and Singleton providers * Fix warnings in tests * Add typing stubs for Callable, Factory, and Singleton .set_provides() attributes * Fix singleton children to have optional provides * Implement provider to provider resolving * Fix pypy3 tests * Implement boto3 session use case and add tests * Implement lazy initialization and improve copying for Callable, Factory, Singleton, and Coroutine providers * Fix Python 2 tests * Add region name for boto3 integration example * Remove f-strings from set_provides() * Fix schema flake8 errors * Implement lazy initialization and improve copying for Delegate provider * Implement lazy initialization and improve copying for Object provider * Speed up wiring tests * Implement lazy initialization and improve copying for FactoryAggregate provider * Implement lazy initialization and improve copying for Selector provider * Implement lazy initialization and improve copying for Dependency provider * Implement lazy initialization and improve copying for Resource provider * Implement lazy initialization and improve copying for Configuration provider * Implement lazy initialization and improve copying for ProvidedInstance provider * Implement lazy initialization and improve copying for AttributeGetter provider * Implement lazy initialization and improve copying for ItemGetter provider * Implement lazy initialization and improve copying for MethodCaller provder * Update changelog * Fix typing in wiring module * Fix wiring module loader uninstallation issue * Fix provided instance providers error handing in asynchronous mode Co-authored-by: Roman Mogylatov <rmk@Romans-MacBook-Pro.local>
2021-03-20 20:16:51 +03:00
)),
)
Pytest migration (#519) * Add pytest and pytest-asyncio to the requirements * Update aiohttp ext test * Update setup.cfg * Update tox.ini * Add pytest to the tox requirements * Update tox.ini * Move configuration to tox.ini * Add pytest configs * Rename pytest-py34-py35.ini -> pytest-py35.ini * Update config file paths * Update makefile * Migrate common tests to pytest * Migrate FastAPI and Flask wiring tests * Rename flask and fastapi wiring test files * Move wiring autoloader tests * Add pytest-asyncio to the tox.ini * Migrate wiring async injection tests * Migrate main wiring tests * Migrate wiring string module and package names tests * Migrate wiring config tests * Migrate misc wiring tests * Update tests structure * Migrate misc wiring tests * Refactor container.from_schema() API tests * Migrate container.from_schema() integration tests * Rename schema samples * Update sample imports * Migrate container tests * Refactor container tests * Migrate container self tests * Migrate container instance tests * Migrate container custom string attribute name tests * Migrate container async resource tests * Fix py2 container tests * Migrate container cls tests * Migrate container class custom string cls as atrribute name tests * Migrate ext.aiohttp tests * Migrate ext.flasks tests * Update ext package tests doc block * Migrate provider utils tests * Migrate Factory async mode tests * Migrate async tests * Rename common test module * Refactor asserts in provider tests * Migrate factory tests * Migrate selector provider tests * Migrate object provider tests * Migrate self provider tests * Migrate delegate provider tests * Migrate provider tests * Migrate dependency provider tests * Migrate dependencies container provider tests * Fix warnings * Migrate list provider tests * Migrate dict provider tests * Migrate callable tests * Migrate injection tests * Migrate container provider tests * Migrate coroutine providers * Migrate traversal tests * Migrate resource tests * Migrate configuration tests * Migrate provided instance provider tests * Update doc blocks and imports * Migrate singleton tests * Update changelog and cosmetic fixes
2021-10-18 23:19:03 +03:00
def test_single_container_schema(container: containers.DynamicContainer):
container.from_yaml_schema(f"{SAMPLES_DIR}/schema/container-single.yml")
container.config.from_dict({
"database": {
"dsn": ":memory:",
},
"aws": {
"access_key_id": "KEY",
"secret_access_key": "SECRET",
},
"auth": {
"token_ttl": 3600,
},
})
# User service
user_service1 = container.user_service()
user_service2 = container.user_service()
assert isinstance(user_service1, UserService)
assert isinstance(user_service2, UserService)
assert user_service1 is not user_service2
assert isinstance(user_service1.db, sqlite3.Connection)
assert isinstance(user_service2.db, sqlite3.Connection)
assert user_service1.db is user_service2.db
# Auth service
auth_service1 = container.auth_service()
auth_service2 = container.auth_service()
assert isinstance(auth_service1, AuthService)
assert isinstance(auth_service2, AuthService)
assert auth_service1 is not auth_service2
assert isinstance(auth_service1.db, sqlite3.Connection)
assert isinstance(auth_service2.db, sqlite3.Connection)
assert auth_service1.db is auth_service2.db
assert auth_service1.db is container.database_client()
assert auth_service2.db is container.database_client()
assert auth_service1.token_ttl == 3600
assert auth_service2.token_ttl == 3600
# Photo service
photo_service1 = container.photo_service()
photo_service2 = container.photo_service()
assert isinstance(photo_service1, PhotoService)
assert isinstance(photo_service2, PhotoService)
assert photo_service1 is not photo_service2
assert isinstance(photo_service1.db, sqlite3.Connection)
assert isinstance(photo_service2.db, sqlite3.Connection)
assert photo_service1.db is photo_service2.db
assert photo_service1.db is container.database_client()
assert photo_service2.db is container.database_client()
assert photo_service1.s3 is photo_service2.s3
assert photo_service1.s3 is container.s3_client()
assert photo_service2.s3 is container.s3_client()
def test_multiple_containers_schema(container: containers.DynamicContainer):
container.from_yaml_schema(f"{SAMPLES_DIR}/schema/container-multiple.yml")
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()
assert isinstance(user_service1, UserService)
assert isinstance(user_service2, UserService)
assert user_service1 is not user_service2
assert isinstance(user_service1.db, sqlite3.Connection)
assert isinstance(user_service2.db, sqlite3.Connection)
assert user_service1.db is user_service2.db
# Auth service
auth_service1 = container.services.auth()
auth_service2 = container.services.auth()
assert isinstance(auth_service1, AuthService)
assert isinstance(auth_service2, AuthService)
assert auth_service1 is not auth_service2
assert isinstance(auth_service1.db, sqlite3.Connection)
assert isinstance(auth_service2.db, sqlite3.Connection)
assert auth_service1.db is auth_service2.db
assert auth_service1.db is container.gateways.database_client()
assert auth_service2.db is container.gateways.database_client()
assert auth_service1.token_ttl == 3600
assert auth_service2.token_ttl == 3600
# Photo service
photo_service1 = container.services.photo()
photo_service2 = container.services.photo()
assert isinstance(photo_service1, PhotoService)
assert isinstance(photo_service2, PhotoService)
assert photo_service1 is not photo_service2
assert isinstance(photo_service1.db, sqlite3.Connection)
assert isinstance(photo_service2.db, sqlite3.Connection)
assert photo_service1.db is photo_service2.db
assert photo_service1.db is container.gateways.database_client()
assert photo_service2.db is container.gateways.database_client()
assert photo_service1.s3 is photo_service2.s3
assert photo_service1.s3 is container.gateways.s3_client()
assert photo_service2.s3 is container.gateways.s3_client()
def test_multiple_reordered_containers_schema(container: containers.DynamicContainer):
container.from_yaml_schema(f"{SAMPLES_DIR}/schema/container-multiple-reordered.yml")
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()
assert isinstance(user_service1, UserService)
assert isinstance(user_service2, UserService)
assert user_service1 is not user_service2
assert isinstance(user_service1.db, sqlite3.Connection)
assert isinstance(user_service2.db, sqlite3.Connection)
assert user_service1.db is user_service2.db
# Auth service
auth_service1 = container.services.auth()
auth_service2 = container.services.auth()
assert isinstance(auth_service1, AuthService)
assert isinstance(auth_service2, AuthService)
assert auth_service1 is not auth_service2
assert isinstance(auth_service1.db, sqlite3.Connection)
assert isinstance(auth_service2.db, sqlite3.Connection)
assert auth_service1.db is auth_service2.db
assert auth_service1.db is container.gateways.database_client()
assert auth_service2.db is container.gateways.database_client()
assert auth_service1.token_ttl == 3600
assert auth_service2.token_ttl == 3600
# Photo service
photo_service1 = container.services.photo()
photo_service2 = container.services.photo()
assert isinstance(photo_service1, PhotoService)
assert isinstance(photo_service2, PhotoService)
assert photo_service1 is not photo_service2
assert isinstance(photo_service1.db, sqlite3.Connection)
assert isinstance(photo_service2.db, sqlite3.Connection)
assert photo_service1.db is photo_service2.db
assert photo_service1.db is container.gateways.database_client()
assert photo_service2.db is container.gateways.database_client()
assert photo_service1.s3 is photo_service2.s3
assert photo_service1.s3 is container.gateways.s3_client()
assert photo_service2.s3 is container.gateways.s3_client()
def test_multiple_containers_with_inline_providers_schema(container: containers.DynamicContainer):
container.from_yaml_schema(f"{SAMPLES_DIR}/schema/container-multiple-inline.yml")
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()
assert isinstance(user_service1, UserService)
assert isinstance(user_service2, UserService)
assert user_service1 is not user_service2
assert isinstance(user_service1.db, sqlite3.Connection)
assert isinstance(user_service2.db, sqlite3.Connection)
assert user_service1.db is user_service2.db
# Auth service
auth_service1 = container.services.auth()
auth_service2 = container.services.auth()
assert isinstance(auth_service1, AuthService)
assert isinstance(auth_service2, AuthService)
assert auth_service1 is not auth_service2
assert isinstance(auth_service1.db, sqlite3.Connection)
assert isinstance(auth_service2.db, sqlite3.Connection)
assert auth_service1.db is auth_service2.db
assert auth_service1.db is container.gateways.database_client()
assert auth_service2.db is container.gateways.database_client()
assert auth_service1.token_ttl == 3600
assert auth_service2.token_ttl == 3600
# Photo service
photo_service1 = container.services.photo()
photo_service2 = container.services.photo()
assert isinstance(photo_service1, PhotoService)
assert isinstance(photo_service2, PhotoService)
assert photo_service1 is not photo_service2
assert isinstance(photo_service1.db, sqlite3.Connection)
assert isinstance(photo_service2.db, sqlite3.Connection)
assert photo_service1.db is photo_service2.db
assert photo_service1.db is container.gateways.database_client()
assert photo_service2.db is container.gateways.database_client()
assert photo_service1.s3 is photo_service2.s3
assert photo_service1.s3 is container.gateways.s3_client()
assert photo_service2.s3 is container.gateways.s3_client()
@mark.skip(reason="Boto3 tries to connect to the internet")
def test_schema_with_boto3_session(container: containers.DynamicContainer):
container.from_yaml_schema(f"{SAMPLES_DIR}/schema/container-boto3-session.yml")
container.config.from_dict(
{
"aws_access_key_id": "key",
"aws_secret_access_key": "secret",
"aws_session_token": "token",
"aws_region_name": "us-east-1",
},
)
assert container.s3_client().__class__.__name__ == "S3"
assert container.sqs_client().__class__.__name__ == "SQS"