예제 #1
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)
예제 #2
0
파일: test_match.py 프로젝트: suned/serum
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
예제 #3
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)
예제 #4
0
파일: test_mock.py 프로젝트: suned/serum
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)
예제 #5
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
예제 #6
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)
예제 #7
0
def test_inject_with_return_annotation():
    @inject
    def f(a: int) -> int:
        return a

    with Context(a=1):
        assert f() == 1
예제 #8
0
def test_gets_most_specific():
    class ConcreteComponentSub(ConcreteComponent):
        pass

    with Context(ConcreteComponent, ConcreteComponentSub):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, ConcreteComponentSub)
예제 #9
0
파일: test_mock.py 프로젝트: suned/serum
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'
예제 #10
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()
예제 #11
0
def test_decorater():
    test_environment = Context(SomeComponent)

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

        test()
예제 #12
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)
예제 #13
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()
예제 #14
0
파일: test_mock.py 프로젝트: suned/serum
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'
예제 #15
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
예제 #16
0
파일: test_search.py 프로젝트: Steap/yapo
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'
예제 #17
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()._)
예제 #18
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()
예제 #19
0
파일: test_mock.py 프로젝트: suned/serum
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'
예제 #20
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)
예제 #21
0
def test_fails_with_ambiguous_dependencies():
    with Context(ConcreteComponent, AlternativeComponent):
        with pytest.raises(AmbiguousDependencies):
            Context.provide(configuration(BaseDependency))
예제 #22
0
def test_named_dependency():
    with Context(key='value'):
        assert NamedDependent().key == 'value'
예제 #23
0
def test_provides_concrete_dependency():
    with Context():
        c = Context.provide(configuration(SomeComponent))
        assert isinstance(c, SomeComponent)
예제 #24
0
class MockIpythonContext:
    context = Context()
예제 #25
0
파일: __init__.py 프로젝트: Steap/yapo
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
예제 #26
0
def test_singleton_is_always_same_instance():
    with Context():
        s1 = Context.provide(configuration(SomeSingleton))
        s2 = Context.provide(configuration(SomeSingleton))
        assert s1 is s2
예제 #27
0
def test_provides_concrete_subclass():
    with Context(ConcreteComponent):
        c = Context.provide(configuration(BaseDependency))
        assert isinstance(c, BaseDependency)
        assert isinstance(c, ConcreteComponent)
예제 #28
0
def test_injected_are_different_instances():
    with Context():
        d1 = Dependent()
        d2 = Dependent()
        assert d1.some_dependency is not d2.some_dependency
예제 #29
0
def test_intersection():
    e1 = Context(SomeComponent)
    e2 = Context(ConcreteComponent)
    e3 = e1 | e2
    assert SomeComponent in e3
    assert ConcreteComponent in e3
예제 #30
0
def test_can_register_dependency():
    e = Context(SomeComponent)
    assert SomeComponent in e