From 6f11715d24cc86cfd90be432bc83ed980d8edf82 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sat, 25 Jul 2015 00:12:24 +0300 Subject: [PATCH] Removing of old providers overriding examples --- examples/override.py | 62 --------------------- examples/readme/overriding_catalog.py | 78 --------------------------- 2 files changed, 140 deletions(-) delete mode 100644 examples/override.py delete mode 100644 examples/readme/overriding_catalog.py diff --git a/examples/override.py b/examples/override.py deleted file mode 100644 index 98f1c91b..00000000 --- a/examples/override.py +++ /dev/null @@ -1,62 +0,0 @@ -"""Override example.""" - -from objects.catalog import AbstractCatalog - -from objects.providers import Singleton -from objects.providers import NewInstance - -from objects.injections import KwArg -from objects.injections import Attribute - -from objects.decorators import override - -import sqlite3 - - -class ObjectA(object): - - """Example class ObjectA, that has dependency on database.""" - - def __init__(self, db): - """Initializer.""" - self.db = db - - -class ObjectAMock(ObjectA): - - """Mock of ObjectA example class.""" - - -class Catalog(AbstractCatalog): - - """Catalog of objects providers.""" - - database = Singleton(sqlite3.Connection, - KwArg('database', ':memory:'), - Attribute('row_factory', sqlite3.Row)) - """:type: (objects.Provider) -> sqlite3.Connection""" - - object_a = NewInstance(ObjectA, - KwArg('db', database)) - """:type: (objects.Provider) -> ObjectA""" - - -@override(Catalog) -class SandboxCatalog(Catalog): - - """Sandbox objects catalog with some mocks that overrides Catalog.""" - - object_a = NewInstance(ObjectAMock, - KwArg('db', Catalog.database)) - """:type: (objects.Provider) -> ObjectA""" - - -# Catalog static provides. -a1 = Catalog.object_a() -a2 = Catalog.object_a() - -# Some asserts. -assert isinstance(a1, ObjectAMock) -assert isinstance(a2, ObjectAMock) -assert a1 is not a2 -assert a1.db is a2.db is Catalog.database() diff --git a/examples/readme/overriding_catalog.py b/examples/readme/overriding_catalog.py deleted file mode 100644 index d05e4ac0..00000000 --- a/examples/readme/overriding_catalog.py +++ /dev/null @@ -1,78 +0,0 @@ -"""Catalog overriding example.""" - -import sqlite3 - -from objects.catalog import AbstractCatalog - -from objects.providers import Singleton -from objects.providers import NewInstance - -from objects.injections import KwArg -from objects.injections import Attribute - -from objects.decorators import override - - -class ObjectA(object): - - """ObjectA has dependency on database.""" - - def __init__(self, database): - """Initializer. - - Database dependency need to be injected via init arg.""" - self.database = database - - def get_one(self): - """Select one from database and return it.""" - return self.database.execute('SELECT 1') - - -class ObjectAMock(ObjectA): - - """Mock of ObjectA. - - Has no dependency on database. - """ - - def __init__(self): - """Initializer.""" - - def get_one(self): - """Select one from database and return it. - - Mock makes no database queries and always returns two instead of one. - """ - return 2 - - -class Catalog(AbstractCatalog): - - """Catalog of objects providers.""" - - database = Singleton(sqlite3.Connection, - KwArg('database', ':memory:'), - KwArg('timeout', 30), - KwArg('detect_types', True), - KwArg('isolation_level', 'EXCLUSIVE'), - Attribute('row_factory', sqlite3.Row)) - - object_a = NewInstance(ObjectA, - KwArg('database', database)) - - -@override(Catalog) -class SandboxCatalog(Catalog): - - """Sandbox objects catalog with some mocks that overrides Catalog.""" - - object_a = NewInstance(ObjectAMock) - - -# Creating several `ObjectA` instances. -object_a_1 = Catalog.object_a() -object_a_2 = Catalog.object_a() - -# Making some asserts. -assert object_a_1 is not object_a_2 -assert object_a_1.get_one() == object_a_2.get_one() == 2