Beispiel #1
0
 def test_collection_work_on(self):
     """ WorkContext propagated through work_on """
     env = mock.MagicMock(name='env')
     collection = mock.MagicMock(name='collection')
     collection.env = env
     work = EventWorkContext(collection=collection, model_name='res.users',
                             components_registry=self.components_registry)
     work2 = work.work_on(model_name='res.partner')
     self.assertEqual('WorkContext', work2.__class__.__name__)
     self.assertEqual(collection, work2.collection)
     self.assertEqual(env, work2.env)
     self.assertEqual('res.partner', work2.model_name)
     self.assertEqual(self.components_registry, work2.components_registry)
Beispiel #2
0
 def test_env_work_on(self):
     """ WorkContext propagated through work_on """
     env = mock.MagicMock(name='env')
     collection = mock.MagicMock(name='collection')
     collection.env = env
     work = EventWorkContext(env=env, model_name='res.users',
                             components_registry=self.components_registry)
     work2 = work.work_on(model_name='res.partner', collection=collection)
     self.assertEqual('WorkContext', work2.__class__.__name__)
     self.assertEqual(env, work2.env)
     self.assertEqual('res.partner', work2.model_name)
     self.assertEqual(self.components_registry, work2.components_registry)
     with self.assertRaises(ValueError):
         # pylint: disable=W0104
         work.collection  # noqa
Beispiel #3
0
 def test_collection_work_on_collection(self):
     """ WorkContext collection changed with work_on """
     env = mock.MagicMock(name='env')
     collection = mock.MagicMock(name='collection')
     collection.env = env
     work = EventWorkContext(model_name='res.users', env=env,
                             components_registry=self.components_registry)
     work2 = work.work_on(collection=collection)
     # when work_on is used inside an event component, we want
     # to switch back to a normal WorkContext, because we don't
     # need anymore the EventWorkContext
     self.assertEqual('WorkContext', work2.__class__.__name__)
     self.assertEqual(collection, work2.collection)
     self.assertEqual(env, work2.env)
     self.assertEqual('res.users', work2.model_name)
     self.assertEqual(self.components_registry, work2.components_registry)
Beispiel #4
0
 def test_env_and_collection(self):
     """ WorkContext with collection and env is forbidden """
     env = mock.MagicMock(name='env')
     collection = mock.MagicMock(name='collection')
     collection.env = env
     with self.assertRaises(ValueError):
         EventWorkContext(model_name='res.users', collection=collection,
                          env=env,
                          components_registry=self.components_registry)
Beispiel #5
0
 def test_env(self):
     """ WorkContext with env """
     work = EventWorkContext(model_name='res.users', env=self.env,
                             components_registry=self.components_registry)
     self.assertEqual(self.env, work.env)
     self.assertEqual('res.users', work.model_name)
     with self.assertRaises(ValueError):
         # pylint: disable=W0104
         work.collection  # noqa
Beispiel #6
0
 def test_collection(self):
     """ WorkContext with collection """
     env = mock.MagicMock(name='env')
     collection = mock.MagicMock(name='collection')
     collection.env = env
     work = EventWorkContext(model_name='res.users', collection=collection,
                             components_registry=self.components_registry)
     self.assertEqual(collection, work.collection)
     self.assertEqual(env, work.env)
     self.assertEqual('res.users', work.model_name)
Beispiel #7
0
    def test_event_cache(self):
        class MyEventListener(Component):
            _name = 'my.event.listener'
            _inherit = 'base.event.listener'

            def on_record_create(self):
                pass

        MyEventListener._build_component(self.comp_registry)

        # collect the event
        collected = self.collecter.collect_events('on_record_create')
        # CollectedEvents.events contains the collected events
        self.assertEqual(1, len(collected.events))
        event = list(collected.events)[0]
        self.assertEqual(self.work, event.__self__.work)
        self.assertEqual(self.work.env, event.__self__.work.env)

        # build and register a new listener
        class MyOtherEventListener(Component):
            _name = 'my.other.event.listener'
            _inherit = 'base.event.listener'

            def on_record_create(self):
                pass

        MyOtherEventListener._build_component(self.comp_registry)

        # get a new collecter and check that we it finds the same
        # events even if we built a new one: it means the cache works
        env = mock.MagicMock()
        work = EventWorkContext(model_name='res.users', env=env,
                                components_registry=self.comp_registry)
        collecter = self.comp_registry['base.event.collecter'](work)
        collected = collecter.collect_events('on_record_create')
        # CollectedEvents.events contains the collected events
        self.assertEqual(1, len(collected.events))
        event = list(collected.events)[0]
        self.assertEqual(work, event.__self__.work)
        self.assertEqual(env, event.__self__.work.env)

        # if we empty the cache, as it on the class, both collecters
        # should now find the 2 events
        collecter._cache.clear()
        self.comp_registry._cache.clear()
        # CollectedEvents.events contains the collected events
        self.assertEqual(
            2,
            len(collecter.collect_events('on_record_create').events)
        )
        self.assertEqual(
            2,
            len(self.collecter.collect_events('on_record_create').events)
        )
Beispiel #8
0
    def setUp(self):
        super(TestEvent, self).setUp()
        self._load_module_components('component_event')

        # get the collecter to notify the event
        # we don't mind about the collection and the model here,
        # the events we test are global
        env = mock.MagicMock()
        self.work = EventWorkContext(model_name='res.users', env=env,
                                     components_registry=self.comp_registry)
        self.collecter = self.comp_registry['base.event.collecter'](self.work)
Beispiel #9
0
    def test_event_cache_collection(self):
        class MyEventListener(Component):
            _name = "my.event.listener"
            _inherit = "base.event.listener"

            def on_record_create(self):
                pass

        MyEventListener._build_component(self.comp_registry)

        # collect the event
        collected = self.collecter.collect_events("on_record_create")
        # CollectedEvents.events contains the collected events
        self.assertEqual(1, len(collected.events))

        # build and register a new listener
        class MyOtherEventListener(Component):
            _name = "my.other.event.listener"
            _inherit = "base.event.listener"
            _collection = "base.collection"

            def on_record_create(self):
                pass

        MyOtherEventListener._build_component(self.comp_registry)

        # get a new collecter and check that we it finds the same
        # events even if we built a new one: it means the cache works
        collection = mock.MagicMock(name="base.collection")
        collection._name = "base.collection"
        collection.env = mock.MagicMock()
        work = EventWorkContext(
            model_name="res.users",
            collection=collection,
            components_registry=self.comp_registry,
        )
        collecter = self.comp_registry["base.event.collecter"](work)
        collected = collecter.collect_events("on_record_create")
        # for a different collection, we should not have the same
        # cache entry
        self.assertEqual(2, len(collected.events))
Beispiel #10
0
    def test_skip_if_no_connector_export(self):
        class MyEventListener(Component):
            _name = "my.event.listener"
            _inherit = "base.event.listener"

            def on_record_create(self, record, fields=None):
                assert True

        class MyOtherEventListener(Component):
            _name = "my.other.event.listener"
            _inherit = "base.connector.listener"

            @skip_if(lambda self, record, fields=None: self.no_connector_export(record))
            def on_record_create(self, record, fields=None):
                raise AssertionError()

        self.env.context = frozendict(self.env.context, no_connector_export=True)
        work = EventWorkContext(
            model_name="res.users", env=self.env, components_registry=self.comp_registry
        )

        # get the collecter to notify the event
        # we don't mind about the collection and the model here,
        # the events we test are global
        self.collecter = self.comp_registry["base.event.collecter"](work)

        self._build_components(
            components.core.BaseConnectorComponent,
            components.listener.ConnectorListener,
            MyEventListener,
            MyOtherEventListener,
        )

        # collect the event and notify it
        record = mock.Mock(name="record")
        collected = self.collecter.collect_events("on_record_create")
        self.assertEqual(2, len(collected.events))
        collected.notify(record)
Beispiel #11
0
    def test_event_cache_model_name(self):
        class MyEventListener(Component):
            _name = 'my.event.listener'
            _inherit = 'base.event.listener'

            def on_record_create(self):
                pass

        MyEventListener._build_component(self.comp_registry)

        # collect the event
        collected = self.collecter.collect_events('on_record_create')
        # CollectedEvents.events contains the collected events
        self.assertEqual(1, len(collected.events))

        # build and register a new listener
        class MyOtherEventListener(Component):
            _name = 'my.other.event.listener'
            _inherit = 'base.event.listener'
            _apply_on = ['res.country']

            def on_record_create(self):
                pass

        MyOtherEventListener._build_component(self.comp_registry)

        # get a new collecter and check that we it finds the same
        # events even if we built a new one: it means the cache works
        env = mock.MagicMock()
        work = EventWorkContext(model_name='res.country',
                                env=env,
                                components_registry=self.comp_registry)
        collecter = self.comp_registry['base.event.collecter'](work)
        collected = collecter.collect_events('on_record_create')
        # for a different collection, we should not have the same
        # cache entry
        self.assertEqual(2, len(collected.events))
Beispiel #12
0
 def test_missing(self):
     """ WorkContext with collection and env is forbidden """
     with self.assertRaises(ValueError):
         EventWorkContext(model_name='res.users',
                          components_registry=self.components_registry)