testResource.py 43.2 KB
Newer Older
Romain Courteaud's avatar
Romain Courteaud committed
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
##############################################################################
#
# Copyright (c) 2005 Nexedi SARL and Contributors. All Rights Reserved.
#          Romain Courteaud <romain@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.
#
##############################################################################

29
import unittest
30
import transaction
Romain Courteaud's avatar
Romain Courteaud committed
31 32
from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
33
from AccessControl.SecurityManagement import newSecurityManager
Romain Courteaud's avatar
Romain Courteaud committed
34
from zLOG import LOG
35
from Products.ERP5Type.tests.Sequence import SequenceList
36
from DateTime import DateTime
37

Romain Courteaud's avatar
Romain Courteaud committed
38 39 40 41 42 43

class TestResource(ERP5TypeTestCase):
  """
    Test ERP5 document Resource
  """
  run_all_test = 1
44
  quiet = 0
Romain Courteaud's avatar
Romain Courteaud committed
45 46 47

  # Global variables
  resource_portal_type = 'Apparel Model'
48
  product_portal_type = 'Product'
49
  node_portal_type = 'Organisation'
50
  sale_supply_portal_type = 'Sale Supply'
51
  sale_order_line_portal_type = 'Sale Order Line'
52
  sale_supply_line_portal_type = 'Sale Supply Line'
53
  purchase_supply_line_portal_type = 'Purchase Supply Line'
54
  sale_supply_cell_portal_type = 'Sale Supply Cell'
Kevin Deldycke's avatar
Kevin Deldycke committed
55
  variation_base_category_list = ['colour', 'size', 'morphology',
56
                                  'industrial_phase']
Kevin Deldycke's avatar
Kevin Deldycke committed
57
  size_list = ['size/Child','size/Man']
58
  variation_property_list = []
Romain Courteaud's avatar
Romain Courteaud committed
59 60 61 62 63

  def getBusinessTemplateList(self):
    """
      Install needed business template
    """
64
    # Trade is needeed for pricing
65
    return ('erp5_base', 'erp5_pdm', 'erp5_trade', 'erp5_apparel', )
Romain Courteaud's avatar
Romain Courteaud committed
66 67 68 69 70 71 72 73 74 75

  def getTitle(self):
    return "Resource"

  def login(self):
    uf = self.getPortal().acl_users
    uf._doAddUser('rc', '', ['Manager'], [])
    user = uf.getUserById('rc').__of__(uf)
    newSecurityManager(None, user)

76
  def setUpPreferences(self):
Fabien Morin's avatar
Fabien Morin committed
77 78 79 80 81 82 83 84 85
    #create apparel variation preferences
    portal_preferences = self.getPreferenceTool()
    preference = getattr(portal_preferences, 'test_site_preference', None)
    if preference is None:
      preference = portal_preferences.newContent(portal_type='System Preference',
                                title='Default Site Preference',
                                id='test_site_preference')
      if preference.getPreferenceState() == 'disabled':
        preference.enable()
86

Fabien Morin's avatar
Fabien Morin committed
87 88 89 90 91
    preference.setPreferredApparelModelVariationBaseCategoryList(('colour', 'size', 'morphology', 'industrial_phase',))
    preference.setPreferredApparelClothVariationBaseCategoryList(('size',))
    preference.setPreferredApparelComponentVariationBaseCategoryList(('variation',))
    if preference.getPreferenceState() == 'disabled':
      preference.enable()
92 93 94
    transaction.commit()
    self.tic()

Romain Courteaud's avatar
Romain Courteaud committed
95 96 97 98 99
  def afterSetUp(self):
    self.login()
    self.portal = self.getPortal()
    self.category_tool = self.getCategoryTool()
    self.createCategories()
100
    self.setUpPreferences()
Romain Courteaud's avatar
Romain Courteaud committed
101

Jérome Perrin's avatar
Jérome Perrin committed
102
  def beforeTearDown(self):
103
    transaction.abort()
Jérome Perrin's avatar
Jérome Perrin committed
104 105 106 107 108 109 110 111
    for folder in (
          self.portal.getDefaultModule(self.resource_portal_type),
          self.portal.getDefaultModule(self.sale_supply_portal_type),
          self.portal.getDefaultModule("Currency"),
          self.portal.getDefaultModule(self.node_portal_type),
          self.portal.getDefaultModule("Sale Order"),
          self.portal.getDefaultModule("Purchase Order"),):
      folder.manage_delObjects([i for i in folder.objectIds()])
112
    transaction.commit()
Jérome Perrin's avatar
Jérome Perrin committed
113 114
    self.tic()

Romain Courteaud's avatar
Romain Courteaud committed
115
  def createCategories(self):
Kevin Deldycke's avatar
Kevin Deldycke committed
116 117
    """
      Light install create only base categories, so we create
Romain Courteaud's avatar
Romain Courteaud committed
118 119 120 121 122 123 124
      some categories for testing them
    """
    size_category_list = ['Baby', 'Child', 'Man', 'Woman']
    if len(self.category_tool.size.contentValues()) == 0 :
      for category_id in size_category_list:
        o = self.category_tool.size.newContent(portal_type='Category',
                                               id=category_id)
Kevin Deldycke's avatar
Kevin Deldycke committed
125
    self.size_category_list = map(lambda x: 'size/%s' % x,
Romain Courteaud's avatar
Romain Courteaud committed
126 127 128 129 130 131 132
                                  size_category_list)

    colour_category_list = ['blue', 'green']
    if len(self.category_tool.colour.contentValues()) == 0 :
      for category_id in colour_category_list:
        o = self.category_tool.colour.newContent(portal_type='Category',
                                               id=category_id)
Kevin Deldycke's avatar
Kevin Deldycke committed
133
    self.colour_category_list = map(lambda x: 'colour/%s' % x,
Romain Courteaud's avatar
Romain Courteaud committed
134 135
                                    colour_category_list)

136 137 138 139 140 141 142
    ind_phase_category_list = ['phase1', 'phase2']
    if len(self.category_tool.industrial_phase.contentValues()) == 0:
      for category_id in ind_phase_category_list:
        o = self.category_tool.industrial_phase.newContent(
                                               portal_type='Category',
                                               id=category_id)
    self.industrial_phase_category_list = map(
Kevin Deldycke's avatar
Kevin Deldycke committed
143
                                    lambda x: 'industrial_phase/%s' % x,
144 145
                                    ind_phase_category_list)

Romain Courteaud's avatar
Romain Courteaud committed
146 147 148 149
    self.morphology_category_list = []
    self.base_category_content_list = {
      'size':self.size_category_list,
      'colour':self.colour_category_list,
150
      'morphology':self.morphology_category_list,
151
      'industrial_phase':self.industrial_phase_category_list
Romain Courteaud's avatar
Romain Courteaud committed
152 153
    }

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    quantity_unit_weight = self.portal.portal_categories.quantity_unit._getOb(
                                        'weight', None)
    if quantity_unit_weight is None:
      quantity_unit_weight = self.portal.portal_categories.quantity_unit\
                                  .newContent(id='weight',
                                              portal_type='Category')
    self.quantity_unit_gram = quantity_unit_weight._getOb('gram', None)
    if self.quantity_unit_gram is None:
      self.quantity_unit_gram = quantity_unit_weight.newContent(
                                      portal_type='Category',
                                      quantity=0.001,
                                      id='gram')
    self.quantity_unit_kilo = quantity_unit_weight._getOb('kilo', None)
    if self.quantity_unit_kilo is None:
      self.quantity_unit_kilo = quantity_unit_weight.newContent(
                                      portal_type='Category',
                                      quantity=1,
                                      id='kilo')


Romain Courteaud's avatar
Romain Courteaud committed
174 175 176 177 178 179 180 181 182 183 184 185 186
  def stepTic(self,**kw):
    self.tic()

  def stepCreateResource(self, sequence=None, sequence_list=None, **kw):
    """
      Create a resource without variation
    """
    resource_module = self.portal.getDefaultModule(self.resource_portal_type)
    resource = resource_module.newContent( \
                                 portal_type=self.resource_portal_type)
    resource.edit(
      title = "Resource"
    )
187 188
    sequence.edit(resource=resource,
                  variation_property_list=[])
Romain Courteaud's avatar
Romain Courteaud committed
189 190 191 192 193
    self.category_list = []
    # Actually, resource has no individual variation
    for base_category in resource.getVariationBaseCategoryList():
      sequence.edit(**{base_category:None})

Kevin Deldycke's avatar
Kevin Deldycke committed
194
  def stepCheckGetVariationBaseCategoryList(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
195 196 197 198 199 200 201 202
                                             sequence_list=None, **kw):
    """
      Check if getVariationBaseCategoryList returns the good result
    """
    resource = sequence.get('resource')
    vbcl = resource.getVariationBaseCategoryList()
    self.failIfDifferentSet(self.variation_base_category_list, vbcl)

Kevin Deldycke's avatar
Kevin Deldycke committed
203
  def stepCheckGetVariationRangeCategoryList(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                                             sequence_list=None, **kw):
    """
      Check if getVariationRangeCategoryList returns the good result
    """
    resource = sequence.get('resource')
    vbcl = resource.getVariationBaseCategoryList()
    correct_variation_range_category_list = []
    for base_category in vbcl:
      # Check if resource has individual variations
      individual_variation_list = sequence.get(base_category)
      if individual_variation_list is None:
        correct_variation_range_category_list.extend(
                               self.base_category_content_list[base_category])
      else:
        correct_variation_range_category_list.extend(individual_variation_list)

    vrcl = resource.getVariationRangeCategoryList()
    self.failIfDifferentSet(correct_variation_range_category_list, vrcl)

  def stepSetCategoryVariation(self, sequence=None, sequence_list=None, **kw):
    """
      Set category variation to current resource
    """
    resource = sequence.get('resource')
    size_list = map(lambda x: x[len('size/'):], self.size_list)
Kevin Deldycke's avatar
Kevin Deldycke committed
229
    resource.setSizeList(size_list)
Romain Courteaud's avatar
Romain Courteaud committed
230 231
    self.category_list = self.size_list[:]

Kevin Deldycke's avatar
Kevin Deldycke committed
232
  def stepSetIndividualVariationWithEmptyBase(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
233 234
                                              sequence_list=None, **kw):
    """
Alexandre Boeglin's avatar
Alexandre Boeglin committed
235 236
    Set the individual variation of the current resource to a base category
    that contains no subobjects.
Romain Courteaud's avatar
Romain Courteaud committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    """
    resource = sequence.get('resource')
    morphology_list = []
    morphology_variation_count = 2
    for i in range(morphology_variation_count) :
      variation_portal_type = 'Apparel Model Morphology Variation'
      variation = resource.newContent(portal_type=variation_portal_type)
      variation.edit(
        title = 'MorphologyVariation%s' % str(i)
      )
      morphology_list.append('morphology/%s' %
                                        variation.getRelativeUrl())
    # store individual resource
    sequence.edit(morphology=morphology_list)

Kevin Deldycke's avatar
Kevin Deldycke committed
252
  def stepSetIndividualVariationWithFillBase(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
253 254
                                              sequence_list=None, **kw):
    """
Alexandre Boeglin's avatar
Alexandre Boeglin committed
255 256
    Set the individual variation of the current resource to a base category
    that contains some subobjects.
Romain Courteaud's avatar
Romain Courteaud committed
257 258 259
    """
    resource = sequence.get('resource')
    colour_list = []
Kevin Deldycke's avatar
Kevin Deldycke committed
260
    colour_variation_count = 1
Romain Courteaud's avatar
Romain Courteaud committed
261 262 263 264 265 266 267 268 269 270
    for i in range(colour_variation_count) :
      variation_portal_type = 'Apparel Model Colour Variation'
      variation = resource.newContent(portal_type=variation_portal_type)
      variation.edit(
        title = 'ColourVariation%s' % str(i)
      )
      colour_list.append('colour/%s' % variation.getRelativeUrl())
    # store individual resource
    sequence.edit(colour=colour_list)

271
  def test_01_getVariationBaseCategoryList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
272 273 274 275 276 277 278 279 280 281 282
    """
      Test the method getVariationBaseCategoryList on a resource.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test when resource has no variation
    sequence_string = '\
                      CreateResource \
                      CheckGetVariationBaseCategoryList \
                      '
    sequence_list.addSequenceString(sequence_string)
283
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
284

285
  def genericTest(self, test_method_name, quiet=quiet):
Romain Courteaud's avatar
Romain Courteaud committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
    """
      Generic test on a resource.
    """
    sequence_list = SequenceList()
    # Test when resource has no variation
    sequence_string = '\
                      CreateResource \
                      %s \
                      ' % test_method_name
    sequence_list.addSequenceString(sequence_string)
    # Test when resource has category variations
    sequence_string = '\
                      CreateResource \
                      SetCategoryVariation \
                      %s \
                      ' % test_method_name
    sequence_list.addSequenceString(sequence_string)
    # Test when resource has individual variation and base category
    # has no content
    sequence_string = '\
                      CreateResource \
                      SetIndividualVariationWithEmptyBase \
                      Tic \
                      %s \
                      ' % test_method_name
    sequence_list.addSequenceString(sequence_string)
    # Test when resource has individual variation and base category
    # has category content
    sequence_string = '\
                      CreateResource \
                      SetIndividualVariationWithFillBase \
                      Tic \
                      %s \
                      ' % test_method_name
    sequence_list.addSequenceString(sequence_string)
    # Test with all cases
    sequence_string = '\
                      CreateResource \
                      SetCategoryVariation \
                      SetIndividualVariationWithEmptyBase \
                      SetIndividualVariationWithFillBase \
                      Tic \
                      %s \
                      ' % test_method_name
    sequence_list.addSequenceString(sequence_string)
331
    sequence_list.play(self, quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
332

333
  def test_02_getVariationRangeCategoryList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
334 335 336 337
    """
      Test the method getVariationRangeCategoryList on a resource.
    """
    if not run: return
338
    self.genericTest('CheckGetVariationRangeCategoryList', quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
339

Kevin Deldycke's avatar
Kevin Deldycke committed
340
  def stepCheckGetVariationRangeCategoryItemList(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
341 342 343 344 345 346 347 348 349 350 351
                                                 sequence_list=None, **kw):
    """
      Check if getVariationRangeCategoryItemList returns the good result.
      Does not test display...
      Item are left display.
    """
    resource = sequence.get('resource')
    vrcl = resource.getVariationRangeCategoryList()
    vrcil = resource.getVariationRangeCategoryItemList()
    self.failIfDifferentSet(vrcl, map(lambda x: x[1], vrcil))

352
  def test_03_getVariationRangeCategoryItemList(self, quiet=quiet,
Romain Courteaud's avatar
Romain Courteaud committed
353 354 355 356 357
                                                run=run_all_test):
    """
      Test the method getVariationRangeCategoryItemList on a resource.
    """
    if not run: return
358
    self.genericTest('CheckGetVariationRangeCategoryItemList', quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
359

Kevin Deldycke's avatar
Kevin Deldycke committed
360
  def stepCheckGetVariationCategoryList(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
361 362
                                                 sequence_list=None, **kw):
    """
Alexandre Boeglin's avatar
Alexandre Boeglin committed
363 364
    Check if getVariationCategoryList returns the good result, with default
    value for omit_individual_variation parameter
Romain Courteaud's avatar
Romain Courteaud committed
365 366 367 368 369
    """
    resource = sequence.get('resource')
    vcl = resource.getVariationCategoryList()
    self.failIfDifferentSet(self.category_list, vcl)

370
  def test_04_getVariationCategoryList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
371 372 373 374
    """
      Test the method getVariationCategoryList on a resource.
    """
    if not run: return
375
    self.genericTest('CheckGetVariationCategoryList', quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
376

Kevin Deldycke's avatar
Kevin Deldycke committed
377
  def stepCheckGetVariationCategoryListWithoutOmit(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
                                                 sequence_list=None, **kw):
    """
      Check if getVariationCategoryList returns the good result,
      with parameter omit_individual_variation=0.
    """
    resource = sequence.get('resource')
    vcl = resource.getVariationCategoryList(omit_individual_variation=0)
    correct_vcl = self.category_list[:]

    for base_category in resource.getVariationBaseCategoryList():
      # Check if resource has individual variations
      individual_variation_list = sequence.get(base_category)
      if individual_variation_list is not None:
        correct_vcl.extend(individual_variation_list)
    self.failIfDifferentSet(correct_vcl, vcl)

394
  def test_05_getVariationCategoryList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
395 396 397 398 399
    """
      Test the method getVariationCategoryList on a resource
      with parameter omit_individual_variation=0.
    """
    if not run: return
400
    self.genericTest('CheckGetVariationCategoryListWithoutOmit', quiet)
Romain Courteaud's avatar
Romain Courteaud committed
401

Kevin Deldycke's avatar
Kevin Deldycke committed
402
  def stepCheckGetVariationCategoryItemList(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
403 404 405 406 407 408 409 410 411 412
                                                 sequence_list=None, **kw):
    """
      Check if getVariationCategoryItemList returns the good result,
      with parameter omit_individual_variation=1.
    """
    resource = sequence.get('resource')
    vcl = resource.getVariationCategoryList()
    vcil = resource.getVariationCategoryItemList()
    self.failIfDifferentSet(vcl, map(lambda x: x[1], vcil))

413
  def test_06_getVariationCategoryItemList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
414 415 416 417
    """
      Test the method getVariationCategoryItemList on a resource.
    """
    if not run: return
418
    self.genericTest('CheckGetVariationCategoryItemList', quiet)
Romain Courteaud's avatar
Romain Courteaud committed
419

Kevin Deldycke's avatar
Kevin Deldycke committed
420
  def stepCheckGetVariationCategoryItemListWithoutOmit(self, sequence=None,
Romain Courteaud's avatar
Romain Courteaud committed
421 422 423 424 425 426 427 428 429 430
                                                 sequence_list=None, **kw):
    """
      Check if getVariationCategoryItemList returns the good result,
      with parameter omit_individual_variation=0.
    """
    resource = sequence.get('resource')
    vcl = resource.getVariationCategoryList(omit_individual_variation=0)
    vcil = resource.getVariationCategoryItemList(omit_individual_variation=0)
    self.failIfDifferentSet(vcl, map(lambda x: x[1], vcil))

431
  def test_07_getVariationCategoryItemList(self, quiet=quiet, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
432 433 434 435 436
    """
      Test the method getVariationCategoryItemList on a resource
      with parameter omit_individual_variation=0.
    """
    if not run: return
437 438
    self.genericTest('CheckGetVariationCategoryItemListWithoutOmit',
        quiet=quiet)
Romain Courteaud's avatar
Romain Courteaud committed
439

Kevin Deldycke's avatar
Kevin Deldycke committed
440
  def stepCheckGetVariationPropertyList(self, sequence=None,
441 442 443 444 445 446 447 448 449
                                        sequence_list=None, **kw):
    """
      Check if GetVariationPropertyList exists on a resource.
    """
    resource = sequence.get('resource')
    vpl = sequence.get('variation_property_list')
    self.failIfDifferentSet(resource.getVariationPropertyList(),
                            vpl)

Kevin Deldycke's avatar
Kevin Deldycke committed
450
  def stepCheckSetVariationPropertyList(self, sequence=None,
451 452 453 454 455 456 457 458 459 460 461 462
                                        sequence_list=None, **kw):
    """
      Check if SetVariationPropertyList exists on a resource.
      And test it.
    """
    resource = sequence.get('resource')
    vpl = ['prop1', 'prop2']
    sequence.edit(variation_property_list=vpl)
    resource.setVariationPropertyList(vpl)
    self.failIfDifferentSet(resource.variation_property_list,
                            vpl)

463
  def test_08_variationPropertyList(self, quiet=quiet, run=run_all_test):
464 465 466 467 468 469 470 471 472 473 474 475 476
    """
      Simply test if method are well generated by the property sheet.
    """
    if not run: return
    sequence_list = SequenceList()
    # Test when resource has no variation
    sequence_string = '\
                      CreateResource \
                      CheckGetVariationPropertyList \
                      CheckSetVariationPropertyList \
                      CheckGetVariationPropertyList \
                      '
    sequence_list.addSequenceString(sequence_string)
477
    sequence_list.play(self, quiet=quiet)
478

479 480 481 482 483 484 485
  def getPriceConfig(self):
    """
    Define somes cases of pricing configuration to test.
    """
    config = [
      {
        'base_price': None,
486 487 488
        'additional_price': None,
        'surcharge_ratio': None,
        'discount_ratio': None,
489 490 491 492
        'exclusive_discount_ratio': None,
        'price': None,
      },{
        'base_price': 5,
493 494 495
        'additional_price': None,
        'surcharge_ratio': None,
        'discount_ratio': None,
496 497 498 499
        'exclusive_discount_ratio': None,
        'price': 5,
      },{
        'base_price': 5,
500 501 502
        'additional_price': 1,
        'surcharge_ratio': None,
        'discount_ratio': None,
503 504 505 506
        'exclusive_discount_ratio': None,
        'price': 6,
      },{
        'base_price': 5,
507 508 509
        'additional_price': 3,
        'surcharge_ratio': 0.5,
        'discount_ratio': None,
510 511 512 513
        'exclusive_discount_ratio': None,
        'price': 12,
      },{
        'base_price': 5,
514 515 516
        'additional_price': 3,
        'surcharge_ratio': None,
        'discount_ratio': 0.25,
517 518 519 520
        'exclusive_discount_ratio': None,
        'price': 6,
      },{
        'base_price': 5,
521 522 523
        'additional_price': 3,
        'surcharge_ratio': None,
        'discount_ratio': None,
524 525 526 527
        'exclusive_discount_ratio': 0.5,
        'price': 4,
      },{
        'base_price': 5,
528 529 530
        'additional_price': 3,
        'surcharge_ratio': None,
        'discount_ratio': 0.5,
531 532 533 534
        'exclusive_discount_ratio': 0.75,
        'price': 2,
      },{
        'base_price': 5,
535 536 537
        'additional_price': 3,
        'surcharge_ratio': None,
        'discount_ratio': 0.75,
538 539 540 541
        'exclusive_discount_ratio': 0.25,
        'price': 2,
      },{
        'base_price': 5,
542 543 544
        'additional_price': 3,
        'surcharge_ratio': 1,
        'discount_ratio': 0.75,
545
        'exclusive_discount_ratio': 0.25,
546
        'price': 4,
547 548
      },{
        'base_price': None,
549 550 551
        'additional_price': 3,
        'surcharge_ratio': 1,
        'discount_ratio': 0.75,
552 553 554 555 556 557 558 559 560 561
        'exclusive_discount_ratio': 0.25,
        'price': None,
      }
    ]
    return config

  def logMessage(self, msg, tab=0):
    """
    Log a message.
    """
562 563
    if self.quiet:
      return
564 565 566 567 568
    if tab:
      msg = '  %s' % msg
    ZopeTestCase._print('\n%s' % msg)
    LOG('testResource.play', 0, msg)

569
  def test_09_getPrice(self, quiet=quiet, run=run_all_test):
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    """
    Test the pricing model.
    """
    if not run: return
    config_list = self.getPriceConfig()
    for i in range(0, len(config_list)):
      self.logMessage("Starting New Pricing Case %i..." % i)
      config = config_list[i]
      # Create product
      self.logMessage("Creating product...", tab=1)
      product_module = self.portal.getDefaultModule(self.product_portal_type)
      product = product_module.newContent( \
                                   portal_type=self.product_portal_type,
                                   title='Product%i' % i)
      # Configure pricing parameters
      for key, value in config.items():
        if key != 'price':
          if value not in [None, []]:
            if type(value) != type([]):
              value_list = [value]
            else:
              value_list = value
            # Create requested supply line
            for pricing_param in value_list:
              self.logMessage("Creating supply line...", tab=1)
              supply_line = product.newContent(
596
                    portal_type=self.sale_supply_line_portal_type)
597
              # Set pricing parameter
598 599
              self.logMessage("Set %s on supply line with value %s..." % \
                              (key, str(pricing_param)), tab=1)
600 601 602
              supply_line.setProperty(key, pricing_param)
      # Commit transaction
      self.logMessage("Commit transaction...", tab=1)
603
      transaction.commit()
604 605 606 607 608 609 610
      # Tic
      self.logMessage("Tic...", tab=1)
      self.tic()
      # Check resource price
      self.logMessage("Check product price...", tab=1)
      self.assertEquals(config['price'], product.getPrice())

611
  def test_10_getPriceWithOptions(self, quiet=quiet, run=run_all_test):
612 613 614 615 616 617
    """
    Test the pricing model on a resource with options.
    """
    if not run: return
    i = 1
    self.logMessage("Starting New Option Pricing Case %i..." % i)
618 619 620 621
    # Fill the PDM preferences
    preference = self.portal.portal_preferences.default_site_preference
    preference.setPreferredProductOptionalVariationBaseCategoryList(['industrial_phase'])
    preference.enable()
622
    transaction.commit()
623
    self.tic()
Kevin Deldycke's avatar
Kevin Deldycke committed
624
    # Create another product/supply, in order to be sure that the
625 626 627 628 629 630 631 632 633
    # nothing will be generated from this supply!
    self.logMessage("Creating fake product...", tab=1)
    product_module = self.portal.getDefaultModule(self.product_portal_type)
    product = product_module.newContent( \
                                 portal_type=self.product_portal_type,
                                 title='FakeProduct%i' % i)
    product.setVariationCategoryList(self.industrial_phase_category_list)
    self.logMessage("Creating supply line...", tab=1)
    supply_line = product.newContent(
634
          portal_type=self.sale_supply_line_portal_type)
635 636 637 638
    supply_line.setProperty('base_price', 100)
    supply_line.setSurchargeRatioQuantityStepList([])
    supply_line.getCellKeyList(base_id='path_optional_surcharge_ratio')
    cell1 = supply_line.newCell('industrial_phase/phase1',
639
        base_id='path_optional_surcharge_ratio', 
640
        portal_type=self.sale_supply_cell_portal_type)
641 642 643 644
    cell1.setSurchargeRatio(20)
    cell1.setMappedValuePropertyList(["surcharge_ratio"])
    cell1.setMembershipCriterionBaseCategory('industrial_phase')
    cell1.setMembershipCriterionCategory('industrial_phase/phase1')
645 646 647 648 649 650 651 652 653 654 655
    # Create product
    self.logMessage("Creating product...", tab=1)
    product_module = self.portal.getDefaultModule(self.product_portal_type)
    product = product_module.newContent( \
                                 portal_type=self.product_portal_type,
                                 title='Product%i' % i)
    # Select some options on the resource
    product.setVariationCategoryList(self.industrial_phase_category_list)
    # Create requested supply line
    self.logMessage("Creating supply line...", tab=1)
    supply_line = product.newContent(
656
          portal_type=self.sale_supply_line_portal_type)
657 658 659 660 661 662
    # Set pricing parameter
    supply_line.setProperty('base_price', 1)
    # Define the additional price matrix range
    supply_line.setAdditionalPriceQuantityStepList([])
    supply_line.getCellKeyList(base_id='path_optional_additional_price')
    cell1 = supply_line.newCell('industrial_phase/phase1',
663
        base_id='path_optional_additional_price', 
664
        portal_type=self.sale_supply_cell_portal_type)
665
    cell1.setAdditionalPrice(2)
666 667 668
    cell1.setMappedValuePropertyList(["additional_price"])
    cell1.setMembershipCriterionBaseCategory('industrial_phase')
    cell1.setMembershipCriterionCategory('industrial_phase/phase1')
669
    cell2 = supply_line.newCell('industrial_phase/phase2',
670
        base_id='path_optional_additional_price', 
671
        portal_type=self.sale_supply_cell_portal_type)
672
    cell2.setAdditionalPrice(7)
673 674 675
    cell2.setMappedValuePropertyList(["additional_price"])
    cell2.setMembershipCriterionBaseCategory('industrial_phase')
    cell2.setMembershipCriterionCategory('industrial_phase/phase2')
676 677
    # Commit transaction
    self.logMessage("Commit transaction...", tab=1)
678
    transaction.commit()
679 680 681 682
    # Tic
    self.logMessage("Tic...", tab=1)
    self.tic()
    # Check resource price
683
    self.logMessage("Check product price without option...", tab=1)
684 685
    self.assertEquals(1, product.getPrice(context=supply_line))
    # Check resource option price
686 687
    self.logMessage("Check product price with option: %s..." % \
                    'industrial_phase/phase1', tab=1)
688 689
    self.assertEquals(3, product.getPrice(
                                   categories=['industrial_phase/phase1']))
690 691
    self.logMessage("Check product price with option: %s..." % \
                    'industrial_phase/phase2', tab=1)
692 693
    self.assertEquals(8, product.getPrice(
                                   categories=['industrial_phase/phase2']))
694 695 696 697 698
    self.logMessage("Check product price with options: %s..." % \
                    'industrial_phase/phase1 industrial_phase/phase2', tab=1)
    self.assertEquals(10, product.getPrice(
                                   categories=['industrial_phase/phase1',
                                               'industrial_phase/phase2']))
699

700
  def test_11_getPriceWithDestinationSection(self, quiet=quiet, run=run_all_test):
701 702
    """
    Test the pricing model with multiple price for 
703
    differents destination sections.
704 705 706 707 708 709
    """
    if not run: return
    # Initialize variables
    test_case_list = []
    # Create product
    product_module = self.portal.getDefaultModule(self.product_portal_type)
710
    supply_module = self.portal.getDefaultModule(self.sale_supply_portal_type)
711 712 713
    # Create generic supply
    self.logMessage("Creating generic fake supply ...", tab=1)
    generic_supply = supply_module.newContent(
714
                     portal_type=self.sale_supply_portal_type,
715
                     title='FakeGenericSupply',)
716 717
    # Create empty supply line
    supply_line = generic_supply.newContent(
718
          portal_type=self.sale_supply_line_portal_type)
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    supply_line.setProperty('base_price', 0)
    for j in range(33, 35):
      self.logMessage("Creating fake product %s..." % j, tab=1)
      product = product_module.newContent(
                           portal_type=self.product_portal_type,
                           title='AnotherFakeProduct%s' % j)
      # Create some nodes
      node_module = self.portal.getDefaultModule(self.node_portal_type)
      for i in range(11, 14):
        self.logMessage("Creating fake node %s..." % i, tab=1)
        node = node_module.newContent(
                       portal_type=self.node_portal_type,
                       title='FakeNode%s%s' % (j, i))
        # Create a supply
        self.logMessage("Creating fake supply %s..." % i, tab=1)
        supply = supply_module.newContent(
735
                                     portal_type=self.sale_supply_portal_type,
736
                                     title='FakeSupply%s' % i,
737
                                     destination_section_value=node)
738 739
        self.logMessage("Creating fake supply line %s..." % i, tab=1)
        supply_line = supply.newContent(
740
              portal_type=self.sale_supply_line_portal_type,
741 742 743 744 745 746 747 748 749
              resource_value=product)
        # Set pricing parameter
        base_price = i*j
        supply_line.setProperty('base_price', base_price)
        # Register the case
        test_case_list.append((product, node, base_price))
      # Create generic supply line
      self.logMessage("Creating generic fake supply line ...", tab=1)
      supply_line = generic_supply.newContent(
750
            portal_type=self.sale_supply_line_portal_type,
751 752 753 754 755
            resource_value=product)
      supply_line.setProperty('base_price', j)
      test_case_list.append((product, None, j))
    # Commit transaction
    self.logMessage("Commit transaction...", tab=1)
756
    transaction.commit()
757 758 759 760 761 762
    # Tic
    self.logMessage("Tic...", tab=1)
    self.tic()
    # Test the cases
    for product, node, base_price in test_case_list:
      if node is not None:
763
        self.logMessage("Check product %s with destination section %s" % \
764 765 766 767
                        (product.getTitle(), node.getTitle()),
                        tab=1)
        self.assertEquals(base_price, 
                          product.getPrice(
768
                    categories=['destination_section/%s' % node.getRelativeUrl()]))
769
      else:
770
        self.logMessage("Check product %s without destination section" % \
771 772 773 774
                        product.getTitle(),
                        tab=1)
        self.assertEquals(base_price, 
                          product.getPrice())
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790

  def test_11b_getPriceWithCells(self, quiet=quiet, run=run_all_test):
    """
    Test the pricing model with multiple price for 
    differents destination sections, using supply cells
    """
    if not run: return
    # Initialize variables
    test_case_list = []
    # Create product
    product_module = self.portal.getDefaultModule(self.product_portal_type)
    supply_module = self.portal.getDefaultModule(self.sale_supply_portal_type)
    # Create generic supply
    self.logMessage("Creating generic fake supply ...", tab=1)
    generic_supply = supply_module.newContent(
                     portal_type=self.sale_supply_portal_type,
791
                     title='FakeGenericSupply',)
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
    # Create empty supply line
    supply_line = generic_supply.newContent(
          portal_type=self.sale_supply_line_portal_type)
    supply_line.setProperty('base_price', 0)
    for j in range(33, 35):
      self.logMessage("Creating fake product %s..." % j, tab=1)
      product = product_module.newContent(
                           portal_type=self.product_portal_type,
                           title='AnotherFakeProduct%s' % j)
      product.setVariationBaseCategoryList(['size'])
      product.setVariationCategoryList(['size/Baby', 'size/Man'])
      # Create some nodes
      node_module = self.portal.getDefaultModule(self.node_portal_type)
      for i in range(11, 14):
        self.logMessage("Creating fake node %s..." % i, tab=1)
        node = node_module.newContent(
                       portal_type=self.node_portal_type,
                       title='FakeNode%s%s' % (j, i))
        # Create a supply
        self.logMessage("Creating fake supply %s..." % i, tab=1)
        supply = supply_module.newContent(
                                     portal_type=self.sale_supply_portal_type,
                                     title='FakeSupply%s' % i,
                                     destination_section_value=node)

        if 0:
          # XXX if both a supply line for the resource and a supply cell for
          # the resource with the exact variation can be applied, one of them
          # is choosen randomly. It looks like a bug, but I'm not sure we
          # should handle such situation.
          self.logMessage("Creating wrong supply line %s..." % i, tab=1)
          wrong_supply_line = supply.newContent(
                portal_type=self.sale_supply_line_portal_type,
                resource_value=product)
          wrong_supply_line.setBasePrice(12454326)

        self.logMessage("Creating fake supply line %s..." % i, tab=1)
        supply_line = supply.newContent(
              portal_type=self.sale_supply_line_portal_type,
              resource_value=product)
        supply_line.setPVariationBaseCategoryList(['size'])
        supply_line.updateCellRange(base_id='path')

        baby_cell = supply_line.newCell('size/Baby',
                           portal_type=self.sale_supply_cell_portal_type)
        baby_cell.setVariationCategoryList(['size/Baby'])
        baby_cell.setPredicateCategoryList(['size/Baby'])
        baby_cell.setMappedValuePropertyList(['base_price'])
        baby_cell.setMembershipCriterionBaseCategory('size')
        baby_cell.setMembershipCriterionCategory('size/Baby')
        base_price = i*j
        baby_cell.setProperty('base_price', base_price)
        # Register the case
        test_case_list.append((product, 'size/Baby', node, base_price))

        man_cell = supply_line.newCell('size/Man',
                        portal_type=self.sale_supply_cell_portal_type)
        man_cell.setVariationCategoryList(['size/Man'])
        man_cell.setPredicateCategoryList(['size/Man'])
        man_cell.setMappedValuePropertyList(['base_price'])
        man_cell.setMembershipCriterionBaseCategory('size')
        man_cell.setMembershipCriterionCategory('size/Man')
        base_price = i*j+3
        man_cell.setProperty('base_price', base_price)
        # Register the case
        test_case_list.append((product, 'size/Man', node, base_price))

      # Create generic supply line
      self.logMessage("Creating generic fake supply line ...", tab=1)
      supply_line = generic_supply.newContent(
            portal_type=self.sale_supply_line_portal_type,
            resource_value=product)
      supply_line.setProperty('base_price', j)
      test_case_list.append((product, None, None, j))

    # Commit transaction
    self.logMessage("Commit transaction...", tab=1)
869
    transaction.commit()
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    # Tic
    self.logMessage("Tic...", tab=1)
    self.tic()
    # Test the cases
    for product, variation, node, base_price in test_case_list:
      if node is not None:
        self.logMessage("Check product %s with destination section %s" % \
                        (product.getTitle(), node.getTitle()),
                        tab=1)
        self.assertEquals(base_price,
             product.getPrice(
               categories=['destination_section/%s' % node.getRelativeUrl(),
                           variation]))
      else:
        self.logMessage("Check product %s without destination section" % \
                        product.getTitle(),
                        tab=1)
        self.assertEquals(base_price,
                          product.getPrice(categories=[variation]))
889
  
890

891
  def test_12_getPurchaseVsSalePrice(self, quiet=quiet, run=run_all_test):
892 893
    """
    Test the pricing model with purchase and sale supply lines, and with
894
    source_section/destination_section.
895 896 897 898 899 900 901 902
    """
    if not run: return
    # Initialize variables
    product_module = self.portal.getDefaultModule(self.product_portal_type)
    organisation_module = self.getOrganisationModule()
    currency_module = self.getCurrencyModule()
    sale_order_module = self.portal.getDefaultModule("Sale Order")
    purchase_order_module = self.portal.getDefaultModule("Purchase Order")
903
    # Create product
904 905 906 907 908 909 910 911 912 913 914 915 916 917
    product = product_module.newContent(
        portal_type=self.product_portal_type,
        title="yet another product")
    # Create organisations
    orga1 = organisation_module.newContent(
        portal_type="Organisation",
        title="orga1")
    orga2 = organisation_module.newContent(
        portal_type="Organisation",
        title="orga2")
    # Create sale supply lines
    product.newContent(
        portal_type=self.sale_supply_line_portal_type,
        base_price=100.0,
918
        destination_section_value=orga1)
919 920 921
    product.newContent(
        portal_type=self.sale_supply_line_portal_type,
        base_price=200.0,
922
        destination_section_value=orga2)
923 924 925 926 927 928 929
    product.newContent(
        portal_type=self.sale_supply_line_portal_type,
        base_price=400.0)
    # Create purchase supply lines
    product.newContent(
        portal_type=self.purchase_supply_line_portal_type,
        base_price=10.0,
930
        source_section_value=orga1)
931 932 933
    product.newContent(
        portal_type=self.purchase_supply_line_portal_type,
        base_price=20.0,
934
        source_section_value=orga2)
935 936 937 938 939 940 941 942 943
    product.newContent(
        portal_type=self.purchase_supply_line_portal_type,
        base_price=40.0)
    # Create sale order and check price
    sale_order = sale_order_module.newContent(
        portal_type="Sale Order",
        start_date=DateTime(),
        stop_date=DateTime())
    sale_order_line = sale_order.newContent(
944
        portal_type=self.sale_order_line_portal_type,
945
        resource_value=product)
946
    transaction.commit()
947 948
    self.tic()
    self.assertEquals(sale_order_line.getPrice(), 400.0)
949
    sale_order.setDestinationSectionValue(orga2)
950
    transaction.commit()
951 952 953 954 955 956 957 958 959 960 961
    self.tic()
    sale_order_line.setPrice(None)
    self.assertEquals(sale_order_line.getPrice(), 200.0)
    # Create purchase order and check price
    purchase_order = purchase_order_module.newContent(
        portal_type="Purchase Order",
        start_date=DateTime(),
        stop_date=DateTime())
    purchase_order_line = purchase_order.newContent(
        portal_type="Purchase Order Line",
        resource_value=product)
962
    transaction.commit()
963 964
    self.tic()
    self.assertEquals(purchase_order_line.getPrice(), 40.0)
965
    purchase_order.setSourceSectionValue(orga2)
966
    transaction.commit()
967 968 969 970
    self.tic()
    purchase_order_line.setPrice(None)
    self.assertEquals(purchase_order_line.getPrice(), 20.0)
  
971 972 973 974 975 976 977
  def testGetPriceWithQuantityUnit(self):
    resource = self.portal.getDefaultModule(self.product_portal_type)\
                .newContent(portal_type=self.product_portal_type)
    resource.setDefaultQuantityUnitValue(self.quantity_unit_kilo)
    supply_line = resource.newContent(
                    portal_type=self.sale_supply_line_portal_type)
    supply_line.setBasePrice(1000)
978
    transaction.commit()
979 980 981 982
    self.tic()
    sale_order = self.portal.getDefaultModule("Sale Order").newContent(
                              portal_type='Sale Order',)
    sale_order_line = sale_order.newContent(
983
                          portal_type=self.sale_order_line_portal_type,
984 985 986 987 988 989 990
                          resource_value=resource,
                          quantity=5)
    self.assertEquals(1000, sale_order_line.getPrice())
    self.assertEquals(5000, sale_order_line.getTotalPrice())
    
    # if we give the quantity unit in grams
    sale_order_line = sale_order.newContent(
991
                          portal_type=self.sale_order_line_portal_type,
992 993 994 995 996 997
                          resource_value=resource,
                          quantity=5000,
                          quantity_unit_value=self.quantity_unit_gram)
    self.assertEquals(1, sale_order_line.getPrice())
    self.assertEquals(5000, sale_order_line.getTotalPrice())

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
  def testGetPriceWithPriceCurrency(self):
    currency_module = self.portal.getDefaultModule("Currency")
    currency = currency_module.newContent(
                     portal_type="Currency",
                     title='A great currency')
    other_currency = currency_module.newContent(
                     portal_type="Currency",
                     title='Another currency')

    resource = self.portal.getDefaultModule(self.product_portal_type)\
                .newContent(portal_type=self.product_portal_type)
    resource.setDefaultQuantityUnitValue(self.quantity_unit_kilo)
    supply_line = resource.newContent(
                    portal_type=self.sale_supply_line_portal_type)
    supply_line.setBasePrice(1000)
    supply_line.setPriceCurrencyValue(currency)
    transaction.commit()
    self.tic()
    sale_order = self.portal.getDefaultModule("Sale Order").newContent(
                              portal_type='Sale Order',
                              price_currency_value=other_currency)
    sale_order_line = sale_order.newContent(
1020
                          portal_type=self.sale_order_line_portal_type,
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
                          resource_value=resource,
                          quantity=5)
    # order and supply lines uses different currency, price does not apply
    self.assertEquals(None, sale_order_line.getPrice())
    
    # set the same currency
    sale_order.setPriceCurrencyValue(currency)

    # price applies
    self.assertEquals(1000, sale_order_line.getPrice())
    self.assertEquals(5000, sale_order_line.getTotalPrice())
    
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
  def testQuantityPrecision(self):
    """test how to define quantity precision on resources.
    """
    resource = self.portal.getDefaultModule(self.product_portal_type)\
                .newContent(portal_type=self.product_portal_type)
    # default is 1
    self.assertEquals(1, resource.getBaseUnitQuantity())
    self.assertEquals(0, resource.getQuantityPrecision())
    # quantity precision is calculated using base quantity unit
    resource.setBaseUnitQuantity(0.001)
    self.assertEquals(3, resource.getQuantityPrecision())
1044

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  def test_defaultSupplyLineAfterClone(self):
    """Check that default supply line is properly set up after clone"""
    resource = self.portal.getDefaultModule(self.product_portal_type)\
                .newContent(portal_type=self.product_portal_type)

    resource.edit( purchase_supply_line_base_price=1.0,
      sale_supply_line_base_price=1.0,
    )

    self.assertEqual( resource,
        resource.getDefaultPurchaseSupplyLineValue().getResourceValue() )
    self.assertEqual( resource,
        resource.getDefaultSaleSupplyLineValue().getResourceValue() )

    module = resource.getParentValue()

    cb_data = module.manage_copyObjects(ids=[resource.getId()])
    p_data = module.manage_pasteObjects(cb_data)

    new_resource = module._getOb(p_data[0]['new_id'])

    self.assertEqual(
      new_resource,
      new_resource.getDefaultPurchaseSupplyLineValue().getResourceValue()
    )

    self.assertEqual(
      new_resource,
      new_resource.getDefaultSaleSupplyLineValue().getResourceValue()
    )
1075 1076 1077 1078 1079

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