testTransformation.py 17.2 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
##############################################################################
#
# Copyright (c) 2009 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.
#
##############################################################################

29 30
from Products.ERP5.tests.testProductionOrder import TestProductionOrderMixin
from Products.ERP5.tests.testInventoryAPI import BaseTestUnitConversion
31
import transaction
32

33
class TestTransformationMixin(TestProductionOrderMixin):
34 35 36
  """
  Mixin class for checking transformations
  """
37 38 39

  # Override, let TestProductionOrderMixin define the categories for us
  operation_category_list = ['sewing', 'packing']
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

  def createTransformation(self):
    module = self.getPortalObject().getDefaultModule(
        self.transformation_portal_type)
    transformation = module.newContent(portal_type=self.transformation_portal_type)
    return transformation

  def createTransformedResource(self, transformation=None):
    transformed_resource = transformation.newContent(
        portal_type=self.transformed_resource_portal_type)
    return transformed_resource

  def createComponent(self, variation_property_list=None):
    module = self.getPortalObject().getDefaultModule(self.component_portal_type)
    component = module.newContent(portal_type=self.component_portal_type)
    if variation_property_list is not None:
      component.setVariationPropertyList(variation_property_list)
    return component

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
  def createResource(self, title, variation_base_category_list,
      variation_category_list):
    portal = self.getPortal()
    resource_module = portal.getDefaultModule(self.resource_portal_type)
    resource = resource_module.newContent(portal_type=self.resource_portal_type)
    resource.edit(
      title = title,
      variation_base_category_list = variation_base_category_list,
    )
    resource.setVariationCategoryList(variation_category_list)
    return resource

  size_category_list = ['size/' + q for q in TestProductionOrderMixin
      .mrp_size_list]
  colour_category_list = ['colour/' + q for q in TestProductionOrderMixin
      .colour_list]

  swimsuit_variation_base_category_list = ['size', 'colour']
  swimsuit_variation_category_list = size_category_list + colour_category_list
  fabric_variation_base_category_list = ['colour']
  fabric_variation_category_list = colour_category_list
  button_variation_base_category_list = ['size']
  button_variation_category_list = size_category_list

  def createButtonComponent(self):
    """
    Buttons, variated by size
    """
    resource = self.createComponent()
    resource.edit(
      title = "Round Button",
      variation_base_category_list = self.button_variation_base_category_list,
    )
    resource.setVariationCategoryList(self.button_variation_category_list)


Nicolas Dumazet's avatar
Nicolas Dumazet committed
95 96 97 98 99 100 101 102 103 104 105 106
class TestTransformation(TestTransformationMixin, BaseTestUnitConversion):
  QUANTITY_UNIT_DICT = {
    # base: (reference, dict_of_others)
    'mass':   ('kilogram', dict(gram=0.001)),
  }
  METRIC_TYPE_CATEGORY_LIST = (
    'mass/net',
  )

  def afterSetUp(self):
    TestTransformationMixin.afterSetUp(self)
    BaseTestUnitConversion.afterSetUp(self)
107 108 109 110

  def getBusinessTemplateList(self):
    """
    """
111 112
    return TestTransformationMixin.getBusinessTemplateList(self) + (
      'erp5_apparel', 'erp5_dummy_movement', 'erp5_project')
113

114 115 116 117 118 119 120 121 122
  def test_01_getAggregatedAmountListSimple(self):
    """
    Make sure that getAggregatedAmountList return something
    """
    transformation = self.createTransformation()
    transformed_resource = self.createTransformedResource(transformation)
    component = self.createComponent()
    transformed_resource.edit(
        resource_value=component,
123
        quantity=2)
124 125
    aggregated_amount_list = transformation.getAggregatedAmountList()
    self.assertEquals(len(aggregated_amount_list), 1)
126 127
    aggregated_amount = aggregated_amount_list[0]
    self.assertEquals(aggregated_amount.quantity, 2)
128

129 130 131 132 133 134
  def test_01_getAggregatedAmountListWithVariatedProperty(self):
    """
    Make sure that getAggregatedAmountList is still working properly if we
    have additionnals propertysheets on transformations lines and that used
    variation properties
    """
135 136 137 138 139 140 141
    ps_id = self._testMethodName
    property_sheet = self.portal.portal_property_sheets.newContent(
      ps_id, portal_type='Property Sheet')
    property_sheet.newContent(portal_type='Standard Property',
                              reference='foo',
                              storage_id='bar',
                              elementary_type='boolean')
142 143
    # When one do that, the property sheet should be added to many other types
    # like movements, order lines and so on.
144 145
    self._addPropertySheet('Amount', ps_id)
    self._addPropertySheet(self.transformed_resource_portal_type, ps_id)
146 147 148

    transformation = self.createTransformation()
    transformed_resource = self.createTransformedResource(transformation)
149
    component = self.createComponent(variation_property_list=['foo'])
150 151 152
    transformed_resource.edit(
        resource_value=component,
        quantity=1)
153
    transformed_resource.setFoo(True)
154
    aggregated_amount, = transformation.getAggregatedAmountList()
155 156 157 158 159 160 161
    # Make sure that the isFoo method is working properly on the temp object
    self.assertTrue(aggregated_amount.isFoo())

    # XXX aborting a transaction should reset classes
    #     if they were reset during the transaction
    transaction.abort()
    self.getTypesTool().resetDynamicDocuments()
162

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  def test_variationCategory(self):
    swimcap = self.createResource(
        'Swimming Cap',
        self.swimsuit_variation_base_category_list,
        self.swimsuit_variation_category_list,
    )
    transformation = self.createTransformation()
    transformation.edit(
        title = 'Swimcap Production',
        variation_base_category_list = self.swimsuit_variation_base_category_list
    )
    transformation.setResourceValue(swimcap)

    transaction.commit()
    self.tic()
178 179 180
    self.assertSameSet(
        set(swimcap.getVariationCategoryList()),
        set(transformation.getVariationCategoryList()),
181 182
    )

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  def test_variationCategoryWithIndividualVariation(self):
    '''Check that individual variation are return when getVariationCategoryList
    is called on a transformation
    '''
    swimcap = self.createResource(
        'Swimming Cap',
        self.swimsuit_variation_base_category_list,
        self.swimsuit_variation_category_list,
    )
    # create individual variation
    individual_variation = swimcap.newContent(portal_type='Product Individual Variation')

    transaction.commit()
    self.tic()
    transformation = self.createTransformation()
    transformation.setResourceValue(swimcap)

    transformation.edit(
        title = 'Swimcap Production',
        variation_base_category_list = \
            self.swimsuit_variation_base_category_list + ['variation',]
    )

    # check the individual variation is returned by the
    # getVariationCategoryList
    individual_url = 'variation/%s' % individual_variation.getRelativeUrl()
    self.assertTrue(individual_url in
        transformation.getVariationCategoryList())


213 214 215
  def test_transformedInventory(self):
    portal = self.getPortal()

216 217
    button_number = 3.0

218 219 220 221 222 223 224 225 226 227 228
    swimsuit = self.createResource(
        'Swimming Suit',
        self.swimsuit_variation_base_category_list,
        self.swimsuit_variation_category_list,
    )
    transformation = self.createTransformation()
    transformation.edit(
        title = 'Swimsuit Production',
        variation_base_category_list = self.swimsuit_variation_base_category_list
    )
    transformation.setResourceValue(swimsuit)
Nicolas Dumazet's avatar
Nicolas Dumazet committed
229
    transaction.commit()
230 231 232 233 234 235 236 237 238 239 240 241

    fabric = self.createResource(
        'Fabric',
        self.fabric_variation_base_category_list,
        self.fabric_variation_category_list,
    )
    fabric_line = self.createTransformedResource(transformation)
    fabric_line.setResourceValue(fabric)

    fabric_line.setVVariationBaseCategoryList(['colour'])
    for colour in self.colour_category_list:
      # For a blue swimming suit, we need blue fabric
242 243 244 245 246
      fabric_line.newCell(colour,
                          categories = colour,
                          membership_criterion_base_category= ('colour',),
                          membership_criterion_category= (colour,),
                          base_id = 'variation')
247 248 249 250 251 252

    fabric_line.setQVariationBaseCategoryList(['size'])
    for i, size in enumerate(self.size_category_list):
      # Depending on the size, the quantity of Fabric is different.
      # arbitrarily, we fix the quantity for size s as:
      # self.size_category_list.index(s) + 1
253 254 255 256 257 258
      fabric_line.newCell(size,
                          quantity = i+1,
                          mapped_value_property_list = ('quantity',),
                          membership_criterion_base_category= ('size',),
                          membership_criterion_category= (size,),
                          base_id = 'quantity')
259 260 261 262 263 264 265 266 267 268

    button = self.createComponent()
    button.edit(
      title = 'Round Button',
      variation_base_category_list = self.button_variation_base_category_list,
    )
    button.setVariationCategoryList(self.button_variation_category_list)

    button_line = self.createTransformedResource(transformation)
    button_line.setResourceValue(button)
269
    button_line.setQuantity(button_number)
270 271 272 273

    button_line.setVVariationBaseCategoryList(['size'])
    for size in self.size_category_list:
      # The button used depends on the size
274 275 276 277 278
      button_line.newCell(size,
                          categories = size,
                          membership_criterion_base_category= ('size',),
                          membership_criterion_category= (size,),
                          base_id = 'variation')
279 280 281 282 283 284 285 286 287 288

    sewing_line = transformation.newContent(
        portal_type = self.operation_line_portal_type)
    sewing_line.setResourceValue(
        portal.portal_categories.resolveCategory('operation/sewing'))

    sewing_line.setQVariationBaseCategoryList(['size', 'colour'])
    i = 1
    for size in self.size_category_list:
      for colour in self.colour_category_list:
289 290 291 292 293 294 295
        sewing_line.newCell(size,
                            colour,
                            mapped_value_property_list = ('quantity',),
                            membership_criterion_base_category= ('size', 'colour'),
                            membership_criterion_category= (size, colour),
                            quantity = i,
                            base_id = 'quantity')
296 297 298 299 300
        i += 1

    transaction.commit()
    self.tic()

Nicolas Dumazet's avatar
Nicolas Dumazet committed
301 302 303 304 305
    self.assertEqual(swimsuit.getDefaultTransformationValue().getRelativeUrl(),
                     transformation.getRelativeUrl())
    self.assertEqual(fabric.getDefaultTransformationValue(), None)
    self.assertEqual(button.getDefaultTransformationValue(), None)

306 307 308 309 310 311 312 313 314
    # Swimming Suit does not use ALL categories in Size category.
    # As a result, transformation lines should restrict their dimensions,
    # using the range induced by the resource, instead of naively
    # using the whole range directly from the variation categories.
    self.assertEqual(
        len(swimsuit.getVariationCategoryList(base_category_list=['size'])),
        len(fabric_line.getCellKeyList(base_id='quantity'))
    )

315
    swimsuit_quantity = 4.0
316
    from Products.ERP5Type.Document import newTempAmount
317 318 319
    n = 1
    # Check that getAggregatedAmount returns the expected results, a.k.a.
    # that our Transformation is set up correctly.
320 321 322
    for i, size in enumerate(self.size_category_list):
      for colour in self.colour_category_list:
        # id does not matter, just make it unique
323 324
        temp_amount = newTempAmount(transformation, "foo_%s_%s" % (size, colour))
        temp_amount.edit(
325
            quantity = swimsuit_quantity,
326 327 328
            variation_category_list = [size, colour],
            resource = swimsuit.getRelativeUrl(),
        )
329
        amount_list = transformation.getAggregatedAmountList(temp_amount)
330

331 332 333 334 335 336
        # fabric + button + sewing
        self.assertEquals(len(amount_list), 3)
        for amount in amount_list:
          resource = amount.getResource()
          if resource == fabric.getRelativeUrl():
            self.assertEquals(amount.getVariationCategoryList(), [colour])
337
            self.assertEquals(amount.getQuantity(), (i+1)*swimsuit_quantity)
338 339
          elif resource == button.getRelativeUrl():
            self.assertEquals(amount.getVariationCategoryList(), [size])
340
            self.assertEquals(amount.getQuantity(), button_number*swimsuit_quantity)
341
          elif resource == "operation/sewing":
342
            self.assertEquals(amount.getQuantity(), n*swimsuit_quantity)
343 344 345
          else:
            self.fail("Invalid Resource: %s" % resource)
        n += 1
346

Nicolas Dumazet's avatar
Nicolas Dumazet committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

    for size in self.size_category_list:
      for colour in self.colour_category_list:
        self.makeMovement(swimsuit_quantity, swimsuit, size, colour)

    transaction.commit()
    self.tic()

    inv = self.getSimulationTool().getInventoryList(
            node_uid=self.node.getUid(),
            transformed_resource=[fabric.getRelativeUrl(),
                                  button.getRelativeUrl(),
                                  "operation/sewing"],
            )
    self.assertEquals(len(inv),
          len(transformation) * len(self.size_category_list) \
            * len(self.colour_category_list))

365 366 367 368 369 370 371
    self.assertEquals(len(self.getSimulationTool().getInventoryList(
            node_uid=self.node.getUid(),
            transformed_resource=[fabric.getRelativeUrl(),
                                  button.getRelativeUrl(),
                                  "operation/sewing"],
            variation_text="something_not_existing",
            )), 0)
372

373
    n = 1
Nicolas Dumazet's avatar
Nicolas Dumazet committed
374 375 376 377 378 379 380 381 382 383
    for i, size in enumerate(self.size_category_list):
      for colour in self.colour_category_list:
        variation_text = '\n'.join([colour, size])
        inv = self.getSimulationTool().getInventoryList(
                node_uid=self.mirror_node.getUid(),
                transformed_resource=[fabric.getRelativeUrl(),
                                      button.getRelativeUrl(),
                                      "operation/sewing"],
                variation_text=variation_text,
              )
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
        self.assertEquals(len(inv), len(transformation))
        for line in inv:
          self.assertEquals(line.getVariationText(), variation_text)
          self.assertEquals(line.getResource(), swimsuit.getRelativeUrl())
          transformed_resource = line.transformed_resource_relative_url
          if transformed_resource == fabric.getRelativeUrl():
            self.assertEquals(line.transformed_variation_text, colour)
            self.assertEquals(line.total_quantity, (i+1)*swimsuit_quantity)
          elif transformed_resource == button.getRelativeUrl():
            self.assertEquals(line.transformed_variation_text, size)
            self.assertEquals(line.total_quantity, button_number*swimsuit_quantity)
          elif transformed_resource == "operation/sewing":
            self.assertEquals(line.total_quantity, n*swimsuit_quantity)
            self.assertEquals(line.transformed_variation_text, "")
          else:
            self.fail("Invalid Transformed Resource: %s" % transformed_resource)
        n += 1
Nicolas Dumazet's avatar
Nicolas Dumazet committed
401 402


403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  def test_resourceIsNotAcquiredOnTransformationLines(self):
    '''
    We don't want resource define on transformation to be acquired on
    transformation lines
    '''
    transformation = self.createTransformation()

    # create a product
    portal = self.getPortalObject()
    product_module = portal.getDefaultModule('Product')
    product = product_module.newContent(portal_type='Product')

    # set the product as resource of the transformations
    transformation.setResourceValue(product)

    # add transformations lines and check the don't acquire the resource
    operation = transformation.newContent(portal_type='Transformation Operation')
    self.assertEquals(operation.getResource(), None)

    optional_resource = transformation.newContent(portal_type=\
        'Transformation Optional Resource')
    self.assertEquals(optional_resource.getResource(), None)

    transformed_resource = transformation.newContent(portal_type=\
        'Transformation Transformed Resource')
    self.assertEquals(transformed_resource.getResource(), None)
429 430 431 432 433 434

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