testDomainTool.py 16.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################



#
# Skeleton ZopeTestCase
#

from random import randint

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE'] = os.path.join(os.getcwd(), 'zLOG.log')
os.environ['EVENT_LOG_SEVERITY'] = '-300'

from Testing import ZopeTestCase
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from DateTime import DateTime
from Products.ERP5.Document.Person import Person
from AccessControl.SecurityManagement import newSecurityManager, noSecurityManager
from Products.ERP5SyncML.Conduit.ERP5Conduit import ERP5Conduit
from Products.ERP5SyncML.SyncCode import SyncCode
from zLOG import LOG
import time

55
class TestDomainTool(ERP5TypeTestCase):
56 57 58

  # Different variables used for this test
  run_all_test = 1
59 60 61
  resource_type='Apparel Component'
  resource_variation_type='Apparel Component Variation'
  resource_module = 'apparel_component_module'
62 63 64 65 66 67

  def getTitle(self):
    """
    """
    return "Domain Tool"

68 69 70 71 72
  def enableHotReindexing(self):
    """
    You can override this. Return if we should create (1) or not (0) an activity tool
    """
    return 0
73 74 75 76 77 78

  def getBusinessTemplateList(self):
    """
      Return the list of business templates.

    """
Aurel's avatar
Aurel committed
79
    return ('erp5_pdm', 'erp5_trade', 'erp5_apparel')
80 81 82 83

  def getPortalId(self):
    return self.getPortal().getId()

84 85 86
  def getResourceModule(self):
    return getattr(self.getPortal(), self.resource_module, None)

87 88 89 90
  def logMessage(self,message):
    ZopeTestCase._print('\n%s ' % message)
    LOG('Testing... ',0,message)

91 92 93 94 95 96 97 98 99
  def afterSetUp(self):
    self.login()

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

100
  def getSaleOrderModule(self):
101
    return getattr(self.getPortal(),'sale_order_module',None)
102 103 104 105 106

  def getOrderLine(self):
    return self.getSaleOrderModule()['1']['1']

  def getPredicate(self):
107
    return self.getOrganisationModule()['1']
108 109 110

  def createData(self):
    # We have no place to put a Predicate, we will put it in a
111
    # Organisation Module
112 113
    portal = self.getPortal()
    type_tool = self.getTypeTool()
114
    module_type = type_tool['Organisation Module']
115
    module_type.allowed_content_types += ('Mapped Value',)
116
    organisation_module = self.getOrganisationModule()
117 118
    if organisation_module.hasContent('1'):
      organisation_module.deleteContent('1')
119
    predicate = organisation_module.newContent(id='1',portal_type='Mapped Value')
120 121
    predicate.setCriterion('quantity',identity=None,min=None,max=None)
    
122
    resource_module = self.getResourceModule()
123 124
    if resource_module.hasContent('1'):
      resource_module.deleteContent('1')
125
    self.resource = resource = resource_module.newContent(id='1',portal_type=self.resource_type)
126 127
    resource.newContent(id='blue',portal_type=self.resource_variation_type)
    resource.newContent(id='red',portal_type=self.resource_variation_type)
128
    resource.setVariationBaseCategoryList(['variation'])
129 130
    if resource.hasContent('default_supply_line'):
      resource.deleteContent('default_supply_line')
131 132
    self.supply_line = supply_line = resource.newContent(id='default_supply_line',portal_type='Supply Line')

133 134
    # Then create an order with a particular line
    order_module = self.getSaleOrderModule()
135 136 137
    if order_module.hasContent('1'):
      order_module.deleteContent('1')
    order = order_module.newContent(id='1',portal_type='Sale Order')
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    line = order.newContent(id='1',portal_type='Sale Order Line')

    # Then create a base category
    portal_categories = self.getCategoryTool()
    for bc in ('region', ):
      if not hasattr(portal_categories, bc):
        portal_categories.newContent(portal_type='Base Category',id=bc)
      portal_categories[bc].setAcquisitionMaskValue(1)
      portal_categories[bc].setAcquisitionCopyValue(0)
      portal_categories[bc].setAcquisitionAppendValue(0)
      if not 'europe' in portal_categories[bc].objectIds():
        big_region = portal_categories[bc].newContent(id='europe',portal_type='Category')
      if not 'africa' in portal_categories[bc].objectIds():
        big_region = portal_categories[bc].newContent(id='africa',portal_type='Category')
      if not 'asia' in portal_categories[bc].objectIds():
        big_region = portal_categories[bc].newContent(id='asia',portal_type='Category')
154 155 156

    get_transaction().commit()
    self.tic()
157 158 159 160 161 162 163

  def checkPredicate(self, test=None):

    predicate = self.getPredicate()
    #predicate.setMembershipCriterionBaseCategoryList([])
    #predicate.setMembershipCriterionCategoryList([])
    #predicate.setCriterion('quantity',identity=45,min=None,max=None)
164
    #predicate.immediateReindexObject()
165 166 167 168 169 170 171 172 173 174 175 176


    order_line = self.getOrderLine()
    domain_tool = self.getDomainTool()

    # Test with order line and predicate to none
    predicate_list = domain_tool.searchPredicateList(order_line,test=test)
    self.assertEquals(len(predicate_list),1) # Actually, a predicate where
                                             # nothing is defined is ok

    # Test with order line not none and predicate to none
    order_line.setQuantity(45)
177 178
    get_transaction().commit()
    self.tic()
179 180 181 182 183
    predicate_list = domain_tool.searchPredicateList(order_line,test=test)
    self.assertEquals(len(predicate_list),1)

    # Test with order line not none and predicate to identity
    order_line.setQuantity(45)
184
    kw = {'portal_type':'Mapped Value'}
185
    predicate.setCriterion('quantity',identity=45,min=None,max=None)
186 187
    get_transaction().commit()
    self.tic()
188
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
189
    self.assertEquals(len(predicate_list),1)
190

191
    order_line.setQuantity(40)
192 193
    get_transaction().commit()
    self.tic()
194
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
195 196 197 198 199 200
    self.assertEquals(len(predicate_list),0)

    # Test with order line not none and predicate to min
    order_line.setQuantity(45)
    predicate = self.getPredicate()
    predicate.setCriterion('quantity',identity=None,min=30,max=None)
201 202
    get_transaction().commit()
    self.tic()
203
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
204
    self.assertEquals(len(predicate_list),1)
205

206
    order_line.setQuantity(10)
207 208
    get_transaction().commit()
    self.tic()
209 210 211 212 213 214 215
    predicate_list = domain_tool.searchPredicateList(order_line,test=test)
    self.assertEquals(len(predicate_list),0)

    # Test with order line not none and predicate to max
    order_line.setQuantity(45)
    predicate = self.getPredicate()
    predicate.setCriterion('quantity',identity=None,min=None,max=50)
216 217
    get_transaction().commit()
    self.tic()
218
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
219
    self.assertEquals(len(predicate_list),1)
220

221
    order_line.setQuantity(60)
222 223
    get_transaction().commit()
    self.tic()
224
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
225 226 227 228 229 230
    self.assertEquals(len(predicate_list),0)

    # Test with order line not none and predicate to min max
    order_line.setQuantity(20)
    predicate = self.getPredicate()
    predicate.setCriterion('quantity',identity=None,min=30,max=50)
231 232
    get_transaction().commit()
    self.tic()
233
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
234
    self.assertEquals(len(predicate_list),0)
235

236
    order_line.setQuantity(60)
237 238
    get_transaction().commit()
    self.tic()
239
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
240
    self.assertEquals(len(predicate_list),0)
241

242
    order_line.setQuantity(45)
243
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
244 245
    get_transaction().commit()
    self.tic()
246 247 248 249 250 251
    self.assertEquals(len(predicate_list),1)

    # Test with order line not none and predicate to min max
    # and also predicate to a category
    predicate.setMembershipCriterionBaseCategoryList(['region'])
    predicate.setMembershipCriterionCategoryList(['region/europe'])
252 253
    get_transaction().commit()
    self.tic()
254
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
255
    self.assertEquals(len(predicate_list),0)
256

257
    order_line.setCategoryList(['region/africa'])
258 259
    get_transaction().commit()
    self.tic()
260
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
261
    self.assertEquals(len(predicate_list),0)
262

263
    order_line.setCategoryList(['region/europe'])
264 265
    get_transaction().commit()
    self.tic()
266
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
267
    self.assertEquals(len(predicate_list),1)
268

269
    order_line.setQuantity(60)
270 271
    get_transaction().commit()
    self.tic()
272
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
273 274
    self.assertEquals(len(predicate_list),0)

275 276 277 278 279 280 281 282 283 284 285 286
    # Test with order line not none and predicate to date min and date max
    kw = {'portal_type':'Supply Line'}
    self.supply_line.setBasePrice(23)
    self.supply_line.setPricedQuantity(1)
    self.supply_line.setDefaultResourceValue(self.resource)
    order_line.setDefaultResourceValue(self.resource)
    date1 = DateTime('2005/04/08 10:47:26.388 GMT-4')
    date2 = DateTime('2005/04/10 10:47:26.388 GMT-4')
    self.supply_line.setStartDateRangeMin(date1)
    self.supply_line.setStartDateRangeMax(date2)
    current_date = DateTime('2005/04/1 10:47:26.388 GMT-4')
    order_line.setStartDate(current_date)
287 288
    get_transaction().commit()
    self.tic()
289 290
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
    self.assertEquals(len(predicate_list),0)
291

292 293
    current_date = DateTime('2005/04/09 10:47:26.388 GMT-4')
    order_line.setStartDate(current_date)
294 295
    get_transaction().commit()
    self.tic()
296 297 298
    predicate_list = domain_tool.searchPredicateList(order_line,test=test,**kw)
    self.assertEquals(len(predicate_list),1)

299 300 301 302 303 304 305 306 307 308 309 310 311 312
  def test_01_SearchPredidateListWithNoTest(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Search Predicate List With No Test')
    self.createData()
    self.checkPredicate(test=0)

  def test_02_SearchPredidateListWithTest(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Search Predicate List With Test')
    self.createData()
    self.checkPredicate(test=1)

313 314 315 316 317
  def test_03_GenerateMappedValue(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Generate Mapped Value')
    self.createData()
318
    self.supply_line.setVariationBaseCategoryList(['colour'])
319 320 321
    self.supply_line.setBasePrice(23)
    self.supply_line.setPricedQuantity(1)
    self.supply_line.setDefaultResourceValue(self.resource)
322
    #self.supply_line.setMultimembershipCriterionBaseCategoryList(['resource'])
323
    self.supply_line.setMappedValuePropertyList(['base_price','priced_quantity'])
324
    #self.supply_line.setMembershipCriterionCategoryList(['resource/%s' % self.resource.getRelativeUrl()])
325 326
    get_transaction().commit()
    self.tic()
327 328 329
    domain_tool = self.getDomainTool()
    context = self.resource.asContext(categories=['resource/%s' % self.resource.getRelativeUrl()])
    mapped_value = domain_tool.generateMappedValue(context)
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    self.assertEquals(mapped_value.getBasePrice(),23)

  def test_04_GenerateMappedValueWithRanges(self, quiet=0, run=run_all_test):
    if not run: return
    if not quiet:
      self.logMessage('Generate Mapped Value With Ranges')
    self.createData()
    self.supply_line.setBasePrice(23)
    self.supply_line.setPricedQuantity(1)
    self.supply_line.setDefaultResourceValue(self.resource)
    date1 = DateTime('2005/04/08')
    date2 = DateTime('2005/04/10')
    self.supply_line.setStartDateRangeMin(date1)
    self.supply_line.setStartDateRangeMax(date2)
    LOG('Test04, supply_line.getStartDateRangeMin',0,self.supply_line.getStartDateRangeMin())
    LOG('Test04, supply_line.getStartDateRangeMax',0,self.supply_line.getStartDateRangeMax())
    self.supply_line.setMappedValuePropertyList(['base_price','priced_quantity'])
347 348
    get_transaction().commit()
    self.tic()
349 350 351 352 353 354 355 356 357 358 359
    domain_tool = self.getDomainTool()
    order_line = self.getOrderLine()
    order_line.setDefaultResourceValue(self.resource)
    current_date = DateTime('2005/04/01')
    order_line.setStartDate(current_date)
    kw = {'portal_type':('Supply Line','Supply Cell')}
    mapped_value = domain_tool.generateMappedValue(order_line,**kw)
    self.assertEquals(mapped_value,None)
    current_date = DateTime('2005/04/09')
    order_line.setStartDate(current_date)
    mapped_value = domain_tool.generateMappedValue(order_line,**kw)
360 361
    self.assertEquals(mapped_value.getBasePrice(),23)

362
  def test_05_GenerateMappedValueWithVariation(self, quiet=0, run=run_all_test):
363 364 365 366
    if not run: return
    if not quiet:
      self.logMessage('Generate Mapped Value With Variation')
    self.createData()
367
    self.supply_line.setVariationBaseCategoryList(['colour'])
368 369 370
    self.supply_line.setBasePrice(23)
    self.supply_line.setPricedQuantity(1)
    self.supply_line.setDefaultResourceValue(self.resource)
371
    #self.supply_line.setMultimembershipCriterionBaseCategoryList(['resource']) # Do we need to add 'variation' ???
372
    self.supply_line.setMappedValuePropertyList(['base_price','priced_quantity'])
373
    #self.supply_line.setMembershipCriterionCategoryList(['resource/%s' % self.resource.getRelativeUrl()])
374 375
    #self.supply_line.setPVariationBaseCategoryList(['variation'])
    self.resource.setPVariationBaseCategoryList(['variation'])
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    self.supply_line.updateCellRange(base_id='path')
    cell_range = self.supply_line.SupplyLine_asCellRange()
    for range in cell_range[0]:
      cell = self.supply_line.newCell(range,base_id='path')
      cell.setMappedValuePropertyList(['base_price','priced_quantity'])
      cell.setMultimembershipCriterionBaseCategoryList(['resource','variation'])
      LOG('test, range',0,range)
      cell.setPricedQuantity(1)
      if range.find('blue')>=0:
        cell.setMembershipCriterionCategoryList([range])
        cell.setBasePrice(45)
      if range.find('red')>=0:
        cell.setMembershipCriterionCategoryList([range])
        cell.setBasePrice(26)

    def sort_method(x,y):
      # make sure we get cell before
      if hasattr(x,'hasCellContent'):
        x_cell = x.hasCellContent(base_id='path')
        if x_cell:
          return 1
      if hasattr(y,'hasCellContent'):
        y_cell = y.hasCellContent(base_id='path')
        if y_cell:
          return -1
      return 0

403 404
    get_transaction().commit()
    self.tic()
405 406 407 408 409 410 411 412 413 414
    domain_tool = self.getDomainTool()
    context = self.resource.asContext(categories=['resource/%s' % self.resource.getRelativeUrl(),'variation/%s/blue' % self.resource.getRelativeUrl()])
    mapped_value = domain_tool.generateMappedValue(context,sort_method=sort_method)
    self.assertEquals(mapped_value.getProperty('base_price'),45)
    context = self.resource.asContext(categories=['resource/%s' % self.resource.getRelativeUrl(),'variation/%s/red' % self.resource.getRelativeUrl()])
    mapped_value = domain_tool.generateMappedValue(context,sort_method=sort_method)
    self.assertEquals(mapped_value.getProperty('base_price'),26)
    # Now check the price
    self.assertEquals(self.resource.getPrice( self.resource.asContext(categories=['resource/%s' % self.resource.getRelativeUrl(),'variation/%s/blue' % self.resource.getRelativeUrl()]),sort_method=sort_method),45)

415