def test_call_selector_is_none(self): provider = providers.Selector( self.selector, one=providers.Object(1), two=providers.Object(2), ) with self.selector.override(None): with self.assertRaises(errors.Error): provider()
def test_call_undefined_provider(self): provider = providers.Selector( self.selector, one=providers.Object(1), two=providers.Object(2), ) with self.selector.override('three'): with self.assertRaises(errors.Error): provider()
def test_traverse_attributes(self): provider1 = providers.Object('bar') provider2 = providers.Object('baz') provider = providers.Singleton(dict) provider.add_attributes(foo='foo', bar=provider1, baz=provider2) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 2) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers)
def test_call_any_callable(selector): selector.set_selector(functools.partial(next, itertools.cycle(["one", "two"]))) selector.set_providers( one=providers.Object(1), two=providers.Object(2), ) assert selector() == 1 assert selector() == 2 assert selector() == 1 assert selector() == 2
def fabric(self, configuration, args): """Load discovery, group, pathfinder, report plugins.""" if args.verbose: if args.verbose == 1: configuration.get("log", {}).get("root", {})["level"] = logging.INFO elif args.verbose > 1: configuration.get("log", {}).get("root", {})["level"] = logging.DEBUG dictConfig(configuration.get("log", {})) self.log = logging.getLogger("cloudselect.CloudSelect") self.log.debug("Logging is initialized") self.log.debug( "Configuration:\n%s", json.dumps(configuration, sort_keys=True, indent=4, separators=(",", ": ")), ) Container.args = providers.Object(args) Container.config = providers.Configuration(name="config", default=configuration) Container.configpath = providers.Object(self.configpath) Container.extensions = providers.Object(self.extensions) Container.options = providers.Callable(self.options) Container.selector = providers.Singleton(Selector) Container.discovery = self.fabric_load_plugin( configuration, "discovery", DiscoveryServiceProvider, DiscoveryStub, ) Container.group = self.fabric_load_plugin( configuration, "group", GroupServiceProvider, GroupStub, ) Container.pathfinder = self.fabric_load_plugin( configuration, "pathfinder", PathFinderServiceProvider, PathFinderStub, ) Container.report = self.fabric_load_plugin( configuration, "report", ReportServiceProvider, ReportStub, args.reporter, ) return Container.selector()
def test_call_any_callable(self): provider = providers.Selector( functools.partial(next, itertools.cycle(['one', 'two'])), one=providers.Object(1), two=providers.Object(2), ) self.assertEqual(provider(), 1) self.assertEqual(provider(), 2) self.assertEqual(provider(), 1) self.assertEqual(provider(), 2)
def test_getattr_attribute_error(self): provider_one = providers.Object(1) provider_two = providers.Object(2) provider = providers.Selector( self.selector, one=provider_one, two=provider_two, ) with self.assertRaises(AttributeError): _ = provider.provider_three
def test_getattr(self): provider_one = providers.Object(1) provider_two = providers.Object(2) provider = providers.Selector( self.selector, one=provider_one, two=provider_two, ) self.assertIs(provider.one, provider_one) self.assertIs(provider.two, provider_two)
def test_call(self): provider = providers.Selector( self.selector, one=providers.Object(1), two=providers.Object(2), ) with self.selector.override('one'): self.assertEqual(provider(), 1) with self.selector.override('two'): self.assertEqual(provider(), 2)
def test_traverse_kwargs(): provider1 = providers.Object("bar") provider2 = providers.Object("baz") provider = providers.Callable(dict, foo="foo", bar=provider1, baz=provider2) all_providers = list(provider.traverse()) assert len(all_providers) == 2 assert provider1 in all_providers assert provider2 in all_providers
def test_call_overridden(self): provider = providers.Selector(self.selector, sample=providers.Object(1)) overriding_provider1 = providers.Selector(self.selector, sample=providers.Object(2)) overriding_provider2 = providers.Selector(self.selector, sample=providers.Object(3)) provider.override(overriding_provider1) provider.override(overriding_provider2) with self.selector.override('sample'): self.assertEqual(provider(), 3)
def test_traverse_overridden(): provider1 = providers.Object("bar") provider2 = providers.Object("baz") provider = providers.Callable(dict, "foo") provider.override(provider1) provider.override(provider2) all_providers = list(provider.traverse()) assert len(all_providers) == 2 assert provider1 in all_providers assert provider2 in all_providers
def test_traverse_overridden(self): provider1 = providers.Object('bar') provider2 = providers.Object('baz') provider = providers.Singleton(dict, 'foo') provider.override(provider1) provider.override(provider2) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 2) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers)
def test_init_optional(self): one = providers.Object(1) two = providers.Object(2) provider = providers.Selector() provider.set_selector(self.selector) provider.set_providers(one=one, two=two) self.assertEqual(provider.providers, {'one': one, 'two': two}) with self.selector.override('one'): self.assertEqual(provider(), one()) with self.selector.override('two'): self.assertEqual(provider(), two())
def test_traverse_provides(self): provider1 = providers.Callable(list) provider2 = providers.Object('bar') provider3 = providers.Object('baz') provider = providers.Singleton(provider1, provider2) provider.override(provider3) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 3) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers) self.assertIn(provider3, all_providers)
def test_providers_attribute(self): provider_one = providers.Object(1) provider_two = providers.Object(2) provider = providers.Selector( self.selector, one=provider_one, two=provider_two, ) self.assertEqual(provider.providers, { 'one': provider_one, 'two': provider_two })
def test_backtest_auto_updates_outdated_csharp_vscode_debug_config() -> None: create_fake_lean_cli_directory() launch_json_path = Path.cwd( ) / "CSharp Project" / ".vscode" / "launch.json" _generate_file( launch_json_path, """ { "version": "0.2.0", "configurations": [ { "name": "Debug with Lean CLI", "request": "attach", "type": "mono", "address": "localhost", "port": 55556 } ] } """) docker_manager = mock.Mock() container.docker_manager.override(providers.Object(docker_manager)) lean_runner = mock.Mock() container.lean_runner.override(providers.Object(lean_runner)) result = CliRunner().invoke( lean, ["backtest", "CSharp Project", "--debug", "vsdbg"]) assert result.exit_code == 0 launch_json = json.loads(launch_json_path.read_text(encoding="utf-8")) assert len(launch_json["configurations"]) == 1 assert launch_json["configurations"][0] == { "name": "Debug with Lean CLI", "request": "attach", "type": "coreclr", "processId": "1", "pipeTransport": { "pipeCwd": "${workspaceRoot}", "pipeProgram": "docker", "pipeArgs": ["exec", "-i", "lean_cli_vsdbg"], "debuggerPath": "/root/vsdbg/vsdbg", "quoteArgs": False }, "logging": { "moduleLoad": False } }
def selector(selector_type, switch, one, two): if selector_type == "default": return providers.Selector(switch, one=one, two=two) elif selector_type == "empty": return providers.Selector() elif selector_type == "sys-streams": return providers.Selector( switch, stdin=providers.Object(sys.stdin), stdout=providers.Object(sys.stdout), stderr=providers.Object(sys.stderr), ) else: raise ValueError("Unknown selector type \"{0}\"".format(selector_type))
class ContainerWithSingletons(containers.DeclarativeContainer): resource1 = providers.Resource(init_resource, providers.Object(RESOURCE1)) resource2 = providers.Resource(init_resource, providers.Object(RESOURCE2)) client = providers.Singleton( Client, resource1=resource1, resource2=resource2, ) service = providers.Singleton( Service, client=client, )
def test_traverse_provides(): provider1 = providers.Callable(list) provider2 = providers.Object("bar") provider3 = providers.Object("baz") provider = providers.Callable(provider1, provider2) provider.override(provider3) all_providers = list(provider.traverse()) assert len(all_providers) == 3 assert provider1 in all_providers assert provider2 in all_providers assert provider3 in all_providers
def test_traverse_overridden(): provider1 = providers.Object("bar") provider2 = providers.Object("baz") provider3 = providers.List(provider1, provider2) provider = providers.List("foo") provider.override(provider3) all_providers = list(provider.traverse()) assert len(all_providers) == 3 assert provider1 in all_providers assert provider2 in all_providers assert provider3 in all_providers
class OpenSlidesBackendWSGI(containers.DeclarativeContainer): """ Container for dependency injection into OpenSlidesBackendWSGIApplication. """ logging = providers.Object(0) view = providers.Object(0) services = providers.DependenciesContainer() setup = providers.Factory( OpenSlidesBackendWSGIApplication, logging=logging, view=view, services=services, )
def test_live_aborts_when_environment_does_not_exist() -> None: create_fake_lean_cli_directory() docker_manager = mock.Mock() container.docker_manager.override(providers.Object(docker_manager)) lean_runner = mock.Mock() container.lean_runner.override(providers.Object(lean_runner)) result = CliRunner().invoke(lean, ["live", "Python Project", "--environment", "fake-environment"]) assert result.exit_code != 0 lean_runner.run_lean.assert_not_called()
def test_traverse_overridden(self): provider1 = providers.Object('bar') provider2 = providers.Object('baz') provider3 = providers.Dict(bar=provider1, baz=provider2) provider = providers.Dict(foo='foo') provider.override(provider3) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 3) self.assertIn(provider1, all_providers) self.assertIn(provider2, all_providers) self.assertIn(provider3, all_providers)
class Container(containers.DeclarativeContainer): resource1 = providers.Resource(init_resource, providers.Object(RESOURCE1)) resource2 = providers.Resource(init_resource, providers.Object(RESOURCE2)) client = providers.Factory( Client, resource1=resource1, resource2=resource2, ) service = providers.Factory( Service, client=client, )
def test_backtest_auto_updates_outdated_python_pycharm_debug_config() -> None: create_fake_lean_cli_directory() workspace_xml_path = Path.cwd( ) / "Python Project" / ".idea" / "workspace.xml" _generate_file( workspace_xml_path, """ <?xml version="1.0" encoding="UTF-8"?> <project version="4"> <component name="RunManager" selected="Python Debug Server.Debug with Lean CLI"> <configuration name="Debug with Lean CLI" type="PyRemoteDebugConfigurationType" factoryName="Python Remote Debug"> <module name="LEAN" /> <option name="PORT" value="6000" /> <option name="HOST" value="localhost" /> <PathMappingSettings> <option name="pathMappings"> <list> <mapping local-root="$PROJECT_DIR$" remote-root="/Lean/Launcher/bin/Debug" /> </list> </option> </PathMappingSettings> <option name="REDIRECT_OUTPUT" value="true" /> <option name="SUSPEND_AFTER_CONNECT" value="true" /> <method v="2" /> </configuration> <list> <item itemvalue="Python Debug Server.Debug with Lean CLI" /> </list> </component> </project> """) docker_manager = mock.Mock() container.docker_manager.override(providers.Object(docker_manager)) lean_runner = mock.Mock() container.lean_runner.override(providers.Object(lean_runner)) result = CliRunner().invoke( lean, ["backtest", "Python Project", "--debug", "pycharm"]) assert result.exit_code == 1 workspace_xml = XMLManager().parse( workspace_xml_path.read_text(encoding="utf-8")) assert workspace_xml.find( ".//mapping[@remote-root='/LeanCLI']") is not None assert workspace_xml.find( ".//mapping[@remote-root='/Lean/Launcher/bin/Debug']") is None
def test_live_aborts_when_project_does_not_exist() -> None: create_fake_lean_cli_directory() create_fake_environment("live-paper", True) docker_manager = mock.Mock() container.docker_manager.override(providers.Object(docker_manager)) lean_runner = mock.Mock() container.lean_runner.override(providers.Object(lean_runner)) result = CliRunner().invoke(lean, ["live", "This Project Does Not Exist"]) assert result.exit_code != 0 lean_runner.run_lean.assert_not_called()
def test_backtest_aborts_when_project_does_not_contain_algorithm_file( ) -> None: create_fake_lean_cli_directory() docker_manager = mock.Mock() container.docker_manager.override(providers.Object(docker_manager)) lean_runner = mock.Mock() container.lean_runner.override(providers.Object(lean_runner)) result = CliRunner().invoke(lean, ["backtest", "data"]) assert result.exit_code != 0 lean_runner.run_lean.assert_not_called()
class ContainerWithAttributes(containers.DeclarativeContainer): resource1 = providers.Resource(init_resource, providers.Object(RESOURCE1)) client = providers.Factory( Client, resource1, resource2=None, ) client.add_attributes(resource2=providers.Object(RESOURCE2)) service = providers.Factory( Service, client=None, ) service.add_attributes(client=client)
def test_repr(self): provider = providers.Selector( self.selector, one=providers.Object(1), two=providers.Object(2), ) self.assertIn( '<dependency_injector.providers.Selector({0}'.format( repr(self.selector)), repr(provider), ) self.assertIn('one={0}'.format(repr(provider.one)), repr(provider)) self.assertIn('two={0}'.format(repr(provider.two)), repr(provider)) self.assertIn('at {0}'.format(hex(id(provider))), repr(provider))