def test_nested_container(self): injector = Injector(a='A', b='A') sub = Injector(injector, b='B') subsub = sub.sub(c='C') self.assertEqual(subsub.call(lambda a, b, c: a + b + c), 'ABC') self.assertEqual(subsub.create(lambda a, b, c: a + b + c), 'ABC')
def wrapper(*args, **kwargs): with loop_context() as loop: injector = Injector(loop=loop, fake_future=lambda: fake_future) asyncio.get_child_watcher().attach_loop(loop) asyncio.set_event_loop(loop) loop.run_until_complete(injector.call(f, *args, **kwargs))
def test_close_made_in_sequence(self): injector = Injector(a=lambda b: self.closer('A'), b=lambda: self.closer('B')) injector.a injector.close() self.assertEqual(['A', 'B'], self.call_list)
def test_close_only_called_once(self): injector = Injector(a=lambda: self.closer('A')) sub = injector.sub(b=lambda: self.closer('B')) sub.b injector.a sub.close() injector.close() self.assertEqual(['B', 'A'], self.call_list)
def test_close_limited_to_scope(self): injector = Injector(a=lambda: self.closer('A')) sub = injector.sub(b=lambda: self.closer('B')) sub.b injector.a sub.close() self.assertEqual(['B'], self.call_list)
def test_close_shared_among_subinjectors(self): injector = Injector(a=lambda: self.closer('A')) sub = injector.sub(b=lambda: self.closer('B')) sub.b injector.a injector.close() self.assertEqual(['B', 'A'], self.call_list)
def test_clear_subinjectors_on_close(self): injector = Injector(a=lambda: self.closer('A')) sub = injector.sub(b=lambda: self.closer('B')) sub.b injector.a self.assertEqual(1, injector.child_count) del sub self.assertEqual(0, injector.child_count)
def test_delete_calls_close(self): injector = Injector(a=lambda: self.closer('A')) injector.a del injector self.assertEqual(['A'], self.call_list)
def test_dependency_chain_circular(self): injector = Injector(a="A", b=lambda test: test, test=lambda a, b: a + b) with self.assertRaises(RecursionError): injector.test
def test_close_propagates_to_initial_child(self): injector = Injector(a=self.closer('A'), b=lambda: self.closer('B')) injector.close() self.assertEqual(['A'], self.call_list)
def test_async_closing(self): injector = Injector(loop=asyncio.get_event_loop, a=self.async_closer) injector.a injector.close() self.assertEqual(['Default'], self.call_list)
def test_close_propagates_to_created_child_as_well(self): injector = Injector(a=self.closer('A'), b=lambda: self.closer('B')) injector.b injector.close() self.assertEqual(['B', 'A'], self.call_list)
def test_dependency_chain_longer(self): injector = Injector(a="A", b=lambda a: a, test=lambda a, b: a + b) self.assertEqual(injector.test, "AA")
def test_close_does_nothing_by_default(self): injector = Injector() injector.close()
def test_injection_direct_call(self): injector = Injector(a='A', b='A') value = injector.call(lambda a, b, c: a + b + c, b='B', c='C') self.assertEqual(value, "ABC")
def test_do_not_call_on_class_level(self): injector = Injector(a=self.closer) injector.close() self.assertEqual([], self.call_list)
def test_injection(self): injector = Injector(test="A") call = injector.wrap(lambda test: test + test + test) self.assertEqual(call(), "AAA")
def test_call_on_instance(self): injector = Injector(a=self.closer) injector.a injector.close() self.assertEqual(['Default'], self.call_list)
def test_injection_kwargs_only(self): injector = Injector(test="A") call = injector.wrap(lambda *, test: test + test + test) self.assertEqual(call(), "AAA")
def test_inject_function_leaves_wrapped_function(self): injector = Injector(a='A', c='C', f=lambda c: lambda a, b: a + b + c) self.assertEqual(injector.f(b='B'), 'ABC') self.assertEqual(injector.f(a='B', b='B'), 'BBC')
def test_direct_access_no_data(self): injector = Injector() with self.assertRaises(AttributeError): injector.test
def test_jit_instance_always_the_same_result(self): injector = Injector(test=self.__class__) self.assertIs(injector.test, injector.test)
def test_direct_access(self): injector = Injector(test="A") self.assertEqual("A", injector.test)
# This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import asyncio import aiohttp from easyinject import Injector from .common import Storage, RepositoryChecker, Subversion, VulnerabilityManager, RepositoryHasher from .common.parallel import BackgroundRunner from .common.cve import CVEReader app = Injector(storage=Storage, loop=asyncio.get_event_loop, background_runner=BackgroundRunner, repository_checker=RepositoryChecker, cve_reader=CVEReader, subversion=Subversion, vulnerability_manager=VulnerabilityManager, aiohttp_session=aiohttp.ClientSession, repository_hasher=RepositoryHasher)
def test_jit_instance(self): item = Exception() injector = Injector(test=lambda: item) self.assertIs(item, injector.test)
def test_dependency_chain(self): injector = Injector(a="A", b="B", test=lambda a, b: a + b) self.assertEqual(injector.test, "AB")
def test_create_instances(self): injector = Injector(test=Injector) self.assertIsInstance(injector.test, Injector)
def test_no_injection(self): injector = Injector(test="A") call = injector.wrap(lambda test: test + test + test) self.assertEqual(call(test="B"), "BBB")
# # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import asyncio import logging from easyinject import Injector from .engine import AioHttpEngine from .kb import KnowledgeBase def custom_event_loop(): try: import uvloop loop = uvloop.new_event_loop() asyncio.set_event_loop(loop) logger = logging.getLogger(__name__) logger.debug('Using uvloop') except ImportError: pass return asyncio.get_event_loop() defaults = Injector(loop=custom_event_loop, request_engine=AioHttpEngine, kb=KnowledgeBase)