testMRP.py 22.8 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 39 40 41 42 43 44 45
# -*- 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 AccessControl.SecurityManagement import newSecurityManager
from DateTime import DateTime

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

from Products.ERP5.tests.testBPMCore import TestBPMMixin

class TestMRPMixin(TestBPMMixin):
  transformation_portal_type = 'Transformation'
  transformed_resource_portal_type = 'Transformation Transformed Resource'
  product_portal_type = 'Product'
46 47 48
  organisation_portal_type = 'Organisation'
  order_portal_type = 'Production Order'
  order_line_portal_type = 'Production Order Line'
49 50 51 52 53 54 55 56 57 58 59 60

  def setUpOnce(self):
    self.portal = self.getPortalObject()

  def invalidateRules(self):
    """
    do reversely of validateRules
    """
    rule_tool = self.getRuleTool()
    for rule in rule_tool.contentValues(
      portal_type=rule_tool.getPortalRuleTypeList()):
      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 161 162 163 164 165 166
    return transformation

  @reindex
  def createSimpleBusinessProcess(self):
    """    mrp/p2                    mrp/3
    ready -------- partial_produced ------- done
    """
    business_process = self.createBusinessProcess()
    business_path_p2 = self.createBusinessPath(business_process)
    business_path_p3 = self.createBusinessPath(business_process)
    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 174 175 176 177
    business_process.edit(referential_date='stop_date')
    business_path_p2.edit(id='p2',
                          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 185 186 187 188
    business_path_p3.edit(id='p3',
                          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 204 205 206 207 208
    return business_process

  @reindex
  def createConcurrentBusinessProcess(self):
    """    mrp/p2
    ready ======== partial_produced
           mrp/p3
    """
    business_process = self.createBusinessProcess()
    business_path_p2 = self.createBusinessPath(business_process)
    business_path_p3 = self.createBusinessPath(business_process)
    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 216 217 218 219
    business_process.edit(referential_date='stop_date')
    business_path_p2.edit(id='p2',
                          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 227 228 229 230
    business_path_p3.edit(id='p3',
                          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 256 257 258 259 260 261 262 263 264 265 266 267 268 269
class TestMRPImplementation(TestMRPMixin, ERP5TypeTestCase):
  """the test for implementation"""
  def test_TransformationRule_getHeadProductionPathList(self):
    rule = self.portal.portal_rules.default_transformation_rule

    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):
270 271 272
    # mock order
    order = self.createDefaultOrder()
    order_line = order.objectValues()[0]
273

274
    business_process = order.getSpecialiseValue()
275

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

280 281 282 283 284 285 286 287 288
    # organisations
    path = business_process.objectValues(
      portal_type=self.portal.getPortalBusinessPathTypeList())[0]
    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)
289 290 291 292 293 294 295 296 297 298 299 300 301

    # 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(),
302
                  resource=order_line.getResource())
303 304 305 306 307 308 309 310
    # test mock
    applied_rule = movement.newContent(potal_type='Applied Rule')

    rule = self.portal.portal_rules.default_transformation_rule
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
311 312 313 314 315 316 317
      ((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)])
318 319 320 321
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
322
      movement_value_set |= set([(tuple(movement.getCausalityList()),
323
                                  movement.getResource(),
324 325 326 327
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
328 329 330 331
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

332
  def test_TransformationRule_expand_concurrent(self):
333
    business_process = self.createConcurrentBusinessProcess()
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

    # 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(
      portal_type=self.portal.getPortalBusinessPathTypeList())[0]
    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()
356 357 358 359 360 361 362 363 364 365

    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(),
366
                  resource=order_line.getResource())
367 368 369 370 371 372 373 374
    # test mock
    applied_rule = movement.newContent(potal_type='Applied Rule')

    rule = self.portal.portal_rules.default_transformation_rule
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
375 376 377 378 379
      ((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)])
380 381 382 383
    movement_list = applied_rule.objectValues()
    self.assertEquals(len(expected_value_set), len(movement_list))
    movement_value_set = set([])
    for movement in movement_list:
384
      movement_value_set |= set([(tuple(movement.getCausalityList()),
385
                                  movement.getResource(),
386 387 388 389
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
390 391 392 393
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

394
  def test_TransformationRule_expand_reexpand(self):
395 396 397 398
    """
    test case of difference when any movement are frozen
    by using above result
    """
399 400
    self.test_TransformationRule_expand_concurrent()

401 402
    self.stepTic()

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    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(
      portal_type=self.portal.getPortalBusinessPathTypeList())[0]
    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():
426
      movement.edit(quantity=1)
427
      # set the state value of isFrozen to 1,
428 429 430
      movement._baseSetFrozen(1)

    # re-expand
431
    rule = self.portal.portal_rules.default_transformation_rule
432 433 434 435
    rule.expand(applied_rule)

    # assertion
    expected_value_set = set([
436 437 438 439 440 441 442 443 444 445
      ((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)])
446 447 448 449 450 451
    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(),
452 453 454 455
                                  (movement.getSourceSection(),
                                   movement.getSource(),
                                   movement.getDestinationSection(),
                                   movement.getDestination(),), # organisations
456 457 458 459
                                  movement.getTradePhase(),
                                  movement.getQuantity())])
    self.assertEquals(expected_value_set, movement_value_set)

460 461 462 463 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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  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')

    rule = self.portal.portal_rules.default_transformation_sourcing_rule
    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)


529 530 531 532
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestMRPImplementation))
  return suite