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
def test_gets_most_specific(): class ConcreteComponentSub(ConcreteComponent): pass with Context(ConcreteComponent, ConcreteComponentSub): c = Context.provide(configuration(BaseDependency)) assert isinstance(c, ConcreteComponentSub)
def test_inject_singleton_without_context(): Context._set_current_env(None) @inject class C: instance: SomeSingleton assert C().instance is C().instance
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)
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)
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
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)
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)
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
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)
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
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'
def test_inject_with_return_annotation(): @inject def f(a: int) -> int: return a with Context(a=1): assert f() == 1
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()
def test_decorater(): test_environment = Context(SomeComponent) @test_environment def test(): component = Context.provide(configuration(SomeComponent)) assert isinstance(component, SomeComponent) test()
def test_inject_with_no_annotations(): @inject def f(a): return a with Context(a='a'): assert f() == 'a' with pytest.raises(TypeError): f()
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'
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'
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()._)
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()
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'
def test_intersection(): e1 = Context(SomeComponent) e2 = Context(ConcreteComponent) e3 = e1 | e2 assert SomeComponent in e3 assert ConcreteComponent in e3
def test_inject_can_get_concrete_component(): with Context(ConcreteDependency): d = AbstractDependent() assert isinstance(d.abstract_dependency, AbstractDependency) assert isinstance(d.abstract_dependency, ConcreteDependency)
def test(): component = Context.provide(configuration(SomeComponent)) assert isinstance(component, SomeComponent) test()
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
def test_provides_concrete_subclass(): with Context(ConcreteComponent): c = Context.provide(configuration(BaseDependency)) assert isinstance(c, BaseDependency) assert isinstance(c, ConcreteComponent)
def test_named_dependency(): with Context(key='value'): assert NamedDependent().key == 'value'
def test_injected_are_different_instances(): with Context(): d1 = Dependent() d2 = Dependent() assert d1.some_dependency is not d2.some_dependency
class MockIpythonContext: context = Context()
def test_provides_concrete_dependency(): with Context(): c = Context.provide(configuration(SomeComponent)) assert isinstance(c, SomeComponent)