testSupply.py 7.79 KB
Newer Older
Łukasz Nowak's avatar
Łukasz Nowak committed
1
##############################################################################
2
# -*- coding: utf-8 -*-
Łukasz Nowak's avatar
Łukasz Nowak committed
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
# Copyright (c) 2009 Nexedi SA and Contributors. All Rights Reserved.
#          Łukasz Nowak <luke@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility 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
# guarantees 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.
#
##############################################################################

import unittest

31
import transaction
Łukasz Nowak's avatar
Łukasz Nowak committed
32
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
33
from AccessControl.SecurityManagement import newSecurityManager
Łukasz Nowak's avatar
Łukasz Nowak committed
34
from Products.ERP5Type.tests.utils import reindex
35
from DateTime import DateTime
Łukasz Nowak's avatar
Łukasz Nowak committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

class TestSupplyMixin:

  supply_portal_type = 'Sale Supply'
  supply_line_portal_type = 'Sale Supply Line'
  supply_cell_portal_type = 'Sale Supply Cell'

  def getBusinessTemplateList(self):
    """
      List of needed Business Templates
    """
    return ('erp5_base', 'erp5_pdm', 'erp5_dummy_movement', 'erp5_trade',)

  def afterSetUp(self, quiet=1, run=1):
    self.login()
    portal = self.getPortal()
    self.category_tool = self.getCategoryTool()
    self.domain_tool = self.getDomainTool()
54
    self.catalog_tool = self.getCatalogTool()
Łukasz Nowak's avatar
Łukasz Nowak committed
55 56 57 58 59 60

    if not hasattr(self.portal, 'testing_folder'):
      self.portal.newContent(portal_type='Folder',
                            id='testing_folder')
    self.folder = self.portal.testing_folder

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
  def changeUser(self, user_id):
    """
     Change the current user to user_id
    """
    user_folder = self.getPortal().acl_users
    user = user_folder.getUserById(user_id).__of__(user_folder)
    newSecurityManager(None, user)

  def createTestUser(self, **kw):
    """
    """
    person = self.portal.person_module.newContent(portal_type="Person", **kw)
    person.validate()



Łukasz Nowak's avatar
Łukasz Nowak committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
class TestSaleSupply(TestSupplyMixin, ERP5TypeTestCase):
  """
    Test Supplies usage
  """
  run_all_test = 1

  def getTitle(self):
    return "Sale Supply"

  @reindex
  def _makeMovement(self, **kw):
    """Creates a movement.
    """
    mvt = self.folder.newContent(portal_type='Dummy Movement')
    mvt.edit(**kw)
    return mvt

  @reindex
  def _makeSupply(self, **kw):
    """Creates a supply.
    """
    supply = self.portal \
      .getDefaultModule(portal_type=self.supply_portal_type) \
      .newContent(portal_type=self.supply_portal_type)
    supply.edit(**kw)
    return supply

  @reindex
  def _makeSupplyLine(self, supply, **kw):
    """Creates a supply line.
    """
    supply_line = supply.newContent(portal_type=self.supply_line_portal_type)
    supply_line.edit(**kw)
    return supply_line

  def test_01_MovementAndSupplyModification(self, quiet=0, run=run_all_test):
    """
      Check that moving timeframe of supply
      and then setting movement into that timeframe works.
    """
    if not run: return
    
    # movement is in middle of timeframe...
    movement = self._makeMovement(start_date='2009/01/15')

    supply = self._makeSupply(start_date_range_min='2009/01/01',
                              start_date_range_max='2009/01/31')

    supply_line = self._makeSupplyLine(supply)
126
    transaction.commit()
Łukasz Nowak's avatar
Łukasz Nowak committed
127 128 129 130 131 132 133 134 135 136 137 138
    self.tic()

    res = self.domain_tool.searchPredicateList(movement,
                                      portal_type=self.supply_line_portal_type)
    
    # ...and predicate shall be found
    self.assertSameSet(res, [supply_line])
    
    # timeframe is moved out of movement date...
    supply.edit(start_date_range_min='2009/02/01',
                start_date_range_max='2009/02/28')

139
    transaction.commit()
Łukasz Nowak's avatar
Łukasz Nowak committed
140 141 142 143 144 145 146 147 148 149 150
    self.tic()
    
    res = self.domain_tool.searchPredicateList(movement,
                                      portal_type=self.supply_line_portal_type)

    # ...and predicate shall NOT be found
    self.assertSameSet(res, [])

    # movement is going back into timeframe...
    movement.edit(start_date='2009/02/15')

151
    transaction.commit()
Łukasz Nowak's avatar
Łukasz Nowak committed
152 153 154 155 156 157 158 159
    self.tic()

    res = self.domain_tool.searchPredicateList(movement,
                                      portal_type=self.supply_line_portal_type)

    # ...and predicate shall be found
    self.assertSameSet(res, [supply_line])

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
  def test_02_checkLineIsReindexedOnSupplyChange(self, quiet=0, run=run_all_test):
    """
      Check that Supply Line is properly reindexed (in predicate table)
      when date is change on Supply.
    """
    if not run: return
    
    original_date = DateTime().earliestTime() # lower precision of date
    new_date = DateTime(original_date + 10)

    self.assertNotEquals(original_date, new_date)

    supply = self._makeSupply(start_date_range_min=original_date)
    supply_line = self._makeSupplyLine(supply)

    kw = {}
    kw['predicate.uid'] = supply_line.getUid()
    kw['select_expression'] = 'predicate.start_date_range_min'

    # check supply line in predicate table
    result = self.catalog_tool(**kw)
    self.assertEquals(1, len(result) )
    result = result[0]
    self.assertEquals(result.start_date_range_min, original_date.toZone('UTC'))

    # set new date on supply...
    supply.edit(start_date_range_min=new_date)
187
    transaction.commit()
188 189 190 191 192 193 194 195
    self.tic()
    
    # ...and check supply line
    kw['predicate.uid'] = supply_line.getUid()
    result = self.catalog_tool(**kw)
    self.assertEquals(1, len(result) )
    result = result[0]
    self.assertEquals(result.start_date_range_min, new_date.toZone('UTC'))
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


  def test_03_SupplyLineApplied(self, quiet=0, run=run_all_test):
    """
      Test supply line being found based on different users and security.
      XXX: Still incimplete.
    """
    if not run: return

    portal = self.portal
    original_date = DateTime().earliestTime() # lower precision of date

    # clean up
    portal.sale_supply_module.manage_delObjects(list(portal.sale_supply_module.objectIds()))
    portal.person_module.manage_delObjects(list(portal.person_module.objectIds()))
    self.stepTic()

    # movement is in middle of timeframe...
    movement = self._makeMovement(start_date=original_date)
    supply = self._makeSupply(start_date_range_min=original_date)
    supply_line = self._makeSupplyLine(supply)
    self.stepTic()
   
    user_reference = "dummy_%s" %self.supply_portal_type[:5]
    self.createTestUser(reference=user_reference)
    self.stepTic()
    self.changeUser(user_reference)

    res = self.domain_tool.searchPredicateList(movement,
                                      portal_type=self.supply_line_portal_type)
226
    
227 228 229
    # ...and predicate shall NOT be found
    self.assertSameSet([], res)

Łukasz Nowak's avatar
Łukasz Nowak committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
class TestPurchaseSupply(TestSaleSupply):
  """
    Test Purchase Supplies usage
  """
  run_all_test = 1

  supply_portal_type = 'Purchase Supply'
  supply_line_portal_type = 'Purchase Supply Line'
  supply_cell_portal_type = 'Purchase Supply Cell'

  def getTitle(self):
    return "Purchase Supply"

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