testInventoryModule.py 23.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
##############################################################################
#
# Copyright (c) 2004, 2005 Nexedi SARL and Contributors. All Rights Reserved.
#          Sebastien Robin <seb@nexedi.com>
#
# 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.
#
##############################################################################

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime
from zLOG import LOG
32
from Products.ERP5Type.tests.Sequence import SequenceList
33 34
from testOrder import TestOrderMixin

35
class TestInventoryModule(TestOrderMixin, ERP5TypeTestCase):
36
  """
37
    Test inventory module
38
  """
39
  run_all_test = 0
40 41
  inventory_portal_type = 'Inventory'
  inventory_line_portal_type = 'Inventory Line'
42
  inventory_cell_portal_type = 'Inventory Cell'
43
  item_portal_type = 'Apparel Bath'
44 45 46
  first_date_string = '2005/12/09' # First Inventory
  second_date_string = '2005/12/29' # Next Inventory
  view_stock_date = '2005/12/31' # The day where we are looking for stock
47
  size_list = ['Child/32','Child/34']
48

Sebastien Robin's avatar
Sebastien Robin committed
49 50 51
  def getTitle(self):
    return "Inventory Module"

52 53 54
  def getInventoryModule(self):
    return getattr(self.getPortal(), 'inventory_module',None)

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  def stepCreateInitialMovements(self, sequence=None, **kw):
    """Create movements before this inventory.
    """
    pplm = self.getPortal().purchase_packing_list_module
    splm = self.getPortal().sale_packing_list_module

    def deliverPackingList(pl):
      """step through all steps of packing list workflow."""
      pl.confirm()
      pl.setReady()
      pl.start()
      pl.stop()
      pl.deliver()
      self.assertEquals(pl.getSimulationState(), 'delivered')

    # we create content :
    #   1 purchase packing list
    #   1 sale packing list
    #   1 internal packing list (actually sale with same source)
    for month in range(1, 11):
      ppl = pplm.newContent(
                      portal_type='Purchase Packing List',
                      source_value = sequence.get('organisation2'),
                      destination_value = sequence.get('organisation1'),
                      start_date=DateTime(2005, month, 1),
                    )
      ppl.newContent( portal_type='Purchase Packing List Line',
                      resource_value=sequence.get('resource'),
                      quantity=month*10) # dummy quantity, it will be
                                         # replaced by inventory
      deliverPackingList(ppl)

      spl = splm.newContent(
                      portal_type='Sale Packing List',
                      source_value = sequence.get('organisation1'),
                      destination_value = sequence.get('organisation2'),
                      start_date=DateTime(2005, month, 1),
                    )
      spl.newContent( portal_type='Sale Packing List Line',
                      resource_value=sequence.get('resource'),
                      quantity=month*10)
      deliverPackingList(spl)
      
      ipl = splm.newContent(
                      portal_type='Sale Packing List',
                      source_value = sequence.get('organisation1'),
                      destination_value = sequence.get('organisation1'),
                      start_date=DateTime(2005, month, 1),
                    )
      ipl.newContent( portal_type='Sale Packing List Line',
                      resource_value=sequence.get('resource'),
                      quantity=month*10)
      deliverPackingList(ipl)
      
109
  def createInventory(self, start_date=None,
110 111 112 113 114
                                       sequence=None,**kw):
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
115
    organisation =  sequence.get('organisation1')
116 117 118
    inventory = self.getInventoryModule().newContent()
    inventory.edit(start_date=start_date,
                   destination_value=organisation)
119
    inventory.deliver()
120 121 122 123 124 125 126 127 128 129 130 131 132
    inventory_list = sequence.get('inventory_list',[])
    inventory_list.append(inventory)
    sequence.edit(inventory_list=inventory_list)
    return inventory

  def createNotVariatedInventoryLine(self, quantity=None,
                                       sequence=None,**kw):
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
    inventory = sequence.get('inventory_list')[-1]
    resource = sequence.get('resource_list')[-1]
133 134 135
    inventory_line = inventory.newContent(
           portal_type=self.inventory_line_portal_type)
    inventory_line.edit(inventory=quantity,
136
                        resource_value = resource)
137 138
    return inventory

139 140
  def stepCreateFirstNotVariatedInventory(self, sequence=None,
                                          sequence_list=None, **kw):
141 142 143 144 145 146 147
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
    date = DateTime(self.first_date_string)
    LOG('stepCreateFirstNotVariatedInventory, date',0,date)
    quantity=self.default_quantity
148 149
    self.createInventory(start_date=date,sequence=sequence)
    self.createNotVariatedInventoryLine(sequence=sequence,
150 151
                                    quantity=quantity)

152 153
  def stepCreateSecondNotVariatedInventory(self, sequence=None,
                                           sequence_list=None, **kw):
154 155 156 157 158 159
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
    date = DateTime(self.second_date_string)
    quantity=self.default_quantity - 2
160
    self.createInventory(start_date=date,sequence=sequence)
161
    self.second_inventory = self.createNotVariatedInventoryLine(sequence=sequence,
162 163
                                    quantity=quantity)

164 165 166 167 168 169 170 171 172 173 174

  def stepModifySecondNotVariatedInventory(self, sequence=None,
                                           sequence_list=None, **kw):
    """
    Modify the quantity to have a tmp line with null quantity
    """
    quantity=self.default_quantity
    inventory_line = self.second_inventory.objectValues()[0]
    inventory_line.edit(inventory=quantity)
    

175 176
  def stepCheckFirstNotVariatedInventory(self, start_date=None,quantity=None,
                                             sequence=None,**kw):
177
    node_uid = sequence.get('organisation1').getUid()
178 179 180 181
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
182
                        to_date=date,simulation_state='delivered')
183 184 185
    self.assertEquals(self.default_quantity,quantity)

  def stepCheckSecondNotVariatedInventory(self, start_date=None,quantity=None,
186
                                             sequence=None, **kw):
187
    node_uid = sequence.get('organisation1').getUid()
188 189
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
190 191
    LOG('CheckSecondNotVariatedInventory', 0,
        self.getSimulationTool().getInventory(node_uid=node_uid,
192 193
                        resource=resource_url,
                        to_date=date,src__=1))
194 195
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
196
                        to_date=date,simulation_state='delivered')
197 198
    self.assertEquals(self.default_quantity-2,quantity)

199 200 201 202 203 204 205 206 207 208 209 210
  def stepCheckSecondNotVariatedInventoryModified(self, start_date=None,quantity=None,
                                             sequence=None,**kw):
    node_uid = sequence.get('organisation1').getUid()
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date,simulation_state='delivered')
    self.assertEquals(self.default_quantity,quantity)


  def test_01_NotVariatedInventory(self, quiet=0, run=1): #run_all_test):
211 212 213 214 215 216 217
    """
    We will create an inventory with the default quantity for
    a particular resource. Then we will check if the is correct.
    Then we create another inventory and see if we have the new
    stock value
    """
    if not run: return
218 219
    self.logMessage('Test Not Variated Inventory')

220 221
    sequence_list = SequenceList()

222
    # Test with a simple inventory without cell
223 224 225 226 227 228 229 230 231
    sequence_string = 'stepCreateNotVariatedResource \
                       stepCreateOrganisation1 \
                       stepCreateInitialMovements \
                       stepTic \
                       stepCreateFirstNotVariatedInventory \
                       stepTic \
                       stepCheckFirstNotVariatedInventory \
                       stepCreateSecondNotVariatedInventory \
                       stepTic \
232 233 234 235
                       stepCheckSecondNotVariatedInventory \
                       stepModifySecondNotVariatedInventory \
                       stepTic \
                       stepCheckSecondNotVariatedInventoryModified'
236 237 238 239
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)

240
  def createVariatedInventoryLine(self, sequence=None, sequence_list=None, 
241
                                 start_date=None, quantity=None, item_list=None,
242
                                 **kw):
243 244 245 246
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
247 248 249 250 251
    inventory = sequence.get('inventory_list')[-1]
    resource = sequence.get('resource_list')[-1]
    inventory_line = inventory.newContent(
           portal_type=self.inventory_line_portal_type)
    inventory_line.edit(resource_value = resource)
252
    resource_vcl = list(resource.getVariationCategoryList(
253 254
                                   omit_individual_variation=1,
                                   omit_option_base_category=1))
255
    resource_vcl.sort()
256 257
    self.assertEquals(len(resource_vcl),2)
    inventory_line.setVariationCategoryList(resource_vcl)
258
    base_id = 'movement'
259
    cell_key_list = list(inventory_line.getCellKeyList(base_id=base_id))
260 261 262
    cell_key_list.sort()
    price = 100
    for cell_key in cell_key_list:
263 264 265
      cell = inventory_line.newCell(base_id=base_id,
                                portal_type=self.inventory_cell_portal_type,
                                *cell_key)
266 267
      cell.edit(mapped_value_property_list=['price','inventory'],
                price=price, inventory=quantity,
268
                predicate_category_list=cell_key,
269 270 271
                variation_category_list=cell_key,)
      if item_list is not None:
        cell.setAggregateValueList(item_list)
272 273 274
      price += 1
      quantity += 1

275
  def stepCreateFirstVariatedInventory(self, sequence=None, sequence_list=None, \
276 277 278 279 280 281
                                 **kw):
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
    date = DateTime(self.first_date_string)
282 283
    inventory = self.createInventory(start_date=date,sequence=sequence)
    quantity = self.default_quantity
284
    self.createVariatedInventoryLine(start_date=date,
285 286 287 288 289 290 291 292 293 294 295
                  sequence=sequence, quantity=quantity)

  def stepCreateSecondVariatedInventory(self, sequence=None, sequence_list=None, \
                                 **kw):
    """
    We will put default values for an inventory
    """
    portal = self.getPortal()
    date = DateTime(self.second_date_string)
    inventory = self.createInventory(start_date=date,sequence=sequence)
    quantity = self.default_quantity - 10
296
    self.createVariatedInventoryLine(start_date=date,
297
                  sequence=sequence, quantity=quantity)
298 299 300 301 302 303

  def createVariatedInventory(self, start_date=None,quantity=None,
                                       sequence=None,**kw):
    """
    We will put default values for an inventory
    """
304 305
    inventory = self.createNotVariatedInventory(sequence=sequence,
                                                start_date=start_date)
306
    resource = sequence.get('resource_list')[0]
307
    organisation =  sequence.get('organisation1')
308 309 310 311 312 313 314 315 316
    inventory = self.getInventoryModule().newContent()
    inventory.edit(start_date=start_date,
                   destination_value=organisation)
    inventory_line = inventory.newContent(
           portal_type=self.inventory_line_portal_type)
    inventory_line.edit(inventory=quantity,
                        resource_value = resource,
                        destination_value=organisation)

317 318
  def stepCheckFirstVariatedInventory(self, start_date=None,quantity=None,
                                             sequence=None,**kw):
319
    node_uid = sequence.get('organisation1').getUid()
320 321 322 323 324 325 326 327 328 329 330 331
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    total_quantity = 99 + 100
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date)
    self.assertEquals(total_quantity,quantity)
    variation_text = 'size/Child/32'
    total_quantity = 99
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        variation_text=variation_text,
332
                        to_date=date,simulation_state='delivered')
333 334 335 336
    self.assertEquals(total_quantity,quantity)

  def stepCheckSecondVariatedInventory(self, start_date=None,quantity=None,
                                             sequence=None,**kw):
337
    node_uid = sequence.get('organisation1').getUid()
338 339 340 341 342 343 344 345 346 347 348 349
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    total_quantity = 89 + 90
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date)
    self.assertEquals(total_quantity,quantity)
    variation_text = 'size/Child/32'
    total_quantity = 89
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        variation_text=variation_text,
350
                        to_date=date,simulation_state='delivered')
351 352
    self.assertEquals(total_quantity,quantity)

353
  def test_02_VariatedInventory(self, run=run_all_test):
354 355 356 357
    """
    Same thing as test_01 with variation
    """
    if not run: return
358 359
    self.logMessage('Test Variated Inventory')

360 361
    sequence_list = SequenceList()

362
    # Test with a variated inventory
363 364 365 366 367 368 369 370 371 372
    sequence_string = 'stepCreateVariatedResource \
                       stepCreateOrganisation1 \
                       stepCreateInitialMovements \
                       stepTic \
                       stepCreateFirstVariatedInventory \
                       stepTic \
                       stepCheckFirstVariatedInventory \
                       stepCreateSecondVariatedInventory \
                       stepTic \
                       stepCheckSecondVariatedInventory'
373 374 375 376
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)

377 378 379 380 381 382 383 384 385 386 387 388 389 390
  def stepCreateItem(self, start_date=None,quantity=None,
                                             sequence=None,**kw):
    """
    Create an Apparel Bath
    """
    portal = self.getPortal()
    item_list = sequence.get('item_list',[])
    item_module = portal.getDefaultModule(self.item_portal_type)
    item = item_module.newContent(portal_type=self.item_portal_type)
    item_list.append(item)
    sequence.edit(item_list=item_list)

  def stepCreateFirstVariatedAggregatedInventory(self, sequence=None, sequence_list=None, \
                                 **kw):
391
    """
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    We will put default values for an inventory
    - size/Child/32 99
    - size/Child/34 100
    - size/Child/32 99    item1,item2
    - size/Child/34 100   item1,item2
    """
    portal = self.getPortal()
    date = DateTime(self.first_date_string)
    inventory = self.createInventory(start_date=date,sequence=sequence)
    quantity = self.default_quantity
    self.createVariatedInventoryLine(start_date=date,
                  sequence=sequence, quantity=quantity)
    item_list = sequence.get('item_list')
    self.createVariatedInventoryLine(start_date=date,
                  sequence=sequence, quantity=quantity,
                  item_list=item_list)

  def getAggregateRelativeUrlText(self,item_list):
    relative_url_list = ['aggregate/%s' % x.getRelativeUrl() for x in item_list]
    relative_url_list.sort()
    relative_url_text = '\n'.join(relative_url_list)
    return relative_url_text

415 416
  def stepCheckFirstVariatedAggregatedInventory(self, start_date=None,
                                quantity=None, sequence=None, **kw):
417
    node_uid = sequence.get('organisation1').getUid()
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    total_quantity = (99 + 100) * 2
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date)
    self.assertEquals(total_quantity,quantity)
    variation_text = 'size/Child/32'
    total_quantity = (99) * 2
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        variation_text=variation_text,
                        to_date=date)
    self.assertEquals(total_quantity,quantity)
    # Also check when we look stock for a particular aggregate
433
    sub_variation_text = self.getAggregateRelativeUrlText(
434 435 436 437 438 439 440 441
                                            sequence.get('item_list'))
    total_quantity = 99
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        variation_text=variation_text,
                        to_date=date,
                        sub_variation_text=sub_variation_text)
    self.assertEquals(total_quantity,quantity)
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  
  def stepCheckExplanationTextInInventoryList(self, start_date=None,
                                quantity=None, sequence=None, **kw):
    """Tests getExplanationText from InventoryBrain
    """
    # this is rather a test for InventoryBrain
    node_uid = sequence.get('organisation1').getUid()
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    for inventory_brain in self.getSimulationTool().getInventoryList(
                        node_uid=node_uid,
                        resource=resource_url,
                        to_date=date):
      self.assertNotEquals(inventory_brain.getExplanationText(),
                           'Unknown')
      self.assertNotEquals(inventory_brain.getListItemUrl(
                                'getExplanationText',
                                0,
                                'dummy_selection_name'), '')
      
  def stepCreateSecondVariatedAggregatedInventory(self, sequence=None,
                                      sequence_list=None, **kw):
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
    """
    We will put default values for an inventory
    - size/Child/32 89    item1,item2
    - size/Child/34 90    item1,item2
    - size/Child/32 89    item1
    - size/Child/34 90    item1
    """
    portal = self.getPortal()
    date = DateTime(self.second_date_string)
    inventory = self.createInventory(start_date=date,sequence=sequence)
    quantity = self.default_quantity - 10
    item_list = sequence.get('item_list')
    self.createVariatedInventoryLine(start_date=date,
                  sequence=sequence, quantity=quantity)
    item_list = sequence.get('item_list')[:1]
    self.createVariatedInventoryLine(start_date=date,
                  sequence=sequence, quantity=quantity,
                  item_list=item_list)

483 484
  def stepCheckSecondVariatedAggregatedInventory(self, start_date=None,
                                quantity=None, sequence=None, **kw):
485
    node_uid = sequence.get('organisation1').getUid()
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
    resource_url = sequence.get('resource').getRelativeUrl()
    date = DateTime(self.view_stock_date)
    total_quantity = (89 + 90) * 2
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date)
    self.assertEquals(total_quantity,quantity)
    variation_text = 'size/Child/32'
    total_quantity = (89) * 2
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        variation_text=variation_text,
                        to_date=date,simulation_state='delivered')
    self.assertEquals(total_quantity,quantity)
    # Also check when we look stock for a particular aggregate
501 502
    sub_variation_text = self.getAggregateRelativeUrlText(
                                                sequence.get('item_list'))
503 504 505 506 507 508 509
    total_quantity = 0
    quantity = self.getSimulationTool().getInventory(node_uid=node_uid,
                        resource=resource_url,
                        to_date=date,
                        sub_variation_text=sub_variation_text,
                        simulation_state='delivered')
    self.assertEquals(total_quantity,quantity)
510 511
    sub_variation_text = self.getAggregateRelativeUrlText(
                                    sequence.get('item_list')[:1])
512 513 514 515

  def test_03_VariatedAggregatedInventory(self, run=run_all_test):
    """
    Same thing as test_01 with variation and aggregate
516 517
    """
    if not run: return
518 519
    self.logMessage('Test Variated Aggregated Inventory')

520 521
    sequence_list = SequenceList()

522
    # Test with a variated inventory with some aggregate
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    sequence_string = 'stepCreateVariatedResource \
                       stepCreateOrganisation1 \
                       stepCreateInitialMovements \
                       stepTic \
                       stepCreateItem \
                       stepCreateItem \
                       stepCreateFirstVariatedAggregatedInventory \
                       stepTic \
                       stepCheckFirstVariatedAggregatedInventory \
                       stepCreateSecondVariatedAggregatedInventory \
                       stepTic \
                       stepCheckSecondVariatedAggregatedInventory'
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)

  def test_04_VariatedAggregatedInventoryGetInventoryList(self, run=run_all_test):
    """
    Same thing as test_03 with testing getInventoryList columns
    """
    if not run: return
    self.logMessage('Test getInventoryList and Variated Aggregated Inventory')

    sequence_list = SequenceList()

    # Test with a variated inventory with some aggregate
    sequence_string = 'stepCreateVariatedResource \
                       stepCreateOrganisation1 \
                       stepCreateInitialMovements \
                       stepTic \
                       stepCreateItem \
                       stepCreateItem \
                       stepCreateFirstVariatedAggregatedInventory \
                       stepTic \
                       stepCheckFirstVariatedAggregatedInventory \
                       stepCheckExplanationTextInInventoryList \
                       stepCreateSecondVariatedAggregatedInventory \
                       stepTic \
                       stepCheckSecondVariatedAggregatedInventory \
                       stepCheckExplanationTextInInventoryList'
563 564 565 566
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)