2015-01-11 19:10:11 +03:00
|
|
|
"""
|
|
|
|
Concept example of objects overrides.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2015-01-18 03:28:41 +03:00
|
|
|
from objects import AbstractCatalog, overrides
|
|
|
|
from objects.providers import Singleton, NewInstance
|
|
|
|
from objects.injections import InitArg, Attribute
|
2015-01-11 19:10:11 +03:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
|
|
# Some example class.
|
|
|
|
class ObjectA(object):
|
|
|
|
def __init__(self, db):
|
|
|
|
self.db = db
|
|
|
|
|
|
|
|
|
|
|
|
class ObjectAMock(ObjectA):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Catalog of objects providers.
|
2015-01-18 03:28:41 +03:00
|
|
|
class Catalog(AbstractCatalog):
|
2015-01-11 19:10:11 +03:00
|
|
|
"""
|
|
|
|
Objects catalog.
|
|
|
|
"""
|
|
|
|
|
|
|
|
database = Singleton(sqlite3.Connection,
|
|
|
|
InitArg('database', ':memory:'),
|
|
|
|
Attribute('row_factory', sqlite3.Row))
|
|
|
|
""" :type: (objects.Provider) -> sqlite3.Connection """
|
|
|
|
|
|
|
|
object_a = NewInstance(ObjectA,
|
|
|
|
InitArg('db', database))
|
|
|
|
""" :type: (objects.Provider) -> ObjectA """
|
|
|
|
|
|
|
|
|
2015-01-18 03:28:41 +03:00
|
|
|
# Overriding Catalog by SandboxCatalog with some mocks.
|
|
|
|
@overrides(Catalog)
|
|
|
|
class SandboxCatalog(Catalog):
|
2015-01-11 19:10:11 +03:00
|
|
|
"""
|
|
|
|
Sandbox objects catalog with some mocks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
object_a = NewInstance(ObjectAMock,
|
2015-01-18 03:28:41 +03:00
|
|
|
InitArg('db', Catalog.database))
|
2015-01-11 19:10:11 +03:00
|
|
|
""" :type: (objects.Provider) -> ObjectA """
|
|
|
|
|
|
|
|
|
|
|
|
# Catalog static provides.
|
2015-01-18 03:28:41 +03:00
|
|
|
a1 = Catalog.object_a()
|
|
|
|
a2 = Catalog.object_a()
|
2015-01-11 19:10:11 +03:00
|
|
|
|
|
|
|
# Some asserts.
|
|
|
|
assert isinstance(a1, ObjectAMock)
|
|
|
|
assert isinstance(a2, ObjectAMock)
|
|
|
|
assert a1 is not a2
|
2015-01-18 03:28:41 +03:00
|
|
|
assert a1.db is a2.db is Catalog.database()
|