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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2010 Nexedi SA and Contributors. All Rights Reserved.
# Julien Muchembled <jm@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################
import unittest
import transaction
from Products.ERP5Legacy.tests import testLegacySimulationPackingList
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import Sequence
class TestDivergenceTester(testLegacySimulationPackingList.TestPackingListMixin,
ERP5TypeTestCase):
"""
Check isDivergent method on movement, delivery
"""
run_all_test = 1
quiet = 0
def getTitle(self):
return "Divergence Tester"
def getDeliveringRule(self):
return self.getRule(reference='default_delivering_rule',
validation_state="validated")
def afterSetUp(self):
"""
Remove all divergence testers from order_rule.
"""
rule = self.getDeliveringRule()
tester_list = rule.contentValues(
portal_type=rule.getPortalDivergenceTesterTypeList())
rule.deleteContent([x.getId() for x in tester_list])
transaction.commit()
self.tic()
def bootstrapSite(self):
"""
Manager has to create an administrator user first.
"""
self.logMessage("Bootstrap the site by creating required " \
"order, simulation, ...")
# Create a clean packing list
sequence_string = ' \
stepCreateOrganisation1 \
stepCreateOrganisation2 \
stepCreateOrganisation3 \
stepCreateOrder \
stepSetOrderProfile \
stepCreateNotVariatedResource \
stepTic \
stepCreateOrderLine \
stepSetOrderLineResource \
stepSetOrderLineDefaultValues \
stepOrderOrder \
stepTic \
stepConfirmOrder \
stepTic \
'
sequence = Sequence(self)
sequence(sequence_string, quiet=self.quiet)
self.logMessage("Bootstrap finished")
def setUpOnce(self, quiet=1, run=run_all_test):
"""
Create an order and generate a packing list from it.
This has to be called only once.
"""
self.validateRules()
transaction.commit()
self.tic(verbose=1)
self.bootstrapSite()
def stepGetPackingList(self, sequence=None, sequence_list=None, **kw):
"""
Set the packing list in the sequence
"""
sql_result = self.getPortal().portal_catalog(
portal_type=self.packing_list_portal_type)
self.assertEquals(1, len(sql_result))
packing_list = sql_result[0].getObject()
# XXX Hardcoded id
movement=packing_list['1']
rule = self.getDeliveringRule()
sequence.edit(
packing_list=packing_list,
movement=movement,
rule=rule,
sim_mvt=movement.getDeliveryRelatedValueList()[0])
def stepCheckPackingListIsDivergent(self, sequence=None,
sequence_list=None, **kw):
"""
Test if packing list is divergent
"""
packing_list = sequence.get('packing_list')
self.assertTrue(packing_list.isDivergent())
def stepSetNewQuantity(self, sequence=None,
sequence_list=None, **kw):
"""
Modify the quantity of the delivery movement
"""
packing_list = sequence.get('packing_list')
movement = sequence.get('movement')
movement.setQuantity(movement.getQuantity()+1234)
def stepSetPreviousQuantity(self, sequence=None,
sequence_list=None, **kw):
"""
Reset the quantity of the delivery movement
"""
sim_mvt = sequence.get('sim_mvt')
movement = sequence.get('movement')
movement.setQuantity(sim_mvt.getQuantity())
def stepSetPreviousQuantityWithEpsilon(self, sequence=None,
sequence_list=None, **kw):
sim_mvt = sequence.get('sim_mvt')
movement = sequence.get('movement')
prevision = sim_mvt.getQuantity()
decision = prevision * (1 + 1e-15)
self.assertNotEqual(prevision, decision)
movement.setQuantity(decision)
def stepAddQuantityDivergenceTester(self, sequence=None,
sequence_list=None, **kw):
"""
Add a quantity divergence tester in the rule
"""
rule = sequence.get('rule')
rule.newContent(portal_type='Quantity Divergence Tester')
def test_01_QuantityDivergenceTester(self, quiet=quiet, run=run_all_test):
"""
Test the quantity divergence tester
"""
if not run: return
sequence_string = """
GetPackingList
CheckPackingListIsNotDivergent
SetNewQuantity
CheckPackingListIsNotDivergent
AddQuantityDivergenceTester
CheckPackingListIsDivergent
SetPreviousQuantity
CheckPackingListIsNotDivergent
SetPreviousQuantityWithEpsilon
CheckPackingListIsNotDivergent
Tic
"""
sequence = Sequence(self)
sequence(sequence_string, quiet=self.quiet)
def stepSetNewSource(self, sequence=None,
sequence_list=None, **kw):
"""
Modify the source of the delivery
"""
packing_list = sequence.get('packing_list')
packing_list.setSource(None)
def stepAddCategoryDivergenceTester(self, sequence=None,
sequence_list=None, **kw):
"""
Add a category divergence tester in the rule
"""
rule = sequence.get('rule')
tester = rule.newContent(portal_type='Category Divergence Tester')
sequence.edit(tester=tester)
def stepConfigureCategoryDivergenceTesterForSource(self, sequence=None,
sequence_list=None, **kw):
"""
Add a category divergence tester in the rule
"""
tester = sequence.get('tester')
tester.setTestedPropertyList(['source | Source'])
def stepSetPreviousSource(self, sequence=None,
sequence_list=None, **kw):
"""
Reset the quantity of the delivery
"""
sim_mvt = sequence.get('sim_mvt')
packing_list = sequence.get('packing_list')
packing_list.setSource(sim_mvt.getSource())
def test_02_CategoryDivergenceTester(self, quiet=quiet, run=run_all_test):
"""
Test the category divergence tester
"""
if not run: return
sequence_string = """
GetPackingList
CheckPackingListIsNotDivergent
SetNewSource
CheckPackingListIsNotDivergent
AddCategoryDivergenceTester
CheckPackingListIsNotDivergent
ConfigureCategoryDivergenceTesterForSource
CheckPackingListIsDivergent
SetPreviousSource
CheckPackingListIsNotDivergent
Tic
"""
sequence = Sequence(self)
sequence(sequence_string, quiet=self.quiet)
def stepSetNewStartDate(self, sequence=None,
sequence_list=None, **kw):
"""
Modify the source of the delivery
"""
packing_list = sequence.get('packing_list')
packing_list.setStartDate(packing_list.getStartDate()+10)
def stepAddPropertyDivergenceTester(self, sequence=None,
sequence_list=None, **kw):
"""
Add a property divergence tester in the rule
"""
rule = sequence.get('rule')
tester = rule.newContent(portal_type='Property Divergence Tester')
sequence.edit(tester=tester)
def stepConfigurePropertyDivergenceTesterForStartDate(self, sequence=None,
sequence_list=None, **kw):
"""
Add a property divergence tester in the rule
"""
tester = sequence.get('tester')
tester.setTestedPropertyList(['start_date | Start Date'])
def stepSetPreviousStartDate(self, sequence=None,
sequence_list=None, **kw):
"""
Reset the quantity of the delivery
"""
sim_mvt = sequence.get('sim_mvt')
packing_list = sequence.get('packing_list')
packing_list.setStartDate(sim_mvt.getStartDate())
def test_03_PropertyDivergenceTester(self, quiet=quiet, run=run_all_test):
"""
Test the property divergence tester
"""
if not run: return
sequence_string = """
GetPackingList
CheckPackingListIsNotDivergent
SetNewStartDate
CheckPackingListIsNotDivergent
AddPropertyDivergenceTester
CheckPackingListIsNotDivergent
ConfigurePropertyDivergenceTesterForStartDate
CheckPackingListIsDivergent
SetPreviousStartDate
CheckPackingListIsNotDivergent
Tic
"""
sequence = Sequence(self)
sequence(sequence_string, quiet=self.quiet)
def stepSetNewAggregate(self, sequence=None,
sequence_list=None, **kw):
"""
Modify the aggregate of the delivery movement
"""
movement = sequence.get('movement')
# Set an aggregate value which does not exist
# but it should not be a problem for testing the divergence
movement.setAggregate('a_great_module/a_random_id')
def stepConfigureCategoryDivergenceTesterForAggregate(self, sequence=None,
sequence_list=None, **kw):
"""
Add a category divergence tester in the rule
"""
tester = sequence.get('tester')
tester.setTestedPropertyList(['aggregate | Aggregate'])
def stepSetPreviousAggregate(self, sequence=None,
sequence_list=None, **kw):
"""
Reset the quantity of the delivery
"""
movement = sequence.get('movement')
movement.setAggregate(None)
def test_04_CategoryDivergenceTester(self, quiet=quiet, run=run_all_test):
"""
Test the category divergence tester
"""
if not run: return
sequence_string = """
GetPackingList
CheckPackingListIsNotDivergent
SetNewAggregate
CheckPackingListIsNotDivergent
AddCategoryDivergenceTester
CheckPackingListIsNotDivergent
ConfigureCategoryDivergenceTesterForAggregate
CheckPackingListIsDivergent
SetPreviousAggregate
CheckPackingListIsNotDivergent
Tic
"""
sequence = Sequence(self)
sequence(sequence_string, quiet=self.quiet)
def test_QuantityDivergenceTesterCompareMethod(self):
rule = self.portal.portal_rules.newContent(portal_type='Delivery Rule')
divergence_tester = rule.newContent(portal_type='Quantity Divergence Tester')
self.assertFalse(divergence_tester.isDecimalAlignmentEnabled())
self.assertFalse(divergence_tester.compare(3.0, 3.001))
self.assertTrue(divergence_tester.compare(3.0, 3.0))
divergence_tester.setDecimalAlignmentEnabled(True)
divergence_tester.setDecimalRoundingOption('ROUND_DOWN')
divergence_tester.setDecimalExponent('0.01')
self.assertTrue(divergence_tester.compare(3.0, 3.001))
self.assertTrue(divergence_tester.compare(3.0, 3.0))
divergence_tester.setDecimalExponent('0.001')
self.assertFalse(divergence_tester.compare(3.0, 3.001))
divergence_tester.setDecimalRoundingOption('ROUND_UP')
divergence_tester.setDecimalExponent('0.01')
self.assertFalse(divergence_tester.compare(3.0, 3.001))
divergence_tester.setDecimalRoundingOption('ROUND_HALF_UP')
divergence_tester.setDecimalExponent('0.01')
self.assertTrue(divergence_tester.compare(3.0, 3.001))
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDivergenceTester))
return suite