testRule.py 25.7 KB
Newer Older
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
##############################################################################
#
# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability 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
# garantees 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest

30 31
import transaction

32 33
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript
34
from Products.ERP5.tests.testOrder import TestOrderMixin
35 36 37 38 39 40

class TestRuleMixin(TestOrderMixin):
  """
  Test basic rule behaviours
  """

Julien Muchembled's avatar
Julien Muchembled committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  def createRule(self, base_reference, version, **kw):
    rule = super(TestRuleMixin, self).getRule(reference=base_reference,
                                              version='<testRule.')
    assert rule.getValidationState() == 'draft'
    parent = rule.getParentValue()
    rule, = parent.manage_pasteObjects(
      parent.manage_copyObjects(ids=rule.getId()))
    rule = parent[rule['new_id']]
    rule._edit(version='testRule.' + version, **kw)
    return rule

  def getRule(self, reference):
    rule = super(TestRuleMixin, self).getRule(reference=reference)
    assert rule.getVersion().startswith('testRule.')
    return rule

57 58
  def afterSetUp(self):
    # delete rules
Julien Muchembled's avatar
Julien Muchembled committed
59 60 61
    rule_tool = self.portal.portal_rules
    rule_tool.manage_delObjects(ids=[x.getId() for x in rule_tool.objectValues()
                                     if x.getVersion().startswith('testRule.')])
62
    # recreate rules
Julien Muchembled's avatar
Julien Muchembled committed
63 64 65 66
    self.createRule('default_order_rule', '1')
    self.createRule('default_delivery_rule', '1')
    transaction.commit()
    self.tic()
67
    # create packing list if necessary
Julien Muchembled's avatar
Julien Muchembled committed
68
    pl_module = self.portal.getDefaultModule(self.packing_list_portal_type)
69
    if pl_module.objectCount() == 0:
70 71
      # at least one default_delivery_rule should be validated here to
      # confirm Sale Packing List
Julien Muchembled's avatar
Julien Muchembled committed
72
      delivery_rule = self.getRule('default_delivery_rule')
73
      delivery_rule.validate()
74
      self.pl = self.createPackingList()
75
      delivery_rule.invalidate()
76
    else:
Julien Muchembled's avatar
Julien Muchembled committed
77
      self.pl = pl_module.objectValues()[0]
78 79 80 81
    #delete applied_rule
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
    # commit
82
    transaction.commit()
83 84 85 86
    self.tic()


  def beforeTearDown(self):
Julien Muchembled's avatar
Julien Muchembled committed
87 88
    module = self.getSimulationTool()
    module.manage_delObjects(list(module.objectIds()))
89
    transaction.commit()
90 91 92 93 94 95 96
    self.tic()

  def getTitle(self):
    return "Rule"

  def getBusinessTemplateList(self):
    return TestOrderMixin.getBusinessTemplateList(self) + ('erp5_accounting',
97
                'erp5_invoicing',)
98 99 100 101 102 103 104 105 106 107

  def createPackingList(self):
    """
    create a packing list, to allow testing
    """
    self.getCategoryTool()['group'].newContent(portal_type='Category', id='a')
    self.getCategoryTool()['group'].newContent(portal_type='Category', id='b')
    pl_module = self.getPortal().getDefaultModule(
        self.packing_list_portal_type)
    pl = pl_module.newContent(portal_type=self.packing_list_portal_type,
Julien Muchembled's avatar
Julien Muchembled committed
108
        specialise=self.business_process,
109
        source_section='group/a', destination_section='group/b')
110 111
    pl.newContent(portal_type=self.packing_list_line_portal_type, id='line',
                  quantity=1)
112
    pl.setStartDate("2007-07-01")
113
    transaction.commit()
114 115
    self.tic()
    pl.confirm()
116
    transaction.commit()
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    self.tic()
    return pl


class TestRule(TestRuleMixin, ERP5TypeTestCase) :

  run_all_test = 1
  quiet = 0

  def test_01_ValidatedRuleWithNoScript(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated, but has no script it will not apply
    """
    if not run: return

Julien Muchembled's avatar
Julien Muchembled committed
132
    delivery_rule = self.getRule('default_delivery_rule')
133
    delivery_rule.validate()
134
    transaction.commit()
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_02_WrongTestMethod(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule's test method returns False, it will not apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'wrong_script', 'rule',
        'return False')
Julien Muchembled's avatar
Julien Muchembled committed
150
    delivery_rule = self.getRule('default_delivery_rule')
151 152
    delivery_rule.setTestMethodId('wrong_script')
    delivery_rule.validate()
153
    transaction.commit()
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_03_GoodTestMethod(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule's test method returns True, it will apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
Julien Muchembled's avatar
Julien Muchembled committed
169
    delivery_rule = self.getRule('default_delivery_rule')
170 171
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.validate()
172
    transaction.commit()
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 1)

  def test_04_NotValidatedRule(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is not validated, it will not apply, even if it has
    a good script
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
Julien Muchembled's avatar
Julien Muchembled committed
189
    delivery_rule = self.getRule('default_delivery_rule')
190 191 192
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.validate()
    delivery_rule.invalidate()
193
    transaction.commit()
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 0)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_06_WrongDateRange(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated but does not have correct date range,
    it will not apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
Julien Muchembled's avatar
Julien Muchembled committed
210
    delivery_rule = self.getRule('default_delivery_rule')
211 212 213 214
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.setStartDateRangeMin('2007-06-01')
    delivery_rule.setStartDateRangeMax('2007-06-04')
    delivery_rule.validate()
215
    transaction.commit()
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 0)

  def test_07_GoodDateRange(self, quiet=quiet, run=run_all_test):
    """
    test that when a rule is validated and has a correct date range, it will
    apply
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'good_script', 'rule',
        'return True')
Julien Muchembled's avatar
Julien Muchembled committed
232
    delivery_rule = self.getRule('default_delivery_rule')
233 234 235 236
    delivery_rule.setTestMethodId('good_script')
    delivery_rule.setStartDateRangeMin('2007-06-01')
    delivery_rule.setStartDateRangeMax('2007-08-01')
    delivery_rule.validate()
237
    transaction.commit()
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    self.tic()

    self.assertEquals(self.getRuleTool().countFolder(
      validation_state="validated")[0][0], 1)
    self.assertEquals(len(self.getRuleTool().searchRuleList(self.pl)), 1)

  def test_08_updateAppliedRule(self, quiet=quiet, run=run_all_test):
    """
    test that when updateAppliedRule is called, the rule with the correct
    reference and higher version is used

    XXX as expand is triggered here, make sure rules won't be created forever
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'rule_script', 'rule',
        "return False")

    # wrong reference
Julien Muchembled's avatar
Julien Muchembled committed
258
    order_rule = self.getRule('default_order_rule')
259 260
    order_rule.setTestMethodId('rule_script')
    order_rule.validate()
Julien Muchembled's avatar
Julien Muchembled committed
261 262

    delivery_rule_1 = self.getRule('default_delivery_rule')
263 264
    delivery_rule_1.setTestMethodId('rule_script')
    delivery_rule_1.validate()
Julien Muchembled's avatar
Julien Muchembled committed
265 266 267

    delivery_rule_2 = self.createRule('default_delivery_rule', '2',
                                      test_method_id='rule_script')
268
    delivery_rule_2.validate()
269
    transaction.commit()
270 271 272 273
    self.tic()

    # delivery_rule_2 should be applied
    self.pl.updateAppliedRule('default_delivery_rule')
274
    transaction.commit()
275 276 277 278 279 280 281 282
    self.tic()
    self.assertEquals(self.pl.getCausalityRelatedValue().getSpecialise(),
        delivery_rule_2.getRelativeUrl())

    self.getSimulationTool().manage_delObjects(
        ids=[self.pl.getCausalityRelatedId()])

    # increase version of delivery_rule_1
Julien Muchembled's avatar
Julien Muchembled committed
283
    delivery_rule_1.setVersion("testRule.3")
284
    transaction.commit()
285 286 287 288
    self.tic()

    # delivery_rule_1 should be applied
    self.pl.updateAppliedRule('default_delivery_rule')
289
    transaction.commit()
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
    self.tic()
    self.assertEquals(self.pl.getCausalityRelatedValue().getSpecialise(),
        delivery_rule_1.getRelativeUrl())

  def test_09_expandTwoRules(self, quiet=quiet, run=run_all_test):
    """
    test that when expand is called on a simulation movement, if two rules
    with the same reference are found, only the one with the higher version
    will be applied.

    XXX as expand is triggered here, make sure rules won't be created forever
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

Julien Muchembled's avatar
Julien Muchembled committed
312
    delivery_rule = self.getRule('default_delivery_rule')
313
    delivery_rule.validate()
Julien Muchembled's avatar
Julien Muchembled committed
314 315 316

    invoicing_rule_1 = self.createRule('default_invoicing_rule', '1',
                                       test_method_id='invoice_rule_script')
317 318
    invoicing_rule_1.validate()

Julien Muchembled's avatar
Julien Muchembled committed
319 320
    invoicing_rule_2 = self.createRule('default_invoicing_rule', '2',
                                       test_method_id='invoice_rule_script')
321 322 323 324 325
    invoicing_rule_2.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
326
    transaction.commit()
327 328 329
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
330
    transaction.commit()
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    self.tic()

    # check that only one invoicing rule (higher version) was applied
    root_applied_rule = self.pl.getCausalityRelatedValue()
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_2.getRelativeUrl())

    # increase version of other rule, clean simulation and check again
    self.getSimulationTool().manage_delObjects(
        ids=[self.pl.getCausalityRelatedId()])
Julien Muchembled's avatar
Julien Muchembled committed
348
    invoicing_rule_1.setVersion('testRule.3')
349
    transaction.commit()
350 351 352
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
353
    transaction.commit()
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    self.tic()

    # check that only one invoicing rule (higher version) was applied
    root_applied_rule = self.pl.getCausalityRelatedValue()
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

  def test_10_expandAddsRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule didn't match previously, and does now, it should apply
371
    if no rule with the same reference is already applied.
372
    - test that it happens if no rule is already applied
373 374
    - test that nothing changes if a rule of same reference is already
      applied (no matter what the version is)
375 376 377 378 379 380 381 382 383 384 385
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

Julien Muchembled's avatar
Julien Muchembled committed
386
    delivery_rule = self.getRule('default_delivery_rule')
387
    delivery_rule.validate()
Julien Muchembled's avatar
Julien Muchembled committed
388

389
    # create rule with a wrong script
Julien Muchembled's avatar
Julien Muchembled committed
390 391
    invoicing_rule_1 = self.createRule('default_invoicing_rule', '1',
                                       test_method_id='delivery_rule_script')
392 393 394 395 396
    invoicing_rule_1.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))
397
    transaction.commit()
398 399 400
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
401
    transaction.commit()
402 403 404 405 406 407 408 409 410 411 412 413 414
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that no invoicing rule was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 0)

    # change rule script so that it matches and test again
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    root_applied_rule.expand()
415
    transaction.commit()
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # add more invoicing_rule and test that nothing is changed
    ## same reference, higher version
Julien Muchembled's avatar
Julien Muchembled committed
434 435
    invoicing_rule_n = self.createRule('default_invoicing_rule', '2',
                                       test_method_id='invoice_rule_script')
436 437
    invoicing_rule_n.validate()
    ## different reference, higher version (but version shouldn't matter here)
Julien Muchembled's avatar
Julien Muchembled committed
438 439 440
    invoicing_rule_2 = self.createRule('default_invoicing_rule', '2',
                                       reference='default_invoicing_rule_2',
                                       test_method_id='invoice_rule_script')
441
    invoicing_rule_2.validate()
442
    transaction.commit()
443 444
    self.tic()
    root_applied_rule.expand()
445
    transaction.commit()
446 447 448 449 450 451 452 453 454 455 456
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())

    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
457 458 459 460
    self.assertEquals(movement.objectCount(), 2)
    applied_rule_list = sorted(movement.objectValues(),
                          key=lambda x: x.getSpecialiseValue().getReference())
    self.assertEquals(applied_rule_list[0].getSpecialise(),
461
        invoicing_rule_1.getRelativeUrl())
462 463
    self.assertEquals(applied_rule_list[1].getSpecialise(),
        invoicing_rule_2.getRelativeUrl())
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482


  def test_11_expandRemovesRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule matched previously and does not anymore, it should be
    removed, if no child movement of this rule is delivered
    - test that it happens if no child is delivered
    - test that nothing is changed if at least one child is delivered
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

Julien Muchembled's avatar
Julien Muchembled committed
483
    delivery_rule = self.getRule('default_delivery_rule')
484
    delivery_rule.validate()
Julien Muchembled's avatar
Julien Muchembled committed
485 486 487

    invoicing_rule_1 = self.createRule('default_invoicing_rule', '1',
                                       test_method_id='invoice_rule_script')
488 489 490 491 492 493
    invoicing_rule_1.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))

494
    transaction.commit()
495 496 497
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
498
    transaction.commit()
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that the invoicing rule was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # invalidate the rule and test that it is still there
    invoicing_rule_1.invalidate()
514
    transaction.commit()
515 516 517
    self.tic()
    self.assertEquals(invoicing_rule_1.getValidationState(), 'invalidated')
    root_applied_rule.expand()
518
    transaction.commit()
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # change the test method to one that fails, and test that the rule is
    # removed
    invoicing_rule_1.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
536
    transaction.commit()
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 0)

    # change the test to one that succeeds, revalidate, expand, add a delivery
    # relation, change the test method to one that fails, expand, and test
    # that the rule is still there
    invoicing_rule_1.setTestMethodId('invoice_rule_script')
    invoicing_rule_1.validate()
552
    transaction.commit()
553 554 555
    self.tic()
    self.assertEquals(invoicing_rule_1.getValidationState(), 'validated')
    root_applied_rule.expand()
556
    transaction.commit()
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]

    sub_movement.setDeliveryValue(self.pl.line)

    invoicing_rule_1.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
576
    transaction.commit()
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]
    self.assertEquals(sub_movement.getDelivery(), self.pl.line.getRelativeUrl())

  def test_12_expandReplacesRule(self, quiet=quiet, run=run_all_test):
    """
    test that if a rule matched previously and does not anymore, and another
    rule matches now, the old rule should be replaced by the new one, if no
    child movement of this rule is delivered
    - test that it happens if no child is delivered
    - test that nothing is changed if at least one child is delivered
    """
    if not run: return

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'delivery_rule_script', 'rule',
        "return False")

    skin_folder = self.getPortal().portal_skins.custom
    skin = createZODBPythonScript(skin_folder, 'invoice_rule_script', 'rule',
        "return context.getParentValue().getSpecialiseReference() == 'default_delivery_rule'")

Julien Muchembled's avatar
Julien Muchembled committed
611
    delivery_rule = self.getRule('default_delivery_rule')
612
    delivery_rule.validate()
Julien Muchembled's avatar
Julien Muchembled committed
613 614 615

    invoicing_rule_1 = self.createRule('default_invoicing_rule', '1',
                                       test_method_id='invoice_rule_script')
616 617
    invoicing_rule_1.validate()

Julien Muchembled's avatar
Julien Muchembled committed
618 619
    invoicing_rule_2 = self.createRule('default_invoicing_rule', '2',
                                       test_method_id='invoice_rule_script')
620 621 622 623 624 625
    invoicing_rule_2.validate()

    # clear simulation
    self.getSimulationTool().manage_delObjects(
        ids=list(self.getSimulationTool().objectIds()))

626
    transaction.commit()
627 628 629
    self.tic()

    self.pl.updateAppliedRule('default_delivery_rule')
630
    transaction.commit()
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    self.tic()
    root_applied_rule = self.pl.getCausalityRelatedValue()

    # check that the invoicing rule 2 was applied
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_2.getRelativeUrl())

    # change the test method to one that fails, and test that the rule is
    # replaced by invoicing rule 1
    invoicing_rule_2.setTestMethodId('delivery_rule_script')
    root_applied_rule.expand()
648
    transaction.commit()
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())

    # change the test of invoicing rule 2 to one that succeeds, add a delivery
    # relation, expand, and test that the invoicing rule 1 is still there
    invoicing_rule_2.setTestMethodId('invoice_rule_script')
    sub_movement = applied_rule.objectValues()[0]
    sub_movement.setDeliveryValue(self.pl.line)
    root_applied_rule.expand()
668
    transaction.commit()
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
    self.tic()

    self.assertEquals(root_applied_rule.getRelativeUrl(),
        self.pl.getCausalityRelated())
    self.assertEquals(root_applied_rule.getSpecialise(),
        delivery_rule.getRelativeUrl())
    self.assertEquals(root_applied_rule.objectCount(), 1)
    movement = root_applied_rule.objectValues()[0]
    self.assertEquals(movement.objectCount(), 1)
    applied_rule = movement.objectValues()[0]
    self.assertEquals(applied_rule.getSpecialise(),
        invoicing_rule_1.getRelativeUrl())
    self.assertEquals(applied_rule.objectCount(), 1)
    sub_movement = applied_rule.objectValues()[0]
    self.assertEquals(sub_movement.getDelivery(), self.pl.line.getRelativeUrl())


def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestRule))
  return suite