Example #1
0
    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
Example #2
0
    def test_propagate_work_on(self):
        """ Check custom attributes and their propagation """
        registry = ComponentRegistry()
        work = WorkContext(
            model_name='res.partner',
            collection=self.collection,
            # we can customize the lookup registry, but used mostly for tests
            components_registry=registry,
            # we can pass our own keyword args that will set as attributes
            test_keyword='value',
        )
        self.assertIs(registry, work.components_registry)
        # check that our custom keyword is set as attribute
        self.assertEqual('value', work.test_keyword)

        # when we want to work on another model, work_on() create
        # another instance and propagate the attributes to it
        work2 = work.work_on('res.users')
        self.assertNotEqual(work, work2)
        self.assertEqual(self.env, work2.env)
        self.assertEqual(self.collection, work2.collection)
        self.assertEqual('res.users', work2.model_name)
        self.assertIs(registry, work2.components_registry)
        # test_keyword has been propagated to the new WorkContext instance
        self.assertEqual('value', work2.test_keyword)
Example #3
0
    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 test_collection_work_on_registry_via_context(self):
     """ Test propagation of registry via context """
     registry = ComponentRegistry()
     collection_record = self.collection.with_context(
         components_registry=registry).new()
     with collection_record.work_on("res.partner") as work:
         self.assertEqual(collection_record, work.collection)
         self.assertEqual("collection.base", work.collection._name)
         self.assertEqual("res.partner", work.model_name)
         self.assertEqual(self.env["res.partner"], work.model)
         self.assertEqual(work.env, collection_record.env)
         self.assertEqual(work.components_registry, registry)
Example #5
0
class ComponentRegistryCase(unittest.TestCase,
                            common.MetaCase('DummyCase', (object, ), {})):
    """ This test case can be used as a base for writings tests on components

    This test case is meant to test components in a special component registry,
    where you want to have maximum control on which components are loaded
    or not, or when you want to create additional components in your tests.

    If you only want to *use* the components of the tested addon in your tests,
    then consider using one of:

    * :class:`TransactionComponentCase`
    * :class:`SavepointComponentCase`

    This test case creates a special
    :class:`odoo.addons.component.core.ComponentRegistry` for the purpose of
    the tests. By default, it loads all the components of the dependencies, but
    not the components of the current addon (which you have to handle
    manually). In your tests, you can add more components in 2 manners.

    All the components of an Odoo module::

        self._load_module_components('connector')

    Only specific components::

        self._build_components(MyComponent1, MyComponent2)

    Note: for the lookups of the components, the default component
    registry is a global registry for the database. Here, you will
    need to explicitly pass ``self.comp_registry`` in the
    :class:`~odoo.addons.component.core.WorkContext`::

        work = WorkContext(model_name='res.users',
                           collection='my.collection',
                           components_registry=self.comp_registry)

    Or::

        collection_record = self.env['my.collection'].browse(1)
        with collection_record.work_on(
                'res.partner',
                components_registry=self.comp_registry) as work:

    """
    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 tearDown(self):
        super(ComponentRegistryCase, self).tearDown()
        # restore the original metaclass' classes
        MetaComponent._modules_components = self._original_components

    def _load_module_components(self, module):
        self.comp_registry.load_components(module)

    def _build_components(self, *classes):
        for cls in classes:
            cls._build_component(self.comp_registry)