Пример #1
0
    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')
Пример #2
0
 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))
Пример #3
0
    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')
Пример #4
0
 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))
Пример #5
0
    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)
Пример #6
0
 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)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
    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)
Пример #10
0
 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)
Пример #11
0
    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)
Пример #12
0
    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)
Пример #13
0
    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)
Пример #14
0
    def test_delete_calls_close(self):
        injector = Injector(a=lambda: self.closer('A'))
        injector.a

        del injector

        self.assertEqual(['A'], self.call_list)
Пример #15
0
    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
Пример #16
0
    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)
Пример #17
0
    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)
Пример #18
0
    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)
Пример #19
0
    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)
Пример #20
0
    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")
Пример #21
0
 def test_close_does_nothing_by_default(self):
     injector = Injector()
     injector.close()
Пример #22
0
    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)
Пример #23
0
 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")
Пример #24
0
    def test_do_not_call_on_class_level(self):
        injector = Injector(a=self.closer)
        injector.close()

        self.assertEqual([], self.call_list)
Пример #25
0
    def test_injection(self):
        injector = Injector(test="A")
        call = injector.wrap(lambda test: test + test + test)

        self.assertEqual(call(), "AAA")
Пример #26
0
    def test_call_on_instance(self):
        injector = Injector(a=self.closer)
        injector.a
        injector.close()

        self.assertEqual(['Default'], self.call_list)
Пример #27
0
    def test_do_not_call_on_class_level(self):
        injector = Injector(a=self.closer)
        injector.close()

        self.assertEqual([], self.call_list)
Пример #28
0
    def test_injection_kwargs_only(self):
        injector = Injector(test="A")
        call = injector.wrap(lambda *, test: test + test + test)

        self.assertEqual(call(), "AAA")
Пример #29
0
 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")
Пример #30
0
    def test_call_on_instance(self):
        injector = Injector(a=self.closer)
        injector.a
        injector.close()

        self.assertEqual(['Default'], self.call_list)
Пример #31
0
 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')
Пример #32
0
    def test_injection_kwargs_only(self):
        injector = Injector(test="A")
        call = injector.wrap(lambda *, test: test + test + test)

        self.assertEqual(call(), "AAA")
Пример #33
0
    def test_direct_access_no_data(self):
        injector = Injector()

        with self.assertRaises(AttributeError):
            injector.test
Пример #34
0
 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')
Пример #35
0
    def test_jit_instance_always_the_same_result(self):
        injector = Injector(test=self.__class__)

        self.assertIs(injector.test, injector.test)
Пример #36
0
    def test_direct_access(self):
        injector = Injector(test="A")

        self.assertEqual("A", injector.test)
Пример #37
0
# 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)
Пример #38
0
    def test_jit_instance(self):
        item = Exception()

        injector = Injector(test=lambda: item)

        self.assertIs(item, injector.test)
Пример #39
0
 def test_close_does_nothing_by_default(self):
     injector = Injector()
     injector.close()
Пример #40
0
    def test_dependency_chain(self):
        injector = Injector(a="A", b="B", test=lambda a, b: a + b)

        self.assertEqual(injector.test, "AB")
Пример #41
0
    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)
Пример #42
0
    def test_create_instances(self):
        injector = Injector(test=Injector)

        self.assertIsInstance(injector.test, Injector)
Пример #43
0
    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)
Пример #44
0
    def test_no_injection(self):
        injector = Injector(test="A")
        call = injector.wrap(lambda test: test + test + test)

        self.assertEqual(call(test="B"), "BBB")
Пример #45
0
#
# 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)