testDivergenceTester.py 12.8 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) 2006 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
Romain Courteaud's avatar
Romain Courteaud committed
30 31

from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
32
from Products.ERP5Type.tests.Sequence import SequenceList
Romain Courteaud's avatar
Romain Courteaud committed
33 34 35 36 37 38 39 40
from testPackingList import TestPackingListMixin

class TestDivergenceTester(TestPackingListMixin, ERP5TypeTestCase):
  """
  Check isDivergent method on movement, delivery
  """
  run_all_test = 1
  quiet = 0
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
41
  rule_id = 'default_delivery_simulation_rule'
Romain Courteaud's avatar
Romain Courteaud committed
42 43 44 45 46 47 48 49 50 51 52 53 54 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

  def getTitle(self):
    return "Divergence Tester"

  def stepRemoveDivergenceTesters(self, sequence=None, 
                                  sequence_list=None, **kw):
    """
    Remove all divergence testers from order_rule.
    """
    rule = getattr(self.getPortal().portal_rules, self.rule_id)
    tester_list = rule.contentValues(
             portal_type=rule.getPortalDivergenceTesterTypeList())
    rule.manage_delObjects(
                    uids=[x.getUid() for x in tester_list])

  def bootstrapSite(self):
    """
    Manager has to create an administrator user first.
    """
    self.logMessage("Bootstrap the site by creating required " \
                    "order, simulation, ...")
    sequence_list = SequenceList()
    # Create a clean packing list
    sequence_string = ' \
          stepRemoveDivergenceTesters \
          stepCreateOrganisation1 \
          stepCreateOrganisation2 \
          stepCreateOrganisation3 \
          stepCreateOrder \
          stepSetOrderProfile \
          stepCreateNotVariatedResource \
          stepTic \
          stepCreateOrderLine \
          stepSetOrderLineResource \
          stepSetOrderLineDefaultValues \
          stepOrderOrder \
          stepTic \
          stepConfirmOrder \
          stepTic \
    '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=self.quiet)
    self.logMessage("Bootstrap finished")

Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
86
  def setUpOnce(self, quiet=1, run=run_all_test):
Romain Courteaud's avatar
Romain Courteaud committed
87 88 89 90
    """
    Create an order and generate a packing list from it.
    This has to be called only once.
    """
Kazuhiko Shiozaki's avatar
Kazuhiko Shiozaki committed
91 92
    self.validateRules()
    self.bootstrapSite()
Romain Courteaud's avatar
Romain Courteaud committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 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 331 332 333 334 335

  def stepGetPackingList(self, sequence=None, sequence_list=None, **kw):
    """
    Set the packing list in the sequence
    """
    sql_result = self.getPortal().portal_catalog(
                         portal_type=self.packing_list_portal_type)
    self.assertEquals(1, len(sql_result))
    packing_list = sql_result[0].getObject()
    # XXX Hardcoded id
    movement=packing_list['1']
    rule = getattr(self.getPortal().portal_rules, self.rule_id)
    sequence.edit(
        packing_list=packing_list,
        movement=movement,
        rule=rule,
        sim_mvt=movement.getDeliveryRelatedValueList()[0])

  def stepCheckPackingListIsDivergent(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Test if packing list is divergent
    """
    packing_list = sequence.get('packing_list')
    self.assertTrue(packing_list.isDivergent())

  def stepSetNewQuantity(self, sequence=None, 
                         sequence_list=None, **kw):
    """
    Modify the quantity of the delivery movement
    """
    packing_list = sequence.get('packing_list')
    movement = sequence.get('movement')
    movement.setQuantity(movement.getQuantity()+1234)

  def stepSetPreviousQuantity(self, sequence=None, 
                              sequence_list=None, **kw):
    """
    Reset the quantity of the delivery movement
    """
    sim_mvt = sequence.get('sim_mvt')
    movement = sequence.get('movement')
    movement.setQuantity(sim_mvt.getQuantity())

  def stepAddQuantityDivergenceTester(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Add a quantity divergence tester in the rule
    """
    rule = sequence.get('rule')
    rule.newContent(portal_type='Quantity Divergence Tester')

  def test_01_QuantityDivergenceTester(self, quiet=quiet, run=run_all_test):
    """
    Test the quantity divergence tester
    """
    if not run: return
    sequence_list = SequenceList()
    # Create a clean packing list
    sequence_string = ' \
          stepGetPackingList \
          stepCheckPackingListIsNotDivergent \
          stepSetNewQuantity \
          stepCheckPackingListIsNotDivergent \
          stepAddQuantityDivergenceTester \
          stepCheckPackingListIsDivergent \
          stepSetPreviousQuantity \
          stepCheckPackingListIsNotDivergent \
          Tic \
          stepRemoveDivergenceTesters \
          Tic \
    '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=self.quiet)

  def stepSetNewSource(self, sequence=None, 
                       sequence_list=None, **kw):
    """
    Modify the source of the delivery
    """
    packing_list = sequence.get('packing_list')
    packing_list.setSource(None)

  def stepAddCategoryDivergenceTester(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Add a category divergence tester in the rule
    """
    rule = sequence.get('rule')
    tester = rule.newContent(portal_type='Category Divergence Tester')
    sequence.edit(tester=tester)

  def stepConfigureCategoryDivergenceTesterForSource(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Add a category divergence tester in the rule
    """
    tester = sequence.get('tester')
    tester.setTestedPropertyList(['source | Source'])

  def stepSetPreviousSource(self, sequence=None, 
                            sequence_list=None, **kw):
    """
    Reset the quantity of the delivery
    """
    sim_mvt = sequence.get('sim_mvt')
    packing_list = sequence.get('packing_list')
    packing_list.setSource(sim_mvt.getSource())

  def test_02_CategoryDivergenceTester(self, quiet=quiet, run=run_all_test):
    """
    Test the category divergence tester
    """
    if not run: return
    sequence_list = SequenceList()
    # Create a clean packing list
    sequence_string = ' \
          stepGetPackingList \
          stepCheckPackingListIsNotDivergent \
          stepSetNewSource \
          stepCheckPackingListIsNotDivergent \
          stepAddCategoryDivergenceTester \
          stepCheckPackingListIsNotDivergent \
          stepConfigureCategoryDivergenceTesterForSource \
          stepCheckPackingListIsDivergent \
          stepSetPreviousSource \
          stepCheckPackingListIsNotDivergent \
          Tic \
          stepRemoveDivergenceTesters \
          Tic \
    '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=self.quiet)

  def stepSetNewStartDate(self, sequence=None, 
                       sequence_list=None, **kw):
    """
    Modify the source of the delivery
    """
    packing_list = sequence.get('packing_list')
    packing_list.setStartDate(packing_list.getStartDate()+10)

  def stepAddPropertyDivergenceTester(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Add a property divergence tester in the rule
    """
    rule = sequence.get('rule')
    tester = rule.newContent(portal_type='Property Divergence Tester')
    sequence.edit(tester=tester)

  def stepConfigurePropertyDivergenceTesterForStartDate(self, sequence=None, 
                                      sequence_list=None, **kw):
    """
    Add a property divergence tester in the rule
    """
    tester = sequence.get('tester')
    tester.setTestedPropertyList(['start_date | Start Date'])

  def stepSetPreviousStartDate(self, sequence=None, 
                               sequence_list=None, **kw):
    """
    Reset the quantity of the delivery
    """
    sim_mvt = sequence.get('sim_mvt')
    packing_list = sequence.get('packing_list')
    packing_list.setStartDate(sim_mvt.getStartDate())

  def test_03_PropertyDivergenceTester(self, quiet=quiet, run=run_all_test):
    """
    Test the property divergence tester
    """
    if not run: return
    sequence_list = SequenceList()
    # Create a clean packing list
    sequence_string = ' \
          stepGetPackingList \
          stepCheckPackingListIsNotDivergent \
          stepSetNewStartDate \
          stepCheckPackingListIsNotDivergent \
          stepAddPropertyDivergenceTester \
          stepCheckPackingListIsNotDivergent \
          stepConfigurePropertyDivergenceTesterForStartDate \
          stepCheckPackingListIsDivergent \
          stepSetPreviousStartDate \
          stepCheckPackingListIsNotDivergent \
          Tic \
          stepRemoveDivergenceTesters \
          Tic \
    '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=self.quiet)

  def stepSetNewAggregate(self, sequence=None, 
                          sequence_list=None, **kw):
    """
    Modify the aggregate of the delivery movement
    """
    movement = sequence.get('movement')
    # Set a aggregate value which does not exist
    # but it should not be a problem for testing the divergency
    movement.setAggregate('a_great_module/a_random_id')

  def stepConfigureCategoryDivergenceTesterForAggregate(self, sequence=None, 
                                                        sequence_list=None, **kw):
    """
    Add a category divergence tester in the rule
    """
    tester = sequence.get('tester')
    tester.setTestedPropertyList(['aggregate | Aggregate'])

  def stepSetPreviousAggregate(self, sequence=None, 
                            sequence_list=None, **kw):
    """
    Reset the quantity of the delivery
    """
    movement = sequence.get('movement')
    movement.setAggregate(None)

  def test_04_CategoryDivergenceTester(self, quiet=quiet, run=run_all_test):
    """
    Test the category divergence tester
    """
    if not run: return
    sequence_list = SequenceList()
    # Create a clean packing list
    sequence_string = ' \
          stepGetPackingList \
          stepCheckPackingListIsNotDivergent \
          stepSetNewAggregate \
          stepCheckPackingListIsNotDivergent \
          stepAddCategoryDivergenceTester \
          stepCheckPackingListIsNotDivergent \
          stepConfigureCategoryDivergenceTesterForAggregate \
          stepCheckPackingListIsDivergent \
          stepSetPreviousAggregate \
          stepCheckPackingListIsNotDivergent \
          Tic \
          stepRemoveDivergenceTesters \
          Tic \
    '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self, quiet=self.quiet)
336

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
  def test_QuantityDivergenceTesterCompareMethod(self):
    rule = self.portal.portal_rules.newContent(portal_type='Delivery Rule')
    divergence_tester = rule.newContent(portal_type='Quantity Divergence Tester')

    self.assert_(not divergence_tester.isDecimalAlignmentEnabled())
    self.assertEqual(divergence_tester.compare(3.0, 3.001), False)
    self.assertEqual(divergence_tester.compare(3.0, 3.0), True)

    divergence_tester.setDecimalAlignmentEnabled(True)
    divergence_tester.setDecimalRoundingOption('ROUND_DOWN')
    divergence_tester.setDecimalExponent('0.01')

    self.assertEqual(divergence_tester.compare(3.0, 3.001), True)
    self.assertEqual(divergence_tester.compare(3.0, 3.0), True)

    divergence_tester.setDecimalExponent('0.001')
    self.assertEqual(divergence_tester.compare(3.0, 3.001), False)

    divergence_tester.setDecimalRoundingOption('ROUND_UP')
    divergence_tester.setDecimalExponent('0.01')
    self.assertEqual(divergence_tester.compare(3.0, 3.001), False)

    divergence_tester.setDecimalRoundingOption('ROUND_HALF_UP')
    divergence_tester.setDecimalExponent('0.01')
    self.assertEqual(divergence_tester.compare(3.0, 3.001), True)

363 364 365 366 367

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