Ejemplo n.º 1
0
def test_load_extension():
    sys.modules['ipython_context'] = MockIpythonContext
    load_ipython_extension(None)
    assert MockIpythonContext.context is Context.current_context()
    unload_ipython_extension(None)
    assert MockIpythonContext is not Context.current_context()
    sys.modules['ipython_context'] = None
Ejemplo n.º 2
0
def test_gets_most_specific():
    class ConcreteComponentSub(ConcreteComponent):
        pass

    with Context(ConcreteComponent, ConcreteComponentSub):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, ConcreteComponentSub)
Ejemplo n.º 3
0
def test_inject_singleton_without_context():
    Context._set_current_env(None)

    @inject
    class C:
        instance: SomeSingleton

    assert C().instance is C().instance
Ejemplo n.º 4
0
def test_provides_correct_implementation():
    with Context(ConcreteComponent):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, BaseDependency)
        assert isinstance(c, ConcreteComponent)
    with Context(AlternativeComponent):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, BaseDependency)
        assert isinstance(c, AlternativeComponent)
Ejemplo n.º 5
0
def test_inject_provides_correct_implementation():
    with Context(ConcreteDependency):
        d = AbstractDependent()
        assert isinstance(d.abstract_dependency, AbstractDependency)
        assert isinstance(d.abstract_dependency, ConcreteDependency)
    with Context(AlternativeDependency):
        d = AbstractDependent()
        assert isinstance(d.abstract_dependency, AbstractDependency)
        assert isinstance(d.abstract_dependency, AlternativeDependency)
Ejemplo n.º 6
0
def test_match_returns_correct_env(environ):
    env1 = Context()
    env2 = Context()
    environ['TEST_ENV'] = 'ENV1'
    env = match(environment_variable='TEST_ENV', ENV1=env1, ENV2=env2)
    assert env is env1
    environ['TEST_ENV'] = 'ENV2'
    env = match(environment_variable='TEST_ENV', ENV1=env1, ENV2=env2)
    assert env is env2
Ejemplo n.º 7
0
def test_new_environment_in_thread():
    def test():
        with Context(AlternativeComponent):
            c1 = Context.provide(configuration(BaseDependency))
            assert isinstance(c1, AlternativeComponent)

    with Context(ConcreteComponent):
        threading.Thread(target=test).start()
        c2 = Context.provide(configuration(BaseDependency))
        assert isinstance(c2, ConcreteComponent)
Ejemplo n.º 8
0
def test_mocks_are_reset_after_context_exit():
    with Context():
        some_component_mock = mock(SomeComponent)
        d = Dependent()
        assert some_component_mock is d.some_component

    with Context():
        d = Dependent()
        assert some_component_mock is not d.some_component
        assert isinstance(d.some_component, SomeComponent)
Ejemplo n.º 9
0
def test_subtype_is_singleton():
    @singleton
    class SomeComponentSingleton(SomeComponent):
        pass

    with Context(SomeComponentSingleton):
        s1 = Context.provide(configuration(SomeComponent))
        s2 = Context.provide(configuration(SomeComponent))
        assert s1 is s2
        s3 = Context.provide(configuration(SomeComponentSingleton))
        assert s1 is s3
Ejemplo n.º 10
0
def test_circular_dependency():
    @dependency
    class AbstractA:
        pass

    @dependency
    class AbstractB:
        pass

    @inject
    class A(AbstractA):
        b: AbstractB

        def __init__(self):
            self.b

    @inject
    class B(AbstractB):
        a: AbstractA

        def __init__(self):
            self.a

    @inject
    class Dependent:
        a: AbstractA

    with Context(A, B):
        pytest.raises(CircularDependency, lambda: Dependent().a)
Ejemplo n.º 11
0
        def create(self, date_list):
            class TestSymbolSources(SymbolSources):
                def __init__(self):
                    self.values = pd.DataFrame({
                        'close':
                        np.linspace(start=1., stop=100., num=len(date_list)),
                        'date':
                        date_list
                    })

                @property
                def sources(self):
                    return [
                        SingleFinancialSymbolSource(
                            namespace='test_ns',
                            name='test',
                            values_fetcher=lambda: self.values,
                            security_type=SecurityType.STOCK_ETF,
                            start_period=pd.Period(self.values['date'].min(),
                                                   freq='D'),
                            end_period=pd.Period(self.values['date'].max(),
                                                 freq='D'),
                            period=Period.DAY,
                            currency=Currency.RUB)
                    ]

            with Context(TestSymbolSources):
                yapo_instance = y._instance.Yapo()
                return yapo_instance
Ejemplo n.º 12
0
def test_mock_always_replaces_component():
    with Context():
        some_component_mock = mock(SomeComponent)
        some_component_mock.method.return_value = 'some other value'
        d = Dependent()
        assert d.some_component is some_component_mock
        assert d.some_component.method() == 'some other value'
Ejemplo n.º 13
0
def test_inject_with_return_annotation():
    @inject
    def f(a: int) -> int:
        return a

    with Context(a=1):
        assert f() == 1
Ejemplo n.º 14
0
def test_same_context_in_thread():
    e = Context(ConcreteComponent)

    def test():
        assert e is not Context.current_context()

    with e:
        threading.Thread(target=test).start()
Ejemplo n.º 15
0
def test_decorater():
    test_environment = Context(SomeComponent)

    @test_environment
    def test():
        component = Context.provide(configuration(SomeComponent))
        assert isinstance(component, SomeComponent)

        test()
Ejemplo n.º 16
0
def test_inject_with_no_annotations():
    @inject
    def f(a):
        return a

    with Context(a='a'):
        assert f() == 'a'

    with pytest.raises(TypeError):
        f()
Ejemplo n.º 17
0
def test_mock_is_specced():
    with Context():
        some_component_mock = mock(SomeComponent)
        assert isinstance(some_component_mock, SomeComponent)
        with pytest.raises(AttributeError):
            some_component_mock.bad_method()
        with pytest.raises(TypeError):
            some_component_mock()
        some_callable_component = mock(SomeCallableComponent)
        some_callable_component.return_value = 'mocked value'
        assert some_callable_component() == 'mocked value'
Ejemplo n.º 18
0
def test__search_exact_finsym():
    qry = 'micex/SBER'

    with Context(AllSymbolSources):
        search_instance = _Search()

    r = search_instance._check_finsym_access(query=qry)
    assert_that(r, not_none())
    assert r.identifier_str == 'micex/SBER'

    rs = y.search(query=qry)
    assert_that(rs, has_length(1))
    assert rs[0].identifier_str == 'micex/SBER'
Ejemplo n.º 19
0
def test_subtype_is_bad_dependency():
    @dependency
    class D:
        pass

    class BadDependency(D):
        def __init__(self, a):
            pass

    @inject
    class C:
        _: D

    with Context(BadDependency):
        pytest.raises(InjectionError, lambda: C()._)
Ejemplo n.º 20
0
def test_dependency_subtype_error_in_function():
    @dependency
    class D:
        pass

    class D2(D):
        def __init__(self):
            raise Exception()

    @inject
    def f(d: D):
        pass

    with Context(D2):
        with pytest.raises(InjectionError):
            f()
Ejemplo n.º 21
0
def test_mock_replaces_named_value():
    class Dependency:
        def method(self):
            return 'not value'

    e = Context(key=Dependency())
    with e:
        mock_dependency = mock('key')
        mock_dependency.method.return_value = 'value'
        with pytest.raises(AttributeError):
            mock_dependency.no_such_method()
        injected = NamedDependent().key
        assert injected == mock_dependency
        assert mock_dependency.method() == 'value'
    with e:
        injected = NamedDependent().key
        assert injected.method() == 'not value'
Ejemplo n.º 22
0
def test_intersection():
    e1 = Context(SomeComponent)
    e2 = Context(ConcreteComponent)
    e3 = e1 | e2
    assert SomeComponent in e3
    assert ConcreteComponent in e3
Ejemplo n.º 23
0
def test_inject_can_get_concrete_component():
    with Context(ConcreteDependency):
        d = AbstractDependent()
        assert isinstance(d.abstract_dependency, AbstractDependency)
        assert isinstance(d.abstract_dependency, ConcreteDependency)
Ejemplo n.º 24
0
    def test():
        component = Context.provide(configuration(SomeComponent))
        assert isinstance(component, SomeComponent)

        test()
Ejemplo n.º 25
0
from serum import Context

from ._instance import Yapo
from ._sources.all_sources import AllSymbolSources

with Context(AllSymbolSources):
    yapo_instance = Yapo()
information = yapo_instance.information
portfolio = yapo_instance.portfolio
portfolio_asset = yapo_instance.portfolio_asset
available_names = yapo_instance.available_names
search = yapo_instance.search
inflation = yapo_instance.inflation
Ejemplo n.º 26
0
def test_provides_concrete_subclass():
    with Context(ConcreteComponent):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, BaseDependency)
        assert isinstance(c, ConcreteComponent)
Ejemplo n.º 27
0
def test_named_dependency():
    with Context(key='value'):
        assert NamedDependent().key == 'value'
Ejemplo n.º 28
0
def test_injected_are_different_instances():
    with Context():
        d1 = Dependent()
        d2 = Dependent()
        assert d1.some_dependency is not d2.some_dependency
Ejemplo n.º 29
0
class MockIpythonContext:
    context = Context()
Ejemplo n.º 30
0
def test_provides_concrete_dependency():
    with Context():
        c = Context.provide(configuration(SomeComponent))
        assert isinstance(c, SomeComponent)