~initos.com/openerp-connector-magento/7.0-import_customer_lang

« back to all changes in this revision

Viewing changes to magentoerpconnect/tests/test_related_action.py

  • Committer: Guewen Baconnier
  • Date: 2014-05-26 09:37:00 UTC
  • Revision ID: guewen.baconnier@camptocamp.com-20140526093700-o0i186du7k39724i
Implements Related Actions

A related action can be attached to a job. It is shown to the user as a button on the form view of the jobs.
When the button is used, the related action is called and must return an OpenERP "client action".

Related action in this connector:
* Open the form view of the record that is concerned by the job on export jobs
* Open a browser on Magento's admin page in order to view/edit the record on import jobs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
import mock
 
3
import unittest2
 
4
 
 
5
import openerp
 
6
import openerp.tests.common as common
 
7
from openerp.addons.connector.queue.job import (
 
8
    Job,
 
9
    OpenERPJobStorage,
 
10
    related_action)
 
11
from openerp.addons.connector.session import (
 
12
    ConnectorSession)
 
13
from .common import (mock_api,
 
14
                     mock_urlopen_image)
 
15
from .test_data import magento_base_responses
 
16
from ..unit.import_synchronizer import import_batch, import_record
 
17
from ..unit.export_synchronizer import export_record
 
18
from ..related_action import unwrap_binding, link
 
19
 
 
20
 
 
21
class test_related_action_storage(common.TransactionCase):
 
22
    """ Test related actions on stored jobs """
 
23
 
 
24
    def setUp(self):
 
25
        super(test_related_action_storage, self).setUp()
 
26
        cr, uid = self.cr, self.uid
 
27
        backend_model = self.registry('magento.backend')
 
28
        self.session = ConnectorSession(cr, uid)
 
29
        warehouse_id = self.ref('stock.warehouse0')
 
30
        backend_id = backend_model.create(
 
31
            cr,
 
32
            uid,
 
33
            {'name': 'Test Magento',
 
34
                'version': '1.7',
 
35
                'location': 'http://anyurl',
 
36
                'username': 'username',
 
37
                'warehouse_id': warehouse_id,
 
38
                'password': '42'})
 
39
        self.backend = backend_model.browse(cr, uid, backend_id)
 
40
        # import the base informations
 
41
        with mock_api(magento_base_responses):
 
42
            import_batch(self.session, 'magento.website', backend_id)
 
43
            import_batch(self.session, 'magento.store', backend_id)
 
44
            import_batch(self.session, 'magento.storeview', backend_id)
 
45
        self.MagentoProduct = self.registry('magento.product.product')
 
46
        self.QueueJob = self.registry('queue.job')
 
47
 
 
48
    def test_unwrap_binding(self):
 
49
        """ Open a related action opening an unwrapped binding """
 
50
        cr, uid = self.cr, self.uid
 
51
        product_id = self.ref('product.product_product_7')
 
52
        magento_product_id = self.MagentoProduct.create(
 
53
            cr, uid,
 
54
            {'openerp_id': product_id,
 
55
             'backend_id': self.backend.id})
 
56
        stored = self._create_job(export_record, 'magento.product.product',
 
57
                                  magento_product_id)
 
58
        expected = {
 
59
            'name': mock.ANY,
 
60
            'type': 'ir.actions.act_window',
 
61
            'view_type': 'form',
 
62
            'view_mode': 'form',
 
63
            'res_id': product_id,
 
64
            'res_model': 'product.product',
 
65
        }
 
66
        self.assertEquals(stored.open_related_action(), expected)
 
67
 
 
68
    def _create_job(self, func, *args):
 
69
        cr, uid = self.cr, self.uid
 
70
        job = Job(func=func, args=args)
 
71
        storage = OpenERPJobStorage(self.session)
 
72
        storage.store(job)
 
73
        stored_ids = self.QueueJob.search(self.cr, self.uid,
 
74
                                          [('uuid', '=', job.uuid)])
 
75
        self.assertEqual(len(stored_ids), 1)
 
76
        return self.QueueJob.browse(cr, uid, stored_ids[0])
 
77
 
 
78
    def test_link(self):
 
79
        """ Open a related action opening an url on Magento """
 
80
        self.backend.write({'admin_location': 'http://www.example.com/admin'})
 
81
        self.backend.refresh()
 
82
        stored = self._create_job(import_record, 'magento.product.product',
 
83
                                  self.backend.id, 123456)
 
84
        expected = {
 
85
            'type': 'ir.actions.act_url',
 
86
            'target': 'new',
 
87
            'url': 'http://www.example.com/admin/catalog_product/edit/id/123456',
 
88
        }
 
89
        self.assertEquals(stored.open_related_action(), expected)
 
90
 
 
91
    def test_link_no_location(self):
 
92
        """ Open a related action opening an url but admin location is not configured """
 
93
        self.backend.write({'admin_location': False})
 
94
        self.backend.refresh()
 
95
        stored = self._create_job(import_record, 'magento.product.product',
 
96
                                  self.backend.id, 123456)
 
97
        with self.assertRaises(openerp.osv.orm.except_orm):
 
98
            stored.open_related_action()