testBPMCore.py 34.6 KB
Newer Older
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
1
# -*- coding: utf-8 -*-
2 3 4
##############################################################################
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Łukasz Nowak <luke@nexedi.com>
5
#          Yusuke Muraoka <yusuke@nexedi.com>
6
#          Fabien Morin <fabien@nexedi.com>
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
#
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################

import unittest
import transaction

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime
36
from Products.ERP5Type.tests.utils import createZODBPythonScript, reindex
37
from Products.ERP5.tests.utils import newSimulationExpectedFailure
38 39

class TestBPMMixin(ERP5TypeTestCase):
40
  """Skeletons for tests which depend on BPM"""
41 42

  def getBusinessTemplateList(self):
43 44 45
    return ('erp5_base', 'erp5_pdm', 'erp5_simulation', 'erp5_trade',
            'erp5_accounting', 'erp5_invoicing', 'erp5_simplified_invoicing',
            'erp5_simulation_test')
46 47

  business_process_portal_type = 'Business Process'
48
  business_link_portal_type = 'Business Link'
49
  trade_model_path_portal_type = 'Trade Model Path'
50 51
  default_business_process = \
    'business_process_module/erp5_default_business_process'
52

53 54 55
  normal_resource_use_category_list = ['normal']
  invoicing_resource_use_category_list = ['discount', 'tax']

56 57
  def createCategoriesInCategory(self, category, category_id_list):
    for category_id in category_id_list:
58
      if not category.hasObject(category_id):
59 60
        category.newContent(category_id,
          title=category_id.replace('_', ' ').title())
61 62 63

  @reindex
  def createCategories(self):
64
    category_tool = self.portal.portal_categories
65
    self.createCategoriesInCategory(category_tool.base_amount, ['discount',
66
      'tax', 'total_tax', 'total_discount', 'total'])
67 68 69 70
    self.createCategoriesInCategory(category_tool.use,
        self.normal_resource_use_category_list + \
            self.invoicing_resource_use_category_list)
    self.createCategoriesInCategory(category_tool.trade_phase, ['default',])
71 72
    self.createCategoriesInCategory(category_tool.trade_phase.default,
        ['accounting', 'delivery', 'invoicing', 'discount', 'tax', 'payment'])
73
    self.createCategoriesInCategory(category_tool.trade_state,
74
        ['ordered', 'invoiced', 'delivered', 'taxed',
75
         'state_a', 'state_b', 'state_c', 'state_d', 'state_e'])
76 77 78 79
    self.createCategoriesInCategory(category_tool, ('tax_range', 'tax_share'))
    self.createCategoriesInCategory(category_tool.tax_range,
                                    ('0_200', '200_inf'))
    self.createCategoriesInCategory(category_tool.tax_share, 'AB')
80 81

  @reindex
82
  def createBusinessProcess(self, **kw):
83
    module = self.portal.getDefaultModule(
84
        portal_type=self.business_process_portal_type,)
85
    business_process =  module.newContent(
86
      portal_type=self.business_process_portal_type,
87
      specialise=self.default_business_process)
88
    self.business_process = business_process
89
    business_process._edit(**kw)
90 91
    self.createTradeModelPath(business_process,
      reference='default_path',
92
      trade_phase_value_list=('default/discount', 'default/tax'),
93
      trade_date='trade_phase/default/invoicing')
94 95 96 97
    # A trade model path already exist for root simulation movements
    # (Accounting Transaction Root Simulation Rule).
    # The ones we are creating are for Invoice Transaction Simulation Rule
    # so we add a test on the portal type of the input movement.
98 99
    kw = dict(business_process=business_process,
              trade_phase='default/accounting',
100
              trade_date='trade_phase/default/invoicing',
101 102
              membership_criterion_base_category='resource_use',
              criterion_property_dict={'portal_type': 'Simulation Movement'})
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
    self.createTradeModelPath(reference='acounting_tax1',
      efficiency=-1,
      source_value=self.receivable_account,
      destination_value=self.payable_account,
      membership_criterion_category='resource_use/use/tax',
      **kw)
    self.createTradeModelPath(reference='acounting_tax2',
      efficiency=1,
      source_value=self.collected_tax_account,
      destination_value=self.refundable_tax_account,
      membership_criterion_category='resource_use/use/tax',
      **kw)
    self.createTradeModelPath(reference='acounting_discount1',
      efficiency=-1,
      source_value=self.receivable_account,
      destination_value=self.payable_account,
      membership_criterion_category='resource_use/use/discount',
      **kw)
    self.createTradeModelPath(reference='acounting_discount2',
      efficiency=1,
      source_value=self.income_account,
      destination_value=self.expense_account,
      membership_criterion_category='resource_use/use/discount',
      **kw)
    self.createTradeModelPath(reference='acounting_normal1',
      efficiency=-1,
      source_value=self.receivable_account,
      destination_value=self.payable_account,
      membership_criterion_category='resource_use/use/normal',
      **kw)
    self.createTradeModelPath(reference='acounting_normal2',
      efficiency=1,
      source_value=self.income_account,
      destination_value=self.expense_account,
      membership_criterion_category='resource_use/use/normal',
      **kw)
    return business_process
140 141

  @reindex
142
  def createBusinessLink(self, business_process=None, **kw):
143
    if business_process is None:
144
      business_process = self.createBusinessProcess()
145 146 147
    business_link = business_process.newContent(
      portal_type=self.business_link_portal_type, **kw)
    return business_link
148

149 150
  def createTradeModelPath(self, business_process=None,
                           criterion_property_dict={}, **kw):
151 152
    if business_process is None:
      business_process = self.createBusinessProcess()
153
    trade_model_path = business_process.newContent(
154
      portal_type=self.trade_model_path_portal_type, **kw)
155 156 157 158 159
    if criterion_property_dict:
      trade_model_path._setCriterionPropertyList(tuple(criterion_property_dict))
      for property, identity in criterion_property_dict.iteritems():
        trade_model_path.setCriterion(property, identity)
    return trade_model_path
160

161 162 163 164 165 166
  def createMovement(self):
    # returns a movement for testing
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule')
    return applied_rule.newContent(portal_type='Simulation Movement')

167 168 169 170 171 172 173
  def createAndValidateAccount(self, account_id, account_type):
    account_module = self.portal.account_module
    account = account_module.newContent(portal_type='Account',
          title=account_id,
          account_type=account_type)
    self.assertNotEqual(None, account.getAccountTypeValue())
    account.validate()
174
    return account
175

176
  def createAndValidateAccounts(self):
177 178 179 180 181 182 183 184 185 186 187 188
    self.receivable_account = self.createAndValidateAccount('receivable',
        'asset/receivable')
    self.payable_account = self.createAndValidateAccount('payable',
        'liability/payable')
    self.income_account = self.createAndValidateAccount('income', 'income')
    self.expense_account = self.createAndValidateAccount('expense', 'expense')
    self.collected_tax_account = self.createAndValidateAccount(
        'collected_tax', 'liability/payable/collected_vat')
    self.refundable_tax_account = self.createAndValidateAccount(
        'refundable_tax',
        'asset/receivable/refundable_vat')

189
  def afterSetUp(self):
190
    self.validateRules()
191
    self.createCategories()
192
    self.createAndValidateAccounts()
193
    self.stepTic()
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
class TestBPMDummyDeliveryMovementMixin(TestBPMMixin):
  def _createDelivery(self, **kw):
    return self.folder.newContent(portal_type='Dummy Delivery', **kw)

  def _createMovement(self, delivery, **kw):
    return delivery.newContent(portal_type='Dummy Movement', **kw)

  def getBusinessTemplateList(self):
    return super(TestBPMDummyDeliveryMovementMixin, self)\
        .getBusinessTemplateList() \
        + ('erp5_dummy_movement', )

  def afterSetUp(self):
    super(TestBPMDummyDeliveryMovementMixin, self).afterSetUp()
    if not hasattr(self.portal, 'testing_folder'):
      self.portal.newContent(portal_type='Folder',
                            id='testing_folder')
    self.folder = self.portal.testing_folder
    self.stepTic()

  def beforeTearDown(self):
    super(TestBPMDummyDeliveryMovementMixin, self).beforeTearDown()
    self.portal.deleteContent(id='testing_folder')
    self.stepTic()
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
  completed_state = 'delivered'
  frozen_state = 'confirmed'

  completed_state_list = [completed_state]
  frozen_state_list = [completed_state, frozen_state]

  def _createOrderedDeliveredInvoicedBusinessProcess(self):
    # simple business process preparation
    business_process = self.createBusinessProcess()
    category_tool = self.getCategoryTool()
    ordered = category_tool.trade_state.ordered
    delivered = category_tool.trade_state.delivered
    invoiced = category_tool.trade_state.invoiced

    # path which is completed, as soon as related simulation movements are in
    # proper state
    self.order_link = self.createBusinessLink(business_process,
        successor_value = ordered,
        trade_phase='default/order',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)

    self.delivery_link = self.createBusinessLink(business_process,
        predecessor_value = ordered, successor_value = delivered,
        trade_phase='default/delivery',
        completed_state_list = self.completed_state_list,
        frozen_state_list = self.frozen_state_list)

    self.invoice_link = self.createBusinessLink(business_process,
        predecessor_value = delivered, successor_value = invoiced,
        trade_phase='default/invoicing')
    self.stepTic()

  def constructSimulationTreeAndDeliveries(self):
    """
    Construct a simple simulation tree with deliveries. This is
    not real simulation tree, we only need the structure, most
    usual properties are not there (quantities, arrow, etc)
    """
    # create order and order line to have starting point for business process
    self.order = order = self._createDelivery()
    order_line = self._createMovement(order)

    # first level rule with simulation movement
    applied_rule = self.portal.portal_simulation.newContent(
        portal_type='Applied Rule', causality_value=order)

    def setTestClassProperty(prefix, property_name, document):
      if prefix:
        property_name = "%s_%s" % (prefix, property_name)
      setattr(self, property_name, document)
      return document

    def constructSimulationTree(applied_rule, prefix=None):
      document = setTestClassProperty(prefix, 'simulation_movement',
        applied_rule.newContent(
        portal_type = 'Simulation Movement',
        delivery_value = order_line,
        causality_value = self.order_link
        ))

      # second level rule with simulation movement
      document = setTestClassProperty(prefix, 'delivery_rule',
        document.newContent(
        portal_type='Applied Rule'))
      document = setTestClassProperty(prefix, 'delivery_simulation_movement',
        document.newContent(
        portal_type='Simulation Movement',
        causality_value = self.delivery_link))

      # third level rule with simulation movement
      document = setTestClassProperty(prefix, 'invoicing_rule',
          document.newContent(
          portal_type='Applied Rule'))
      document = setTestClassProperty(prefix, 'invoicing_simulation_movement',
          document.newContent(
          portal_type='Simulation Movement',
          causality_value = self.invoice_link))

    constructSimulationTree(applied_rule)
    constructSimulationTree(applied_rule, prefix='split')
    self.stepTic()

class TestBPMImplementation(TestBPMDummyDeliveryMovementMixin):
304
  """Business Process implementation tests"""
305
  def test_BusinessProcess_getBusinessLinkValueList(self):
306 307
    business_process = self.createBusinessProcess()

308 309
    accounting_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
310 311
        trade_phase='default/accounting')

312 313
    delivery_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
314 315
        trade_phase='default/delivery')

316 317
    accounting_delivery_business_link = business_process.newContent(
        portal_type=self.business_link_portal_type,
318 319 320 321 322
        trade_phase=('default/accounting', 'default/delivery'))

    self.stepTic()

    self.assertSameSet(
323
      (accounting_business_link, accounting_delivery_business_link),
324
      business_process.getBusinessLinkValueList(trade_phase='default/accounting')
325 326 327
    )

    self.assertSameSet(
328
      (delivery_business_link, accounting_delivery_business_link),
329
      business_process.getBusinessLinkValueList(trade_phase='default/delivery')
330 331 332
    )

    self.assertSameSet(
333 334
      (accounting_delivery_business_link, delivery_business_link,
        accounting_business_link),
335
      business_process.getBusinessLinkValueList(trade_phase=('default/delivery',
336 337 338
        'default/accounting'))
    )

339
  def test_BusinessLinkStandardCategoryAccessProvider(self):
340 341 342
    source_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    source_section_node = self.portal.organisation_module.newContent(
343
                    portal_type='Organisation')
344 345 346 347 348
    business_link = self.createBusinessLink()
    business_link.setSourceValue(source_node)
    business_link.setSourceSectionValue(source_section_node)
    self.assertEquals([source_node], business_link.getSourceValueList())
    self.assertEquals([source_node.getRelativeUrl()], business_link.getSourceList())
349
    self.assertEquals(source_node.getRelativeUrl(),
350
        business_link.getSource(default='something'))
351

352 353 354 355
  def test_EmptyBusinessLinkStandardCategoryAccessProvider(self):
    business_link = self.createBusinessLink()
    self.assertEquals(None, business_link.getSourceValue())
    self.assertEquals(None, business_link.getSource())
356
    self.assertEquals('something',
357
        business_link.getSource(default='something'))
358

Sebastien Robin's avatar
Sebastien Robin committed
359
  def test_BusinessPathDynamicCategoryAccessProvider(self):
360 361 362
    source_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    source_section_node = self.portal.organisation_module.newContent(
363
                    portal_type='Organisation')
Sebastien Robin's avatar
Sebastien Robin committed
364 365
    business_path = self.createTradeModelPath()
    business_path.setSourceMethodId('TradeModelPath_getDefaultSourceList')
366 367

    context_movement = self.createMovement()
368 369
    context_movement.setSourceValue(source_node)
    context_movement.setSourceSectionValue(source_section_node)
Sebastien Robin's avatar
Sebastien Robin committed
370
    self.assertEquals(None, business_path.getSourceValue())
371
    self.assertEquals([source_node],
Sebastien Robin's avatar
Sebastien Robin committed
372
                      business_path.getSourceValueList(context=context_movement))
373
    self.assertEquals([source_node.getRelativeUrl()],
Sebastien Robin's avatar
Sebastien Robin committed
374
                      business_path.getSourceList(context=context_movement))
Łukasz Nowak's avatar
Łukasz Nowak committed
375
    self.assertEquals(source_node.getRelativeUrl(),
Sebastien Robin's avatar
Sebastien Robin committed
376
      business_path.getSource(context=context_movement, default='something'))
377

Sebastien Robin's avatar
Sebastien Robin committed
378
  def test_BusinessPathDynamicCategoryAccessProviderBusinessLinkPrecedence(self):
379 380 381 382
    movement_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
    path_node = self.portal.organisation_module.newContent(
                    portal_type='Organisation')
Sebastien Robin's avatar
Sebastien Robin committed
383 384 385
    business_path = self.createTradeModelPath()
    business_path.setSourceMethodId('TradeModelPath_getDefaultSourceList')
    business_path.setSourceValue(path_node)
386 387 388

    context_movement = self.createMovement()
    context_movement.setSourceValue(movement_node)
Sebastien Robin's avatar
Sebastien Robin committed
389
    self.assertEquals(path_node, business_path.getSourceValue())
390
    self.assertEquals(path_node,
Sebastien Robin's avatar
Sebastien Robin committed
391
                      business_path.getSourceValue(context=context_movement))
392
    self.assertEquals([path_node],
Sebastien Robin's avatar
Sebastien Robin committed
393
                      business_path.getSourceValueList(context=context_movement))
394

Sebastien Robin's avatar
Sebastien Robin committed
395 396 397
  def test_BusinessPathDynamicCategoryAccessProviderEmptyMovement(self):
    business_path = self.createTradeModelPath()
    business_path.setSourceMethodId('TradeModelPath_getDefaultSourceList')
398 399

    context_movement = self.createMovement()
Sebastien Robin's avatar
Sebastien Robin committed
400
    self.assertEquals(None, business_path.getSourceValue())
401
    self.assertEquals(None,
Sebastien Robin's avatar
Sebastien Robin committed
402
                      business_path.getSourceValue(context=context_movement))
403
    self.assertEquals(None,
Sebastien Robin's avatar
Sebastien Robin committed
404
                      business_path.getSource(context=context_movement))
405
    self.assertEquals('something',
Sebastien Robin's avatar
Sebastien Robin committed
406
      business_path.getSource(context=context_movement, default='something'))
407

408 409
  def test_BusinessState_getRemainingTradePhaseList(self):
    """
410 411
    This test case is described for what trade_phase is remaining after the
    given business link.
412 413
    """
    # define business process
414
    category_tool = self.getCategoryTool()
415
    business_process = self.createBusinessProcess()
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    business_link_order = self.createBusinessLink(business_process,
                                 title='order', id='order',
                                 trade_phase='default/order')
    business_link_deliver = self.createBusinessLink(business_process,
                                 title='deliver', id='deliver',
                                 trade_phase='default/delivery')
    business_link_invoice = self.createBusinessLink(business_process,
                                 title='invoice', id='invoice',
                                 trade_phase='default/invoicing')
    trade_state = category_tool.trade_state
    business_link_order.setSuccessorValue(trade_state.ordered)
    business_link_deliver.setPredecessorValue(trade_state.ordered)
    business_link_deliver.setSuccessorValue(trade_state.delivered)
    business_link_invoice.setPredecessorValue(trade_state.delivered)
    business_link_invoice.setSuccessorValue(trade_state.invoiced)

    trade_phase = category_tool.trade_phase.default

    self.assertEquals([trade_phase.delivery,
                       trade_phase.invoicing],
                      business_process.getRemainingTradePhaseList(
                       business_process.order))
    self.assertEquals([trade_phase.invoicing],
                      business_process.getRemainingTradePhaseList(
                       business_process.deliver))
    self.assertEquals([],
                      business_process.getRemainingTradePhaseList(
                       business_process.invoice))
444

445
  @newSimulationExpectedFailure
446
  def test_BusinessLink_calculateExpectedDate(self):
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
    """
    This test case is described for what start/stop date is expected on
    each path by explanation.
    In this case, root explanation is path of between "b" and "d", and
    lead time and wait time is set on each path.
    ("l" is lead time, "w" is wait_time)

    Each path must calculate most early day from getting most longest
    path in the simulation.

    "referential_date" represents for which date have to get of explanation from reality.

                  (root_explanation)
        l:2, w:1         l:3, w:1       l:4, w:2
    a ------------ b -------------- d -------------- e
                    \             /
                     \           /
             l:2, w:1 \         / l:3, w:0
                       \       /
                        \     /
                         \   /
                          \ /
                           c
    """
    # define business process
472
    category_tool = self.getCategoryTool()
473
    business_process = self.createBusinessProcess()
474 475 476 477 478
    business_link_a_b = self.createBusinessLink(business_process)
    business_link_b_c = self.createBusinessLink(business_process)
    business_link_b_d = self.createBusinessLink(business_process)
    business_link_c_d = self.createBusinessLink(business_process)
    business_link_d_e = self.createBusinessLink(business_process)
479 480 481 482 483
    business_state_a = category_tool.trade_state.state_a
    business_state_b = category_tool.trade_state.state_b
    business_state_c = category_tool.trade_state.state_c
    business_state_d = category_tool.trade_state.state_d
    business_state_e = category_tool.trade_state.state_e
484 485 486 487 488 489 490 491 492 493
    business_link_a_b.setPredecessorValue(business_state_a)
    business_link_b_c.setPredecessorValue(business_state_b)
    business_link_b_d.setPredecessorValue(business_state_b)
    business_link_c_d.setPredecessorValue(business_state_c)
    business_link_d_e.setPredecessorValue(business_state_d)
    business_link_a_b.setSuccessorValue(business_state_b)
    business_link_b_c.setSuccessorValue(business_state_c)
    business_link_b_d.setSuccessorValue(business_state_d)
    business_link_c_d.setSuccessorValue(business_state_d)
    business_link_d_e.setSuccessorValue(business_state_e)
494 495

    business_process.edit(referential_date='stop_date')
496 497 498 499 500
    business_link_a_b.edit(title='a_b', lead_time=2, wait_time=1)
    business_link_b_c.edit(title='b_c', lead_time=2, wait_time=1)
    business_link_b_d.edit(title='b_d', lead_time=3, wait_time=1)
    business_link_c_d.edit(title='c_d', lead_time=3, wait_time=0)
    business_link_d_e.edit(title='d_e', lead_time=4, wait_time=2)
501 502

    # root explanation
503
    business_link_b_d.edit(deliverable=True)
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
    self.stepTic()

    """
    Basic test, lead time of reality and simulation are consistent.
    """
    class Mock:
      def __init__(self, date):
        self.date = date
      def getStartDate(self):
        return self.date
      def getStopDate(self):
        return self.date + 3 # lead time of reality

    base_date = DateTime('2009/04/01 GMT+9')
    mock = Mock(base_date)

    # root explanation.
521 522
    self.assertEquals(business_link_b_d.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_b_d.getExpectedStopDate(mock), DateTime('2009/04/04 GMT+9'))
523 524

    # assertion for each path without root explanation.
525 526 527 528 529 530 531 532
    self.assertEquals(business_link_a_b.getExpectedStartDate(mock), DateTime('2009/03/27 GMT+9'))
    self.assertEquals(business_link_a_b.getExpectedStopDate(mock), DateTime('2009/03/29 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStartDate(mock), DateTime('2009/03/30 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStopDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStopDate(mock), DateTime('2009/04/04 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStartDate(mock), DateTime('2009/04/06 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStopDate(mock), DateTime('2009/04/10 GMT+9'))
533 534 535 536 537 538 539 540

    """
    Test of illegal case, lead time of reality and simulation are inconsistent,
    always reality is taken, but it depends on which date(e.g. start_date and stop_date) is referential.

    How we know which is referential, currently implementation of it can be known by
    BusinessProcess.isStartDateReferential and BusinessProcess.isStopDateReferential.

541
    In this test case, stop_date on business_link_b_d is referential, because business_link_b_d is
542 543 544
    root explanation and business_process refer to stop_date as referential.

    calculation example(when referential date is 2009/04/06 GMT+9):
545
    start_date of business_link_b_d = referential_date - 3(lead_time of business_link_b_d)
546 547 548 549 550 551 552 553 554 555 556 557 558 559
                                    = 2009/04/06 GMT+9 - 3
                                    = 2009/04/03 GMT+9
    """
    class Mock:
      def __init__(self, date):
        self.date = date
      def getStartDate(self):
        return self.date
      def getStopDate(self):
        return self.date + 5 # changed

    base_date = DateTime('2009/04/01 GMT+9')
    mock = Mock(base_date)

560
    self.assertEquals(business_link_b_d.getExpectedStartDate(mock), DateTime('2009/04/03 GMT+9'))
561
    # This is base in this context, because referential_date is 'stop_date'
562
    self.assertEquals(business_link_b_d.getExpectedStopDate(mock), DateTime('2009/04/06 GMT+9'))
563 564

    # assertion for each path without root explanation.
565 566 567 568 569 570 571 572
    self.assertEquals(business_link_a_b.getExpectedStartDate(mock), DateTime('2009/03/29 GMT+9'))
    self.assertEquals(business_link_a_b.getExpectedStopDate(mock), DateTime('2009/03/31 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStartDate(mock), DateTime('2009/04/01 GMT+9'))
    self.assertEquals(business_link_b_c.getExpectedStopDate(mock), DateTime('2009/04/03 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStartDate(mock), DateTime('2009/04/03 GMT+9'))
    self.assertEquals(business_link_c_d.getExpectedStopDate(mock), DateTime('2009/04/06 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStartDate(mock), DateTime('2009/04/08 GMT+9'))
    self.assertEquals(business_link_d_e.getExpectedStopDate(mock), DateTime('2009/04/12 GMT+9'))
573

574 575 576 577 578 579 580 581 582 583 584 585
  def test_isBuildable(self):
    """Test isBuildable for ordered, delivered and invoiced sequence

    Here Business Process sequence corresponds simulation tree.

    delivery_path is related to root applied rule, and invoice_path is related
    to rule below, and invoice_path is after delivery_path
    """
    self._createOrderedDeliveredInvoicedBusinessProcess()
    self.constructSimulationTreeAndDeliveries()

    self.order.setSimulationState(self.completed_state)
586 587
    self.stepTic()

Sebastien Robin's avatar
Sebastien Robin committed
588 589 590 591
    def checkIsBusinessLinkBuildable(explanation, business_link, value):
      self.assertEquals(self.business_process.isBusinessLinkBuildable(
       explanation, business_link), value)

592
    # in the beginning only order related movements shall be buildable
593
    checkIsBusinessLinkBuildable(self.order, self.delivery_link, True)
Sebastien Robin's avatar
Sebastien Robin committed
594 595 596
    self.assertEquals(self.delivery_simulation_movement.isBuildable(), True)
    self.assertEquals(self.split_delivery_simulation_movement.isBuildable(), True)

597
    checkIsBusinessLinkBuildable(self.order, self.invoice_link, False)
Sebastien Robin's avatar
Sebastien Robin committed
598 599
    self.assertEquals(self.invoicing_simulation_movement.isBuildable(), False)
    self.assertEquals(self.split_invoicing_simulation_movement.isBuildable(),
600 601 602
        False)

    # add delivery
603
    delivery = self._createDelivery(causality_value = self.order)
604
    delivery_line = self._createMovement(delivery)
605 606

    # relate not split movement with delivery (deliver it)
Sebastien Robin's avatar
Sebastien Robin committed
607
    self.delivery_simulation_movement.edit(delivery_value = delivery_line)
608 609 610

    self.stepTic()

611
    # delivery_link (for order) is still buildable, as split movement is not
612 613
    # delivered yet
    #
614
    # invoice_link is not yet buildable, delivery is in inproper simulation
Łukasz Nowak's avatar
Łukasz Nowak committed
615
    # state
616
    #
617
    # delivery_link (for delivery) is not buildable - delivery is already
618
    # built for those movements
619
    checkIsBusinessLinkBuildable(self.order, self.delivery_link, True)
Sebastien Robin's avatar
Sebastien Robin committed
620 621 622 623 624 625
    self.assertEquals(self.split_delivery_simulation_movement.isBuildable(), True)

    checkIsBusinessLinkBuildable(delivery, self.delivery_link, False)
    checkIsBusinessLinkBuildable(delivery, self.invoice_link, False)
    self.assertEquals(self.delivery_simulation_movement.isBuildable(), False)
    self.assertEquals(self.invoicing_simulation_movement.isBuildable(), False)
626
    checkIsBusinessLinkBuildable(self.order, self.invoice_link, False)
Sebastien Robin's avatar
Sebastien Robin committed
627
    self.assertEquals(self.split_invoicing_simulation_movement.isBuildable(), False)
628 629 630

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)
631

632
    delivery.setSimulationState(self.completed_state)
633

634
    self.assertEqual(self.completed_state, delivery.getSimulationState())
635 636 637

    self.stepTic()

638
    # delivery_link (for order) is still buildable, as split movement is not
639 640
    # delivered yet
    #
641 642 643 644
    # invoice_link is not buildable in case of order because delivery_link
    # is not completed yet.
    #
    # invoice link is buildable for delivery because part of tree is buildable
645 646 647
    #
    # split movement for invoicing is not buildable - no proper delivery
    # related for previous path
648
    checkIsBusinessLinkBuildable(self.order, self.delivery_link, True)
Sebastien Robin's avatar
Sebastien Robin committed
649 650 651
    self.assertEquals(self.invoicing_simulation_movement.isBuildable(), True)
    checkIsBusinessLinkBuildable(delivery, self.invoice_link, True)

652
    checkIsBusinessLinkBuildable(self.order, self.invoice_link, False)
Sebastien Robin's avatar
Sebastien Robin committed
653 654 655 656
    checkIsBusinessLinkBuildable(delivery, self.invoice_link, True)
    checkIsBusinessLinkBuildable(delivery, self.delivery_link, False)
    self.assertEquals(self.delivery_simulation_movement.isBuildable(), False)
    self.assertEquals(self.split_invoicing_simulation_movement.isBuildable(),
657 658
        False)

659
  def test_isCompleted(self):
660 661
    """Test isCompleted for ordered, delivered and invoiced sequence"""
    self._createOrderedDeliveredInvoicedBusinessProcess()
662
    self.constructSimulationTreeAndDeliveries()
663

664 665
    self.assertEqual(self.delivery_link.isCompleted(self.order), False)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(self.order), False)
666

667 668
    self.assertEqual(self.invoice_link.isCompleted(self.order), False)
    self.assertEqual(self.invoice_link.isPartiallyCompleted(self.order), False)
669 670

    # add delivery
671
    delivery = self._createDelivery(causality_value = self.order)
672 673 674
    delivery_line = self._createMovement(delivery)

    # relate not split movement with delivery (deliver it)
675
    self.delivery_simulation_movement.edit(delivery_value = delivery_line)
676 677 678 679

    self.stepTic()

    # nothing changes
680 681
    self.assertEqual(self.delivery_link.isCompleted(self.order), False)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(self.order), False)
682

683 684
    self.assertEqual(self.invoice_link.isCompleted(self.order), False)
    self.assertEqual(self.invoice_link.isPartiallyCompleted(self.order), False)
685 686

    # from delivery point of view everything is same
687 688
    self.assertEqual(self.delivery_link.isCompleted(delivery), False)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(delivery), False)
689

690 691
    self.assertEqual(self.invoice_link.isCompleted(delivery), False)
    self.assertEqual(self.invoice_link.isPartiallyCompleted(delivery), False)
692 693 694 695 696 697 698 699 700 701

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.completed_state)

    self.assertEqual(self.completed_state, delivery.getSimulationState())

    self.stepTic()

702 703 704 705 706
    self.assertEqual(self.delivery_link.isCompleted(self.order), False)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(self.order), True)

    self.assertEqual(self.invoice_link.isCompleted(self.order), False)
    self.assertEqual(self.invoice_link.isPartiallyCompleted(self.order), False)
707

708 709
    self.assertEqual(self.delivery_link.isCompleted(delivery), True)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(delivery), True)
710

711 712 713 714 715 716 717 718 719 720
    self.assertEqual(self.invoice_link.isCompleted(delivery), False)
    self.assertEqual(self.invoice_link.isPartiallyCompleted(delivery), False)

    # and finally deliver everything simulation movement coming from order
    another_delivery = self._createDelivery()
    another_delivery_line = self._createMovement(another_delivery)
    self.split_delivery_simulation_movement.edit(
      delivery_value=another_delivery_line)
    another_delivery.setSimulationState(self.completed_state)
    self.stepTic()
721

722 723
    self.assertEqual(self.delivery_link.isCompleted(self.order), True)
    self.assertEqual(self.delivery_link.isPartiallyCompleted(self.order), True)
724

725 726 727
  def test_isFrozen_OrderedDeliveredInvoiced(self):
    """Test isFrozen for ordered, delivered and invoiced sequence"""
    self._createOrderedDeliveredInvoicedBusinessProcess()
728
    self.constructSimulationTreeAndDeliveries()
729

730 731 732 733 734
    self.assertEqual(self.order_link.isFrozen(self.order), False)
    self.assertEqual(self.delivery_link.isFrozen(self.order), False)
    self.assertEqual(self.invoice_link.isFrozen(self.order), False)
    self.assertEqual(self.simulation_movement.isFrozen(), False)
    self.assertEqual(self.split_simulation_movement.isFrozen(), False)
735

736
    self.order.setSimulationState(self.completed_state)
737
    self.stepTic()
738 739
    self.assertEqual(self.order_link.isFrozen(self.order), True)
    self.assertEqual(self.delivery_link.isFrozen(self.order), False)
740

741 742 743 744
    self.assertEqual(self.simulation_movement.isFrozen(), True)
    self.assertEqual(self.invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(self.split_simulation_movement.isFrozen(), True)
    self.assertEqual(self.split_invoicing_simulation_movement.isFrozen(), False)
745 746

    # add delivery
747
    delivery = self._createDelivery()
748 749 750
    delivery_line = self._createMovement(delivery)

    # relate not split movement with delivery (deliver it)
751
    self.delivery_simulation_movement.edit(delivery_value = delivery_line)
752 753 754 755

    self.stepTic()

    # nothing changes
756 757
    self.assertEqual(self.delivery_link.isFrozen(self.order), False)
    self.assertEqual(self.invoice_link.isFrozen(self.order), False)
758 759

    # from delivery point of view everything is same
760 761
    self.assertEqual(self.delivery_link.isFrozen(delivery), False)
    self.assertEqual(self.invoice_link.isFrozen(delivery), False)
762

763 764 765 766
    self.assertEqual(self.simulation_movement.isFrozen(), True)
    self.assertEqual(self.invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(self.split_simulation_movement.isFrozen(), True)
    self.assertEqual(self.split_invoicing_simulation_movement.isFrozen(), False)
767 768 769 770 771 772 773 774 775 776

    # put delivery in simulation state configured on path (and this state is
    # available directly on movements)

    delivery.setSimulationState(self.frozen_state)

    self.assertEqual(self.frozen_state, delivery.getSimulationState())

    self.stepTic()

777 778 779 780
    self.assertEqual(self.delivery_link.isFrozen(self.order), False)
    self.assertEqual(self.invoice_link.isFrozen(self.order), False)
    self.assertEqual(self.delivery_link.isFrozen(delivery), True)
    self.assertEqual(self.invoice_link.isFrozen(delivery), False)
781

782 783 784 785
    self.assertEqual(self.delivery_simulation_movement.isFrozen(), True)
    self.assertEqual(self.invoicing_simulation_movement.isFrozen(), False)
    self.assertEqual(self.split_simulation_movement.isFrozen(), True)
    self.assertEqual(self.split_invoicing_simulation_movement.isFrozen(), False)
786

787 788 789 790
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestBPMImplementation))
  return suite