python-dependency-injector/examples/miniapps/bundles/run.py

42 lines
1.2 KiB
Python
Raw Normal View History

2017-12-25 17:47:58 +03:00
"""Run 'Bundles' example application."""
import sqlite3
import boto3
2017-12-20 13:25:46 +03:00
from dependency_injector import containers
from dependency_injector import providers
2017-12-20 13:25:46 +03:00
2017-12-21 23:06:00 +03:00
from bundles.users import Users
from bundles.photos import Photos
2017-12-20 13:25:46 +03:00
class Core(containers.DeclarativeContainer):
"""Core container."""
2017-12-25 17:47:58 +03:00
config = providers.Configuration('config')
sqlite = providers.Singleton(sqlite3.connect, config.database.dsn)
s3 = providers.Singleton(
boto3.client, 's3',
aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=config.aws.secret_access_key)
2017-12-20 13:25:46 +03:00
if __name__ == '__main__':
# Initializing containers
core = Core()
2017-12-25 17:47:58 +03:00
core.config.update({'database': {'dsn': ':memory:'},
'aws': {'access_key_id': 'KEY',
'secret_access_key': 'SECRET'}})
users = Users(database=core.sqlite)
photos = Photos(database=core.sqlite, file_storage=core.s3)
2017-12-20 13:25:46 +03:00
# Fetching few users
user_repository = users.user_repository()
user1 = user_repository.get(id=1)
user2 = user_repository.get(id=2)
# Making some checks
assert user1.id == 1
assert user2.id == 2
2017-12-25 17:47:58 +03:00
assert user_repository.db is core.sqlite()