def _setup_registry(class_or_instance): ComponentRegistryCase._setup_registry(class_or_instance) class_or_instance._service_registry = RestServicesRegistry() # take a copy of registered controllers controllers = http.controllers_per_module http.controllers_per_module = controllers class_or_instance._controllers_per_module = copy.deepcopy( http.controllers_per_module) class_or_instance._original_addon_rest_controllers_per_module = copy.deepcopy( _rest_controllers_per_module[_get_addon_name( class_or_instance.__module__)]) db_name = get_db_name() # makes the test component registry available for the db name _component_databases[db_name] = class_or_instance.comp_registry # makes the test service registry available for the db name class_or_instance._original_services_registry = _rest_services_databases.get( db_name, {}) _rest_services_databases[db_name] = class_or_instance._service_registry # build the services and controller of every installed addons # but the current addon (when running with pytest/nosetest, we # simulate the --test-enable behavior by excluding the current addon # which is in 'to install' / 'to upgrade' with --test-enable). current_addon = _get_addon_name(class_or_instance.__module__) with new_rollbacked_env() as env: RestServiceRegistration = env["rest.service.registration"] RestServiceRegistration.build_registry( class_or_instance._service_registry, states=("installed", ), exclude_addons=[current_addon], ) RestServiceRegistration._build_controllers_routes( class_or_instance._service_registry) # register our components class_or_instance.comp_registry.load_components("base_rest") # Define a base test controller here to avoid to have this controller # registered outside tests class_or_instance._collection_name = "base.rest.test" BaseTestController = class_or_instance._get_test_controller( class_or_instance) class_or_instance._BaseTestController = BaseTestController class_or_instance._controller_route_method_names = { "my_controller_route_without", "my_controller_route_with", "my_controller_route_without_auth_2", }
def _build_services(self, *classes): self._build_components(*classes) with new_rollbacked_env() as env: RestServiceRegistration = env["rest.service.registration"] current_addon = _get_addon_name(self.__module__) RestServiceRegistration.load_services(current_addon, self._service_registry) RestServiceRegistration._build_controllers_routes(self._service_registry)
def __init__(cls, name, bases, attrs): # noqa: B902 if ("RestController" in globals() and RestController in bases and Controller not in bases): # to be registered as a controller into the ControllerType, # our RestConrtroller must be a direct child of Controller bases += (Controller, ) super(RestControllerType, cls).__init__(name, bases, attrs) if "RestController" not in globals() or not any( issubclass(b, RestController) for b in bases): return # register the rest controller into the rest controllers registry root_path = getattr(cls, "_root_path", None) collection_name = getattr(cls, "_collection_name", None) if root_path and collection_name: cls._module = _get_addon_name(cls.__module__) _rest_controllers_per_module[cls._module].append({ "root_path": root_path, "collection_name": collection_name, "controller_class": cls, }) _logger.debug( "Added rest controller %s for module %s", _rest_controllers_per_module[cls._module][-1], cls._module, )
def setUp(self): super(ComponentRegistryCase, self).setUp() # keep the original classes registered by the metaclass # so we'll restore them at the end of the tests, it avoid # to pollute it with Stub / Test components self._original_components = copy.deepcopy( MetaComponent._modules_components) # it will be our temporary component registry for our test session self.comp_registry = ComponentRegistry() # it builds the 'final component' for every component of the # 'component' addon and push them in the component registry self.comp_registry.load_components('component') # build the components of every installed addons already installed # but the current addon (when running with pytest/nosetest, we # simulate the --test-enable behavior by excluding the current addon # which is in 'to install' / 'to upgrade' with --test-enable). current_addon = _get_addon_name(self.__module__) with new_rollbacked_env() as env: env['component.builder'].build_registry( self.comp_registry, states=('installed', ), exclude_addons=[current_addon], ) # Fake that we are ready to work with the registry # normally, it is set to True and the end of the build # of the components. Here, we'll add components later in # the components registry, but we don't mind for the tests. self.comp_registry.ready = True
def _setup_registry(class_or_instance): # keep the original classes registered by the metaclass # so we'll restore them at the end of the tests, it avoid # to pollute it with Stub / Test components class_or_instance._original_components = copy.deepcopy(MetaComponent._modules_components) # it will be our temporary component registry for our test session class_or_instance.comp_registry = ComponentRegistry() # it builds the 'final component' for every component of the # 'component' addon and push them in the component registry class_or_instance.comp_registry.load_components("component") # build the components of every installed addons already installed # but the current addon (when running with pytest/nosetest, we # simulate the --test-enable behavior by excluding the current addon # which is in 'to install' / 'to upgrade' with --test-enable). current_addon = _get_addon_name(class_or_instance.__module__) with new_rollbacked_env() as env: env["component.builder"].build_registry( class_or_instance.comp_registry, states=("installed",), exclude_addons=[current_addon], ) # Fake that we are ready to work with the registry # normally, it is set to True and the end of the build # of the components. Here, we'll add components later in # the components registry, but we don't mind for the tests. class_or_instance.comp_registry.ready = True if hasattr(class_or_instance, "env"): # let it propagate via ctx class_or_instance.env.context = dict( class_or_instance.env.context, components_registry=class_or_instance.comp_registry, )
def _teardown_registry(class_or_instance): ComponentRegistryCase._teardown_registry(class_or_instance) http.controllers_per_module = class_or_instance._controllers_per_module db_name = get_db_name() _component_databases[db_name] = class_or_instance._original_components _rest_services_databases[ db_name] = class_or_instance._original_services_registry class_or_instance._service_registry = {} _rest_controllers_per_module[_get_addon_name( class_or_instance.__module__ )] = class_or_instance._original_addon_rest_controllers_per_module
def setUpComponent(cls): with new_rollbacked_env() as env: builder = env['component.builder'] # build the components of every installed addons comp_registry = builder._init_global_registry() cls._components_registry = comp_registry # ensure that we load only the components of the 'installed' # modules, not 'to install', which means we load only the # dependencies of the tested addons, not the siblings or # chilren addons builder.build_registry(comp_registry, states=('installed', )) # build the components of the current tested addon current_addon = _get_addon_name(cls.__module__) env['component.builder'].load_components(current_addon)
def setUpRegistry(cls): with new_rollbacked_env() as env: service_registration = env['rest.service.registration'] # build the registry of every installed addons services_registry = service_registration._init_global_registry() cls._services_registry = services_registry # ensure that we load only the services of the 'installed' # modules, not 'to install', which means we load only the # dependencies of the tested addons, not the siblings or # children addons service_registration.build_registry(services_registry, states=('installed', )) # build the services of the current tested addon current_addon = _get_addon_name(cls.__module__) env['rest.service.registration'].load_services( current_addon, services_registry)
def __init__(cls, name, bases, attrs): super(RestControllerType, cls).__init__(name, bases, attrs) if "RestController" not in globals() or RestController not in bases: return # register our REST controllers root_path = getattr(cls, '_root_path', None) collection_name = getattr(cls, '_collection_name', None) if root_path and collection_name: if not hasattr(cls, '_module'): cls._module = _get_addon_name(cls.__module__) _rest_controllers_per_module[cls._module].append({ 'root_path': root_path, 'collection_name': collection_name })
def __init__(cls, name, bases, attrs): # noqa: B902 if ("RestController" in globals() and RestController in bases and Controller not in bases): # to be registered as a controller into the ControllerType, # our RestConrtroller must be a direct child of Controller bases += (Controller, ) super(RestControllerType, cls).__init__(name, bases, attrs) if "RestController" not in globals() or RestController not in bases: return # register the rest controller into the rest controllers registry root_path = getattr(cls, "_root_path", None) collection_name = getattr(cls, "_collection_name", None) if root_path and collection_name: if not hasattr(cls, "_module"): cls._module = _get_addon_name(cls.__module__) _rest_controllers_per_module[cls._module].append({ "root_path": root_path, "collection_name": collection_name })