testMRP.py 23 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 30 31 32 33 34 35 36 37 38
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Yusuke Muraoka <yusuke@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., 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

from Products.CMFCore.utils import getToolByName
from Products.ERP5Type.tests.utils import reindex

from Products.ERP5.tests.testBPMCore import TestBPMMixin
39
from Products.ERP5Type.tests.backportUnittest import skip
40 41 42 43 44

class TestMRPMixin(TestBPMMixin):
  transformation_portal_type = 'Transformation'
  transformed_resource_portal_type = 'Transformation Transformed Resource'
  product_portal_type = 'Product'
45 46 47
  organisation_portal_type = 'Organisation'
  order_portal_type = 'Production Order'
  order_line_portal_type = 'Production Order Line'
48

49
  def getBusinessTemplateList(self):
50
    return TestBPMMixin.getBusinessTemplateList(self) + ('erp5_mrp', )
51

52 53 54 55 56 57 58
  def invalidateRules(self):
    """
    do reversely of validateRules
    """
    rule_tool = self.getRuleTool()
    for rule in rule_tool.contentValues(
      portal_type=rule_tool.getPortalRuleTypeList()):
59 60
      if rule.getValidationState() == 'validated':
        rule.invalidate()
61 62

  def _createDocument(self, portal_type, **kw):
63
    module = self.portal.getDefaultModule(
64 65
        portal_type=portal_type)
    return self._createObject(module, portal_type, **kw)
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
  def _createObject(self, parent, portal_type, id=None, **kw):
    o = None
    if id is not None:
      o = parent.get(str(id), None)
    if o is None:
      o = parent.newContent(portal_type=portal_type)
    o.edit(**kw)
    return o

  def createTransformation(self, **kw):
    return self._createDocument(self.transformation_portal_type, **kw)

  def createProduct(self, **kw):
    return self._createDocument(self.product_portal_type, **kw)

  def createOrganisation(self, **kw):
    return self._createDocument(self.organisation_portal_type, **kw)

  def createOrder(self, **kw):
    return self._createDocument(self.order_portal_type, **kw)

  def createOrderLine(self, order, **kw):
    return self._createObject(order, self.order_line_portal_type, **kw)

  def createTransformedResource(self, transformation, **kw):
    return self._createObject(transformation, self.transformed_resource_portal_type, **kw)
93 94 95 96 97 98 99 100 101 102

  @reindex
  def createCategories(self):
    category_tool = getToolByName(self.portal, 'portal_categories')
    self.createCategoriesInCategory(category_tool.base_amount, ['weight'])
    self.createCategoriesInCategory(category_tool.base_amount.weight, ['kg'])
    self.createCategoriesInCategory(category_tool.trade_phase, ['mrp',])
    self.createCategoriesInCategory(category_tool.trade_phase.mrp,
        ['p' + str(i) for i in range(5)]) # phase0 ~ 4

103 104 105 106 107 108 109 110
  @reindex
  def createDefaultOrder(self, transformation=None, business_process=None):
    if transformation is None:
      transformation = self.createDefaultTransformation()
    if business_process is None:
      business_process = self.createSimpleBusinessProcess()

    base_date = DateTime()
111

112 113 114 115 116 117 118 119 120 121 122 123
    order = self.createOrder(specialise_value=business_process,
                             start_date=base_date,
                             stop_date=base_date+3)
    order_line = self.createOrderLine(order,
                                      quantity=10,
                                      resource=transformation.getResource(),
                                      specialise_value=transformation)
    # XXX in some case, specialise_value is not related to order_line by edit,
    #     but by setSpecialise() is ok, Why?
    order_line.setSpecialiseValue(transformation)
    return order
    
124 125
  @reindex
  def createDefaultTransformation(self):
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
    resource1 = self.createProduct(id='1', quantity_unit_list=['weight/kg'])
    resource2 = self.createProduct(id='2', quantity_unit_list=['weight/kg'])
    resource3 = self.createProduct(id='3', quantity_unit_list=['weight/kg'])
    resource4 = self.createProduct(id='4', quantity_unit_list=['weight/kg'])
    resource5 = self.createProduct(id='5', quantity_unit_list=['weight/kg'])

    transformation = self.createTransformation(resource_value=resource5)
    self.createTransformedResource(transformation=transformation,
                                   resource_value=resource1,
                                   quantity=3,
                                   quantity_unit_list=['weight/kg'],
                                   trade_phase='mrp/p2')
    self.createTransformedResource(transformation=transformation,
                                   resource_value=resource2,
                                   quantity=1,
                                   quantity_unit_list=['weight/kg'],
                                   trade_phase='mrp/p2')
    self.createTransformedResource(transformation=transformation,
                                   resource_value=resource3,
                                   quantity=4,
                                   quantity_unit_list=['weight/kg'],
                                   trade_phase='mrp/p3')
    self.createTransformedResource(transformation=transformation,
                                   resource_value=resource4,
                                   quantity=1,
                                   quantity_unit_list=['weight/kg'],
                                   trade_phase='mrp/p3')
153 154 155 156 157 158 159 160
    return transformation

  @reindex
  def createSimpleBusinessProcess(self):
    """    mrp/p2                    mrp/3
    ready -------- partial_produced ------- done
    """
    business_process = self.createBusinessProcess()
161 162
    business_link_p2 = self.createBusinessLink(business_process)
    business_link_p3 = self.createBusinessLink(business_process)
163 164 165 166
    business_state_ready = self.createBusinessState(business_process)
    business_state_partial = self.createBusinessState(business_process)
    business_state_done = self.createBusinessState(business_process)

167 168 169 170 171 172
    # organisations
    source_section = self.createOrganisation(title='source_section')
    source = self.createOrganisation(title='source')
    destination_section = self.createOrganisation(title='destination_section')
    destination = self.createOrganisation(title='destination')
    
173
    business_process.edit(referential_date='stop_date')
174
    business_link_p2.edit(id='p2',
175 176 177
                          predecessor_value=business_state_ready,
                          successor_value=business_state_partial,
                          quantity=1,
178 179 180 181 182 183
                          trade_phase=['mrp/p2'],
                          source_section_value=source_section,
                          source_value=source,
                          destination_section_value=destination_section,
                          destination_value=destination,
                          )
184
    business_link_p3.edit(id='p3',
185 186 187 188
                          predecessor_value=business_state_partial,
                          successor_value=business_state_done,
                          quantity=1,
                          deliverable=1, # root explanation
189 190 191 192 193 194
                          trade_phase=['mrp/p3'],
                          source_section_value=source_section,
                          source_value=source,
                          destination_section_value=destination_section,
                          destination_value=destination,
                          )
195 196 197 198 199 200 201 202 203
    return business_process

  @reindex
  def createConcurrentBusinessProcess(self):
    """    mrp/p2
    ready ======== partial_produced
           mrp/p3
    """
    business_process = self.createBusinessProcess()
204 205
    business_link_p2 = self.createBusinessLink(business_process)
    business_link_p3 = self.createBusinessLink(business_process)
206 207 208
    business_state_ready = self.createBusinessState(business_process)
    business_state_partial = self.createBusinessState(business_process)

209 210 211 212 213 214
    # organisations
    source_section = self.createOrganisation(title='source_section')
    source = self.createOrganisation(title='source')
    destination_section = self.createOrganisation(title='destination_section')
    destination = self.createOrganisation(title='destination')

215
    business_process.edit(referential_date='stop_date')
216
    business_link_p2.edit(id='p2',
217 218 219
                          predecessor_value=business_state_ready,
                          successor_value=business_state_partial,
                          quantity=1,
220 221 222 223 224 225
                          trade_phase=['mrp/p2'],
                          source_section_value=source_section,
                          source_value=source,
                          destination_section_value=destination_section,
                          destination_value=destination,
                          )
226
    business_link_p3.edit(id='p3',
227 228 229 230
                          predecessor_value=business_state_ready,
                          successor_value=business_state_partial,
                          quantity=1,
                          deliverable=1, # root explanation
231 232 233 234 235 236
                          trade_phase=['mrp/p3'],
                          source_section_value=source_section,
                          source_value=source,
                          destination_section_value=destination_section,
                          destination_value=destination,
                          )
237 238
    return business_process

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
  @reindex
  def beforeTearDown(self):
    super(TestMRPMixin, self).beforeTearDown()
    transaction.abort()
    for module in (
      self.portal.organisation_module,
      self.portal.production_order_module, 
      self.portal.transformation_module,
      self.portal.business_process_module,
      # don't remove document because reuse it for testing of id
      # self.portal.product_module,
      self.portal.portal_simulation,):    
      module.manage_delObjects(list(module.objectIds()))
    transaction.commit()

254 255
class TestMRPImplementation(TestMRPMixin, ERP5TypeTestCase):
  """the test for implementation"""
256
  @skip('Unfinished experimental feature')
257
  def test_TransformationRule_getHeadProductionPathList(self):
258
    rule = self.portal.portal_rules.default_transformation_model_rule
259 260 261 262 263 264 265 266 267 268 269 270

    transformation = self.createDefaultTransformation()

    business_process = self.createSimpleBusinessProcess()
    self.assertEquals([business_process.p2],
                      rule.getHeadProductionPathList(transformation, business_process))

    business_process = self.createConcurrentBusinessProcess()
    self.assertEquals(set([business_process.p2, business_process.p3]),
                      set(rule.getHeadProductionPathList(transformation, business_process)))

  def test_TransformationRule_expand(self):
271 272 273
    # mock order
    order = self.createDefaultOrder()
    order_line = order.objectValues()[0]
274

275
    business_process = order.getSpecialiseValue()
276

277 278 279
    # paths
    path_p2 = '%s/p2' % business_process.getRelativeUrl()
    path_p3 = '%s/p3' % business_process.getRelativeUrl()
280

281 282
    # organisations
    path = business_process.objectValues(
283
      portal_type=self.portal.getPortalBusinessLinkTypeList())[0]
284 285 286 287 288 289
    source_section = path.getSourceSection()
    source = path.getSource()
    destination_section = path.getDestinationSection()
    destination = path.getDestination()
    consumed_organisations = (source_section, source, destination_section, None)
    produced_organisations = (source_section, None, destination_section, destination)
290 291 292 293 294 295 296 297 298 299 300 301 302

    # don't need another rules, just need TransformationRule for test
    self.invalidateRules()

    self.stepTic()

    # alter simulations of the order
    # root
    applied_rule = self.portal.portal_simulation.newContent(portal_type='Applied Rule')
    movement = applied_rule.newContent(portal_type='Simulation Movement')
    applied_rule.edit(causality_value=order)
    movement.edit(order_value=order_line,
                  quantity=order_line.getQuantity(),
303
                  resource=order_line.getResource())
304 305 306
    # test mock
    applied_rule = movement.newContent(potal_type='Applied Rule')

307
    rule = self.portal.portal_rules.default_transformation_model_rule
308 309 310 311
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
312 313 314 315 316 317 318
      ((path_p2,), 'product_module/5', produced_organisations, 'mrp/p3', -10),
      ((path_p2,), 'product_module/1', consumed_organisations, 'mrp/p2', 30),
      ((path_p2,), 'product_module/2', consumed_organisations, 'mrp/p2', 10),
      ((path_p3,), 'product_module/5', consumed_organisations, 'mrp/p3', 10),
      ((path_p3,), 'product_module/3', consumed_organisations, 'mrp/p3', 40),
      ((path_p3,), 'product_module/4', consumed_organisations, 'mrp/p3', 10),
      ((path_p3,), 'product_module/5', produced_organisations, None, -10)])
319 320 321 322
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
323
      movement_value_set |= set([(tuple(movement.getCausalityList()),
324
                                  movement.getResource(),
325 326 327 328
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
329 330 331 332
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

333
  @skip('Unfinished experimental feature')
334
  def test_TransformationRule_expand_concurrent(self):
335
    business_process = self.createConcurrentBusinessProcess()
336 337 338 339 340 341 342 343 344 345 346

    # mock order
    order = self.createDefaultOrder(business_process=business_process)
    order_line = order.objectValues()[0]

    # phases
    phase_p2 = '%s/p2' % business_process.getRelativeUrl()
    phase_p3 = '%s/p3' % business_process.getRelativeUrl()

    # organisations
    path = business_process.objectValues(
347
      portal_type=self.portal.getPortalBusinessLinkTypeList())[0]
348 349 350 351 352 353 354 355 356 357
    source_section = path.getSourceSection()
    source = path.getSource()
    destination_section = path.getDestinationSection()
    destination = path.getDestination()
    organisations = (source_section, source, destination_section, destination)
    consumed_organisations = (source_section, source, destination_section, None)
    produced_organisations = (source_section, None, destination_section, destination)

    # don't need another rules, just need TransformationRule for test
    self.invalidateRules()
358 359 360 361 362 363 364 365 366 367

    self.stepTic()

    # alter simulations of the order
    # root
    applied_rule = self.portal.portal_simulation.newContent(portal_type='Applied Rule')
    movement = applied_rule.newContent(portal_type='Simulation Movement')
    applied_rule.edit(causality_value=order)
    movement.edit(order_value=order_line,
                  quantity=order_line.getQuantity(),
368
                  resource=order_line.getResource())
369 370 371
    # test mock
    applied_rule = movement.newContent(potal_type='Applied Rule')

372
    rule = self.portal.portal_rules.default_transformation_model_rule
373 374 375 376
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
377 378 379 380 381
      ((phase_p2,), 'product_module/1', consumed_organisations, 'mrp/p2', 30),
      ((phase_p2,), 'product_module/2', consumed_organisations, 'mrp/p2', 10),
      ((phase_p3,), 'product_module/3', consumed_organisations, 'mrp/p3', 40),
      ((phase_p3,), 'product_module/4', consumed_organisations, 'mrp/p3', 10),
      ((phase_p2, phase_p3), 'product_module/5', produced_organisations, None, -10)])
382 383 384 385
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
386
      movement_value_set |= set([(tuple(movement.getCausalityList()),
387
                                  movement.getResource(),
388 389 390 391
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
392 393 394 395
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

396
  @skip('Unfinished experimental feature')
397
  def test_TransformationRule_expand_reexpand(self):
398 399 400 401
    """
    test case of difference when any movement are frozen
    by using above result
    """
402 403
    self.test_TransformationRule_expand_concurrent()

404 405
    self.stepTic()

406 407 408 409 410 411 412 413 414 415
    applied_rule = self.portal.portal_simulation.objectValues()[0]

    business_process = applied_rule.getCausalityValue().getSpecialiseValue()

    # phases
    phase_p2 = '%s/p2' % business_process.getRelativeUrl()
    phase_p3 = '%s/p3' % business_process.getRelativeUrl()

    # organisations
    path = business_process.objectValues(
416
      portal_type=self.portal.getPortalBusinessLinkTypeList())[0]
417 418 419 420 421 422 423 424 425 426 427 428
    source_section = path.getSourceSection()
    source = path.getSource()
    destination_section = path.getDestinationSection()
    destination = path.getDestination()
    consumed_organisations = (source_section, source, destination_section, None)
    produced_organisations = (source_section, None, destination_section, destination)

    movement = applied_rule.objectValues()[0]
    applied_rule = movement.objectValues()[0]

    # these movements are made by transformation
    for movement in applied_rule.objectValues():
429
      movement.edit(quantity=1)
430
      # set the state value of isFrozen to 1,
431 432 433
      movement._baseSetFrozen(1)

    # re-expand
434
    rule = self.portal.portal_rules.default_transformation_model_rule
435 436 437 438
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
439 440 441 442 443 444 445 446 447 448
      ((phase_p2,), 'product_module/1', consumed_organisations, 'mrp/p2', 1), # Frozen
      ((phase_p2,), 'product_module/1', consumed_organisations, 'mrp/p2', 29),
      ((phase_p2,), 'product_module/2', consumed_organisations, 'mrp/p2', 1), # Frozen
      ((phase_p2,), 'product_module/2', consumed_organisations, 'mrp/p2', 9),
      ((phase_p3,), 'product_module/3', consumed_organisations, 'mrp/p3', 1), # Frozen
      ((phase_p3,), 'product_module/3', consumed_organisations, 'mrp/p3', 39),
      ((phase_p3,), 'product_module/4', consumed_organisations, 'mrp/p3', 1), # Frozen
      ((phase_p3,), 'product_module/4', consumed_organisations, 'mrp/p3', 9),
      ((phase_p2, phase_p3), 'product_module/5', produced_organisations, None, 1), # Frozen
      ((phase_p2, phase_p3), 'product_module/5', produced_organisations, None, -11)])
449 450 451 452 453 454
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
      movement_value_set |= set([(tuple(movement.getCausalityList()),
                                  movement.getResource(),
455 456 457 458
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
459 460 461 462
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

463
  @skip('Unfinished experimental feature')
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
  def test_TransformationSourcingRule_expand(self):
    # mock order
    order = self.createDefaultOrder()
    order_line = order.objectValues()[0]

    # don't need another rules, just need TransformationSourcingRule for test
    self.invalidateRules()

    self.stepTic()

    business_process = order.getSpecialiseValue()

    # get last path of a business process
    # in simple business path, the last is between "partial_produced" and "done"
    causality_path = None
    for state in business_process.objectValues(
      portal_type=self.portal.getPortalBusinessStateTypeList()):
      if len(state.getRemainingTradePhaseList(self.portal)) == 0:
        causality_path = state.getSuccessorRelatedValue()

    # phases
    phase_p2 = '%s/p2' % business_process.getRelativeUrl()

    # organisations
    source_section = causality_path.getSourceSection()
    source = causality_path.getSource()
    destination_section = causality_path.getDestinationSection()
    destination = causality_path.getDestination()
    organisations = (source_section, source, destination_section, destination)

    # sourcing resource
    sourcing_resource = order_line.getResource()

    # alter simulations of the order
    # root
    applied_rule = self.portal.portal_simulation.newContent(portal_type='Applied Rule')
    movement = applied_rule.newContent(portal_type='Simulation Movement')
    applied_rule.edit(causality_value=order)
    movement.edit(order_value=order_line,
                  causality_value=causality_path,
                  quantity=order_line.getQuantity(),
                  resource=sourcing_resource,
                  )

    self.stepTic()

    # test mock
    applied_rule = movement.newContent(potal_type='Applied Rule')

513
    rule = self.portal.portal_rules.default_transformation_sourcing_model_rule
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
      ((phase_p2,), sourcing_resource, organisations, 10)])
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
      movement_value_set |= set([(tuple(movement.getCausalityList()),
                                  movement.getResource(),
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)


533 534 535 536
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestMRPImplementation))
  return suite