~jfb-tempo-consulting/unifield-wm/sync-env-py3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python2

#Load config file
import config

#Load OpenERP Client Library
import openerplib

#Load tests procedures
import unittest
#from tests import *
from tests.openerplib import db

from scripts.common import Synchro, HQ, Coordo, Project, Project2

try:
    import ipdb as pdb
except:
    import pdb

skipCreation = False
skipModules = False
skipConfig = False
skipGroups = False
skipModuleTest = False
skipRegister = False
skipSync = False
skipUniUser = False

class db_creation(object):

    buggy_models = ('sale.price.setup',)

    base_wizards = {
        'res.config.view' : {
            'name' : "auto_init",
            'view' : 'extended',
        },
        'sale.price.setup' : {
            'sale_price' : 0.10,
        },
        'msf_instance.setup' : {
            'button' : 'action_skip',
        },
        'account.installer' : {
            'charts' : 'msf_chart_of_account',
        },
        'stock.location.configuration.wizard' : {
            'location_type' : 'internal',
            'location_usage' : 'stock',
            'location_name' : 'Test Location',
            'button' : 'action_stop',
        },
    }

    db = None

    def setUp(self):
        if self.db is None:
            raise Exception("Bad use of class")

    @unittest.skipIf(skipCreation, "Creation desactivated")
    def test_00_drop(self):
        self.db.connect('admin')
        self.db.drop()

    @unittest.skipIf(skipCreation, "Creation desactivated")
    def test_01_create_db(self):
        self.db.connect('admin')
        self.db.create_db(config.admin_password)
        self.db.wait()

    @unittest.skipIf(skipModules, "Modules installation desactivated")
    def test_02_base_install(self):
        self.db.connect('admin')
        self.db.module('msf_profile').install().do()
        self.db.module('sync_so').install().do()

    @unittest.skipIf(skipModules, "Modules installation desactivated")
    def test_03_specific_install(self):
        self.db.connect('admin')
        if self.db is Synchro:
            self.db.module('sync_server_test').install().do()
            self.db.module('update_server').install().do()
        else:
            self.db.module('update_client').install()
            self.db.module('sync_client_web').install().do()
            Synchro.connect('admin')
            Synchro.user(self.db.__name__).add(self.db.__name__).addGroups('Sync / User')
            # Force reconnect to set connection manager
            self.db.connect('admin', reconnect=True)

    @unittest.skipIf(skipUniUser, "Unifield user creation desactivated")
    def test_04_unifield_user_creation(self):
        self.db.connect('admin')
        self.db.user('unifield').add('admin').addGroups('Sync / User', 'Purchase / User')

    def configure(self):
        model = 'base.setup.installer'
        while model != 'ir.ui.menu':
            try:
                if model in self.buggy_models or (model == 'account.installer' and self.db not in (Synchro, HQ)):
                    proxy = self.db.get(model)
                    answer = proxy.action_skip([])
                elif model == 'base.setup.config':
                    answer = self.db.wizard(model, data).config()
                else:
                    data = self.base_wizards.get(model, {})
                    button = data.pop('button', 'action_next')
                    answer = getattr(self.db.wizard(model, data), button)()
                model = answer.get('res_model', None)
            except:
                print "DEBUG: db=%s, model=%s" % (self.db.__name__, model)
                raise

    def sync(self, db=None):
        if db is None: db = self.db
        if not db.get('sync.client.entity').sync():
            monitor = db.get('sync.monitor')
            ids = monitor.search([], 0, 1, '"end" desc')
            self.fail('Synchronization process of database "%s" failed!\n%s' % (db.db_name,monitor.read(ids, ['error'])[0]['error']))
 
class synchro_creation(db_creation, unittest.TestCase):
    db = Synchro

    @unittest.skipIf(skipGroups, "Group creation desactivated")
    def test_10_make_groups(self):
        group = Synchro.get('sync.server.entity_group')
        group.unlink(group.search([]))
        group_type = Synchro.get('sync.server.group_type')
        group.create({
            'name' : 'Section',
            'type_id' : group_type.search([('name','=','Section')])[0],
        })
        group.create({
            'name' : 'Mission',
            'type_id' : group_type.search([('name','=','Coordination')])[0],
        })

    @unittest.skipIf(skipConfig, "Modules configuration desactivated")
    def test_20_configuration_wizards(self):
        self.db.connect('admin')
        self.configure()

    @unittest.skipIf(skipSync, "Synchronization desactivated")
    def test_30_activate_rules(self):
        self.db.connect('admin')
        Synchro.activate('sync_server.sync_rule', [])

class client_creation(db_creation):

    @unittest.skipIf(skipRegister, "Registration desactivated")
    def test_10_register_entity(self):
        if self.db is Synchro: return
        self.db.connect('admin')
        Synchro.connect('admin')
        wizard = self.db.wizard('sync.client.register_entity', {'email':config.default_email})
        # Fetch instances
        wizard.next()
        # Group state
        wizard.group_state()
        # Register instance
        wizard.validate()
        # Search entity record, server side
        entities = Synchro.get('sync.server.entity')
        ids = entities.search([('name','=',self.db.__name__)])
        if not len(ids) == 1: raise Exception, "Cannot find validation request for entity %s!" % self.db.__name__
        # Set parent
        if self.db is not HQ:
            if self.db is Coordo:
                parents = entities.search([('name','=','HQ')])
            elif self.db in (Project, Project2):
                parents = entities.search([('name','=','Coordo')])
            else:
                raise NotImplementedError('Cannot identify database %s' % self.db.__name__)
            if not parents:
                raise Exception('Cannot find parent entity for %s!' % self.db.__name__)
            entities.write(ids, {'parent_id':parents[0]})
        # Server accept validation
        entities.validate_action(ids)
        # Add entity to groups
        group = Synchro.get('sync.server.entity_group')
        group.write(group.search([('name','=','Section')]), {
            'entity_ids' : [(4,ids[0])],
        })
        group.write(group.search([('name','=','Mission')]), {
            'entity_ids' : [(4,ids[0])],
        })

    @unittest.skipIf(skipSync, "Synchronization desactivated")
    def test_40_synchronize(self):
        self.db.connect('admin')
        self.sync()

class hq_creation(client_creation, unittest.TestCase):
    db = HQ

    @unittest.skipIf(skipModuleTest, "client_test installation desactivated")
    def test_20_install_test_client(self):
        self.db.connect('admin')
        self.db.module('sync_client_test').install().do()

    @unittest.skipIf(skipConfig, "Modules configuration desactivated")
    def test_30_configuration_wizards(self):
        self.db.connect('admin')
        self.configure()

class coordo_creation(client_creation, unittest.TestCase):
    db = Coordo

    @unittest.skipIf(skipConfig, "Modules configuration desactivated")
    def test_50_configuration_wizards(self):
        self.db.connect('admin')
        self.configure()

class project_creation(client_creation, unittest.TestCase):
    db = Project

    @unittest.skipIf(skipConfig, "Modules configuration desactivated")
    def test_50_configuration_wizards(self):
        self.db.connect('admin')
        self.configure()

class project2_creation(client_creation, unittest.TestCase):
    db = Project2

    @unittest.skipIf(skipConfig, "Modules configuration desactivated")
    def test_50_configuration_wizards(self):
        self.db.connect('admin')
        self.configure()

test_cases = (synchro_creation, hq_creation, coordo_creation, project_creation, project2_creation)
#test_cases = (synchro_creation,)
#test_cases = (hq_creation,)
#test_cases = (coordo_creation,)
#test_cases = (synchro_creation, hq_creation, coordo_creation,)
#test_cases = (project_creation,project2_creation,)
#test_cases = (project2_creation,)

def load_tests(loader, tests, pattern):
    suite = unittest.TestSuite()
    for test_class in test_cases:
        tests = loader.loadTestsFromTestCase(test_class)
        suite.addTests(tests)
    return suite

if __name__ == '__main__':
    unittest.main(failfast=True, verbosity=2)