2015-11-23 14:48:21 +00:00
|
|
|
"""`ExternalDependency` providers example."""
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
import sqlite3
|
2015-09-02 15:36:43 +00:00
|
|
|
import contextlib
|
2016-06-09 14:37:05 +00:00
|
|
|
|
2016-06-06 08:54:05 +00:00
|
|
|
import dependency_injector.providers as providers
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
|
2016-06-09 14:37:05 +00:00
|
|
|
class UsersService(object):
|
|
|
|
"""Example class UsersService.
|
2015-06-18 13:34:26 +00:00
|
|
|
|
2016-06-09 14:37:05 +00:00
|
|
|
UsersService has dependency on DBAPI 2.0 database connection.
|
2015-07-17 07:01:27 +00:00
|
|
|
"""
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
def __init__(self, database):
|
|
|
|
"""Initializer.
|
|
|
|
|
2015-11-23 14:48:21 +00:00
|
|
|
:param database: Database connection.
|
|
|
|
:type database: sqlite3.dbapi2.Connection
|
2015-07-17 07:01:27 +00:00
|
|
|
"""
|
2015-06-18 13:34:26 +00:00
|
|
|
self.database = database
|
2015-09-02 15:55:36 +00:00
|
|
|
self.database.row_factory = sqlite3.dbapi2.Row
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
def init_database(self):
|
|
|
|
"""Initialize database, if it has not been initialized yet."""
|
2015-09-02 15:36:43 +00:00
|
|
|
with contextlib.closing(self.database.cursor()) as cursor:
|
2015-06-18 13:34:26 +00:00
|
|
|
cursor.execute("""
|
|
|
|
CREATE TABLE IF NOT EXISTS users(
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
name VARCHAR(32)
|
|
|
|
)
|
|
|
|
""")
|
|
|
|
|
|
|
|
def create(self, name):
|
|
|
|
"""Create user with provided name and return his id."""
|
2015-09-02 15:36:43 +00:00
|
|
|
with contextlib.closing(self.database.cursor()) as cursor:
|
2015-06-18 13:34:26 +00:00
|
|
|
cursor.execute('INSERT INTO users(name) VALUES (?)', (name,))
|
|
|
|
return cursor.lastrowid
|
|
|
|
|
|
|
|
def get_by_id(self, id):
|
|
|
|
"""Return user info by user id."""
|
2015-09-02 15:36:43 +00:00
|
|
|
with contextlib.closing(self.database.cursor()) as cursor:
|
2015-06-18 13:34:26 +00:00
|
|
|
cursor.execute('SELECT id, name FROM users WHERE id=?', (id,))
|
|
|
|
return cursor.fetchone()
|
|
|
|
|
|
|
|
|
2016-06-09 14:37:05 +00:00
|
|
|
# Database and UsersService providers:
|
2015-11-23 14:48:21 +00:00
|
|
|
database = providers.ExternalDependency(instance_of=sqlite3.dbapi2.Connection)
|
2016-06-09 14:37:05 +00:00
|
|
|
users_service_factory = providers.Factory(UsersService,
|
2015-11-23 14:48:21 +00:00
|
|
|
database=database)
|
2015-06-18 13:34:26 +00:00
|
|
|
|
|
|
|
# Out of library's scope.
|
|
|
|
#
|
|
|
|
# Setting database provider:
|
2015-11-23 14:48:21 +00:00
|
|
|
database.provided_by(providers.Singleton(sqlite3.dbapi2.Connection,
|
|
|
|
database=':memory:',
|
|
|
|
timeout=30,
|
|
|
|
detect_types=True,
|
|
|
|
isolation_level='EXCLUSIVE'))
|
2015-06-18 13:34:26 +00:00
|
|
|
|
2016-06-09 14:37:05 +00:00
|
|
|
# Creating UsersService instance:
|
2015-06-18 13:34:26 +00:00
|
|
|
users_service = users_service_factory()
|
|
|
|
|
2016-06-09 14:37:05 +00:00
|
|
|
# Initializing UsersService database:
|
2015-06-18 13:34:26 +00:00
|
|
|
users_service.init_database()
|
|
|
|
|
|
|
|
# Creating test user and retrieving full information about him:
|
|
|
|
test_user_id = users_service.create(name='test_user')
|
|
|
|
test_user = users_service.get_by_id(test_user_id)
|
|
|
|
|
|
|
|
# Making some asserts:
|
|
|
|
assert test_user['id'] == 1
|
|
|
|
assert test_user['name'] == 'test_user'
|