Beispiel #1
0
    def test_channel_on_job(self):
        job(task_a)
        self.function_model._register_jobs()
        path_a = '%s.%s' % (task_a.__module__, task_a.__name__)
        job_func = self.function_model.search([('name', '=', path_a)])
        self.assertEquals(job_func.channel, 'root')

        test_job = Job(func=task_a)
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        stored = self.job_model.search([('uuid', '=', test_job.uuid)])
        self.assertEquals(stored.channel, 'root')

        channel = self.channel_model.create({'name': 'sub',
                                             'parent_id': self.root_channel.id,
                                             })
        job_func.channel_id = channel

        test_job = Job(func=task_a)
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        stored = self.job_model.search([('uuid', '=', test_job.uuid)])
        self.assertEquals(stored.channel, 'root.sub')

        # it's also possible to override the channel
        test_job = Job(func=task_a, channel='root.sub.sub.sub')
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        stored = self.job_model.search([('uuid', '=', test_job.uuid)])
        self.assertEquals(stored.channel, test_job.channel)
Beispiel #2
0
    def test_channel_on_job(self):
        job(task_a)
        self.function_model._register_jobs(self.cr)
        path_a = 'openerp.addons.connector.tests.test_job.task_a'
        func_ids = self.function_model.search(self.cr, self.uid,
                                              [('name', '=', path_a)])
        self.assertEqual(len(func_ids), 1)
        job_func = self.function_model.browse(self.cr, self.uid, func_ids[0])
        self.assertEquals(job_func.channel, 'root')

        test_job = Job(func=task_a)
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        stored_ids = self.job_model.search(self.cr, self.uid,
                                           [('uuid', '=', test_job.uuid)])
        self.assertEqual(len(stored_ids), 1)
        stored = self.job_model.browse(self.cr, self.uid, stored_ids[0])
        self.assertEquals(stored.channel, 'root')

        channel = self.channel_model.create(self.cr, self.uid, {
            'name': 'sub',
            'parent_id': self.root_channel,
        })
        job_func.refresh()
        self.function_model.write(self.cr, self.uid, job_func.id,
                                  {'channel_id': channel})

        test_job = Job(func=task_a)
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        stored_ids = self.job_model.search(self.cr, self.uid,
                                           [('uuid', '=', test_job.uuid)])
        self.assertEqual(len(stored_ids), 1)
        stored = self.job_model.browse(self.cr, self.uid, stored_ids[0])
        self.assertEquals(stored.channel, 'root.sub')
Beispiel #3
0
 def test_store(self):
     test_job = Job(func=task_a)
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     stored = self.queue_job.search(self.cr, self.uid,
                                    [('uuid', '=', test_job.uuid)])
     self.assertEqual(len(stored), 1)
Beispiel #4
0
    def _cancel_jobs(self, cr, uid, context=None):
        """Find moves where the mark has been removed and cancel the jobs.

        For the moves that are posted already it's too late: we skip them.

        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)
        storage = OpenERPJobStorage(session)

        move_ids = self.search(cr,
                               uid, [
                                   ('to_post', '=', False),
                                   ('post_job_uuid', '!=', False),
                                   ('state', '=', 'draft'),
                               ],
                               context=context)

        for move in self.browse(cr, uid, move_ids, context=context):
            job_rec = storage.load(move.post_job_uuid)
            if job_rec.state in (u'pending', u'enqueued'):
                job_rec.set_done(result=_(
                    u'Task set to Done because the user unmarked the move'))
                storage.store(job_rec)
Beispiel #5
0
 def _create_job(self):
     test_job = Job(func=task_a)
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     stored = storage.db_record_from_uuid(test_job.uuid)
     self.assertEqual(len(stored), 1)
     return stored
Beispiel #6
0
 def _create_job(self):
     test_job = Job(func=task_a)
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     stored = self.queue_job.search([('uuid', '=', test_job.uuid)])
     self.assertEqual(len(stored), 1)
     return stored
Beispiel #7
0
 def setUp(self):
     super(TestBaseImportConnector, self).setUp()
     self.import_obj = self.registry['base_import.import']
     self.move_obj = self.registry['account.move']
     self.job_obj = self.registry['queue.job']
     self.session = ConnectorSession(self.cr, self.uid)
     self.storage = OpenERPJobStorage(self.session)
Beispiel #8
0
 def _create_job(self, func, *args):
     cr, uid = self.cr, self.uid
     job = Job(func=func, args=args)
     storage = OpenERPJobStorage(self.session)
     storage.store(job)
     stored_ids = self.QueueJob.search(self.cr, self.uid,
                                       [('uuid', '=', job.uuid)])
     self.assertEqual(len(stored_ids), 1)
     return self.QueueJob.browse(cr, uid, stored_ids[0])
Beispiel #9
0
    def test_read(self):
        eta = datetime.now() + timedelta(hours=5)
        test_job = Job(func=dummy_task_args,
                       model_name='res.users',
                       args=('o', 'k'),
                       kwargs={'c': '!'},
                       priority=15,
                       eta=eta,
                       description="My description")
        test_job.user_id = 1
        test_job.company_id = self.env.ref("base.main_company").id
        storage = OpenERPJobStorage(self.session)
        storage.store(test_job)
        job_read = storage.load(test_job.uuid)
        self.assertEqual(test_job.uuid, job_read.uuid)
        self.assertEqual(test_job.model_name, job_read.model_name)
        self.assertEqual(test_job.func, job_read.func)
        self.assertEqual(test_job.args, job_read.args)
        self.assertEqual(test_job.kwargs, job_read.kwargs)
        self.assertEqual(test_job.func_name, job_read.func_name)
        self.assertEqual(test_job.func_string, job_read.func_string)
        self.assertEqual(test_job.description, job_read.description)
        self.assertEqual(test_job.state, job_read.state)
        self.assertEqual(test_job.priority, job_read.priority)
        self.assertEqual(test_job.exc_info, job_read.exc_info)
        self.assertEqual(test_job.result, job_read.result)
        self.assertEqual(test_job.user_id, job_read.user_id)
        self.assertEqual(test_job.company_id, job_read.company_id)
        delta = timedelta(seconds=1)  # DB does not keep milliseconds
        self.assertAlmostEqual(test_job.date_created,
                               job_read.date_created,
                               delta=delta)
        self.assertAlmostEqual(test_job.date_started,
                               job_read.date_started,
                               delta=delta)
        self.assertAlmostEqual(test_job.date_enqueued,
                               job_read.date_enqueued,
                               delta=delta)
        self.assertAlmostEqual(test_job.date_done,
                               job_read.date_done,
                               delta=delta)
        self.assertAlmostEqual(test_job.eta, job_read.eta, delta=delta)

        test_date = datetime(2015, 3, 15, 21, 7, 0)
        job_read.date_enqueued = test_date
        job_read.date_started = test_date
        job_read.date_done = test_date
        job_read.canceled = True
        storage.store(job_read)

        job_read = storage.load(test_job.uuid)
        self.assertAlmostEqual(job_read.date_started, test_date, delta=delta)
        self.assertAlmostEqual(job_read.date_enqueued, test_date, delta=delta)
        self.assertAlmostEqual(job_read.date_done, test_date, delta=delta)
        self.assertEqual(job_read.canceled, True)
Beispiel #10
0
 def test_job_unlinked(self):
     test_job = Job(func=dummy_task_args,
                    model_name='res.users',
                    args=('o', 'k'),
                    kwargs={'c': '!'})
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     stored = self.queue_job.search([('uuid', '=', test_job.uuid)])
     stored.unlink()
     with self.assertRaises(NoSuchJobError):
         storage.load(test_job.uuid)
Beispiel #11
0
 def open_related_action(self):
     """ Open the related action associated to the job """
     self.ensure_one()
     session = ConnectorSession(self.env.cr,
                                self.env.uid,
                                context=self.env.context)
     storage = OpenERPJobStorage(session)
     job = storage.load(self.uuid)
     action = job.related_action(session)
     if action is None:
         raise exceptions.Warning(_('No action available for this job'))
     return action
Beispiel #12
0
 def test_job_worker(self):
     worker = self.env['queue.worker'].create(
         {'uuid': '57569b99-c2c1-47b6-aad1-72f953c92c87'})
     test_job = Job(func=dummy_task_args,
                    model_name='res.users',
                    args=('o', 'k'),
                    kwargs={'c': '!'})
     test_job.worker_uuid = worker.uuid
     storage = OpenERPJobStorage(self.session)
     self.assertEqual(storage._worker_id(worker.uuid), worker.id)
     storage.store(test_job)
     job_read = storage.load(test_job.uuid)
     self.assertEqual(job_read.worker_uuid, worker.uuid)
Beispiel #13
0
 def _change_job_state(self, state, result=None):
     """ Change the state of the `Job` object itself so it
     will change the other fields (date, result, ...)
     """
     session = ConnectorSession(self.env.cr,
                                self.env.uid,
                                context=self.env.context)
     storage = OpenERPJobStorage(session)
     for job in self:
         job = storage.load(job.uuid)
         if state == DONE:
             job.set_done(result=result)
         elif state == PENDING:
             job.set_pending(result=result)
         else:
             raise ValueError('State not supported: %s' % state)
         storage.store(job)
Beispiel #14
0
 def test_unicode(self):
     test_job = Job(func=dummy_task_args,
                    model_name='res.users',
                    args=(u'öô¿‽', u'ñě'),
                    kwargs={'c': u'ßø'},
                    priority=15,
                    description=u"My dé^Wdescription")
     test_job.user_id = 1
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     job_read = storage.load(test_job.uuid)
     self.assertEqual(test_job.args, job_read.args)
     self.assertEqual(job_read.args, ('res.users', u'öô¿‽', u'ñě'))
     self.assertEqual(test_job.kwargs, job_read.kwargs)
     self.assertEqual(job_read.kwargs, {'c': u'ßø'})
     self.assertEqual(test_job.description, job_read.description)
     self.assertEqual(job_read.description, u"My dé^Wdescription")
 def test_accented_bytestring(self):
     job = Job(func=dummy_task_args,
               model_name='res.users',
               args=('öô¿‽', 'ñě'),
               kwargs={'c': 'ßø'},
               priority=15,
               description="My dé^Wdescription")
     job.user_id = 1
     storage = OpenERPJobStorage(self.session)
     storage.store(job)
     job_read = storage.load(job.uuid)
     self.assertEqual(job.args, job_read.args)
     self.assertEqual(job_read.args, ('res.users', 'öô¿‽', 'ñě'))
     self.assertEqual(job.kwargs, job_read.kwargs)
     self.assertEqual(job_read.kwargs, {'c': 'ßø'})
     # the job's description has been created as bytestring but is
     # decoded to utf8 by the ORM so make them comparable
     self.assertEqual(job.description, job_read.description.encode('utf8'))
     self.assertEqual(job_read.description,
                      "My dé^Wdescription".decode('utf8'))
Beispiel #16
0
 def test_job_identity_key_no_duplicate(self):
     """ If a job with same identity key in queue do not add a new one """
     storage = OpenERPJobStorage(self.session)
     test_job_uuid = storage.enqueue(func=task_a,
                                     args=(1, "a"),
                                     identity_key=identity_exact)
     test_job = storage.load(test_job_uuid)
     stored = self.queue_job.search([('identity_key', '=',
                                      test_job.identity_key)])
     self.assertTrue(stored)
     self.assertEqual(1, len(stored))
     self.assertEqual(stored.uuid, test_job.uuid)
     test_job_uuid2 = storage.enqueue(func=task_a,
                                      args=(1, "a"),
                                      identity_key=identity_exact)
     self.assertEqual(test_job_uuid2, test_job.uuid)
     stored = self.queue_job.search([('identity_key', '=',
                                      test_job.identity_key)])
     self.assertTrue(stored)
     self.assertEqual(1, len(stored))
Beispiel #17
0
    def _cancel_jobs(self, cr, uid, context=None):
        """Find payment.orders where the mark has been removed and cancel the jobs.
        """

        if context is None:
            context = {}

        session = ConnectorSession(cr, uid, context=context)
        storage = OpenERPJobStorage(session)

        paymentorder_ids = self.search(cr, uid, [
            ('to_process', '=', False),
            ('post_job_uuid', '!=', False)
        ], context=context)

        for paymentorder in self.browse(cr, uid, paymentorder_ids, context=context):
            job_rec = storage.load(paymentorder.post_job_uuid)
            if job_rec.state in (u'pending', u'enqueued'):
                job_rec.set_done(result=_(
                    u'Task set to Done because the user unmarked the payment order'
                ))
                storage.store(job_rec)
Beispiel #18
0
 def test_read(self):
     eta = datetime.now() + timedelta(hours=5)
     job = Job(func=dummy_task_args,
               model_name='res.users',
               args=('o', 'k'),
               kwargs={'c': '!'},
               priority=15,
               eta=eta,
               description="My description")
     job.user_id = 1
     storage = OpenERPJobStorage(self.session)
     storage.store(job)
     job_read = storage.load(job.uuid)
     self.assertEqual(job.uuid, job_read.uuid)
     self.assertEqual(job.model_name, job_read.model_name)
     self.assertEqual(job.func, job_read.func)
     self.assertEqual(job.args, job_read.args)
     self.assertEqual(job.kwargs, job_read.kwargs)
     self.assertEqual(job.func_name, job_read.func_name)
     self.assertEqual(job.func_string, job_read.func_string)
     self.assertEqual(job.description, job_read.description)
     self.assertEqual(job.state, job_read.state)
     self.assertEqual(job.priority, job_read.priority)
     self.assertEqual(job.exc_info, job_read.exc_info)
     self.assertEqual(job.result, job_read.result)
     self.assertEqual(job.user_id, job_read.user_id)
     delta = timedelta(seconds=1)  # DB does not keep milliseconds
     self.assertAlmostEqual(job.date_created, job_read.date_created,
                            delta=delta)
     self.assertAlmostEqual(job.date_started, job_read.date_started,
                            delta=delta)
     self.assertAlmostEqual(job.date_enqueued, job_read.date_enqueued,
                            delta=delta)
     self.assertAlmostEqual(job.date_done, job_read.date_done,
                            delta=delta)
     self.assertAlmostEqual(job.eta, job_read.eta,
                            delta=delta)
Beispiel #19
0
 def _create_job(self):
     test_job = Job(func=sample_job)
     storage = OpenERPJobStorage(self.session)
     storage.store(test_job)
     stored = storage.db_record_from_uuid(test_job.uuid)
     return stored