testERP5BankingAvailableInventory.py 15.9 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin 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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
##############################################################################
#
# Copyright (c) 2005-2006 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.
#
##############################################################################


# import requested python module
import os
from zLOG import LOG
from DateTime import DateTime
from Products.CMFCore.utils import getToolByName
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.Sequence import SequenceList
from Products.DCWorkflow.DCWorkflow import Unauthorized, ValidationFailed
from Testing.ZopeTestCase.PortalTestCase import PortalTestCase
from Products.ERP5Banking.tests.TestERP5BankingMixin import TestERP5BankingMixin
from Products.ERP5Banking.tests.testERP5BankingCheckPayment \
      import TestERP5BankingCheckPaymentMixin
from Products.ERP5Banking.tests.testERP5BankingMoneyDeposit \
      import TestERP5BankingMoneyDepositMixin
44
from Products.ERP5Form.Document.Preference import Priority
Sebastien Robin's avatar
Sebastien Robin committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

# Needed in order to have a log file inside the current folder
os.environ['EVENT_LOG_FILE']     = os.path.join(os.getcwd(), 'zLOG.log')
# Define the level of log we want, here is all
os.environ['EVENT_LOG_SEVERITY'] = '-300'

# Define how to launch the script if we don't use runUnitTest script
if __name__ == '__main__':
  execfile(os.path.join(sys.path[0], 'framework.py'))


class TestERP5BankingAvailableInventory(TestERP5BankingCheckPaymentMixin,
                                        TestERP5BankingMoneyDepositMixin,
                                        TestERP5BankingMixin,
                                        ERP5TypeTestCase):
  """
61 62 63 64
  Unit test class in order to make sure that it is not possible
  to debit two times the same account if the amount on the account is
  too short.

65 66 67
  We must make sure that future incoming movements will not be
  taken into account

68 69
  We will by the way check the way counter dates are working :
  - it must not be possible to open two counter dates by the same time
70 71
  - it must not be possible to open two counter dates by the same time
    even if there is different dates
72
  - make sure the reference defined on counter dates is increased every day
73 74 75

  Also, we must make sure it is not possible to deliver two check
  payments by the same time (due to tag on counter)
Sebastien Robin's avatar
Sebastien Robin committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  """

  login = PortalTestCase.login

  # pseudo constants
  RUN_ALL_TEST = 1 # we want to run all test
  QUIET = 0 # we don't want the test to be quiet


  def getTitle(self):
    """
      Return the title of the test
    """
    return "ERP5BankingAvailabeInventory"

  def afterSetUp(self):
    """
      Method called before the launch of the test to initialize some data
    """
    TestERP5BankingCheckPaymentMixin.afterSetUp(self)
    self.money_deposit_counter = self.paris.surface.banque_interne
    self.money_deposit_counter_vault = self.paris.surface.banque_interne.guichet_1.encaisse_des_billets_et_monnaies.entrante
98

Sebastien Robin's avatar
Sebastien Robin committed
99 100 101 102 103 104 105
    self.createCashInventory(source=None, 
                             destination=self.money_deposit_counter_vault, 
                             currency=self.currency_1,
                             line_list=self.line_list)

    self.openCounter(site=self.money_deposit_counter_vault,id='counter_2')

106 107 108 109 110 111 112 113 114 115 116 117
    # Define foreign currency variables
    inventory_dict_line_1 = {'id' : 'inventory_line_1',
                             'resource': self.usd_billet_20,
                             'variation_id': ('emission_letter', 'cash_status', 'variation'),
                             'variation_value': ('emission_letter/not_defined', 
                                   'cash_status/not_defined') + self.usd_variation_list,
                             'variation_list': self.usd_variation_list,
                             'quantity': self.quantity_usd_20}

    self.foreign_line_list = [inventory_dict_line_1]
    

Sebastien Robin's avatar
Sebastien Robin committed
118 119 120
    # Set some variables : 
    self.money_deposit_module = self.getMoneyDepositModule()

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    # Add a preference
    preference = self.getPortal().portal_preferences.newContent()
    preference.setPreferredUsualCashMaxRenderingPrice(1000000)
    preference.setPriority(Priority.USER)
    preference.enable()

  def stepCheckOpenCounterDateTwiceFail(self, sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not open the counter date twice
    """
    self.openCounterDate(site=self.paris,id='counter_date_2',open=0)
    # open counter date and counter
    self.assertRaises(ValidationFailed,
                     self.workflow_tool.doActionFor,
                     self.counter_date_2,'open_action',
                     wf_id='counter_date_workflow')
    # get workflow history
    workflow_history = self.workflow_tool.getInfoFor(
           ob=self.counter_date_2, name='history', wf_id='counter_date_workflow')
    # check its len is 2
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    msg = workflow_history[-1]['error_message']
    self.assertTrue('there is already a counter date opened' in "%s" %(msg,))

  def stepCheckOpenCounterDateTwiceWithOtherDateFail(self, sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not open the counter date twice
    """
    self.openCounterDate(site=self.paris,id='counter_date_7',open=0)
    self.counter_date_7.setStartDate(DateTime()-1)
    # open counter date and counter
    self.assertRaises(ValidationFailed,
                     self.workflow_tool.doActionFor,
                     self.counter_date_7,'open_action',
                     wf_id='counter_date_workflow')
    # get workflow history
    workflow_history = self.workflow_tool.getInfoFor(
           ob=self.counter_date_7, name='history', wf_id='counter_date_workflow')
    # check its len is 2
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
    msg = workflow_history[-1]['error_message']
    self.assertTrue('there is already a counter date opened' in "%s" %(msg,))

  def stepCheckRemainingOperations(self, 
               sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not close the counter date 
    when there is still some operations remaining
    """
    site = self.counter_date_2.getSiteValue()
    self.assertRaises(ValidationFailed,
                     self.getPortal().Baobab_checkRemainingOperation,
                     site=site)

  def stepCheckNoRemainingOperations(self, 
               sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not close the counter date 
    when there is still some operations remaining
    """
    site = self.counter_date_1.getSiteValue()
    self.getPortal().Baobab_checkRemainingOperation(site=site)

  def stepCheckBadStockBeforeClosingDate(self, 
               sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not close the counter date 
    when there is still some operations remaining
    """
    site = self.counter_date_1.getSiteValue()
    self.assertRaises(ValidationFailed,
                     self.getPortal().Baobab_checkStockBeforeClosingDate,
                     site=site)

  def stepResetInventory(self, 
               sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not close the counter date 
    when there is still some operations remaining
    """
    bi_counter = self.paris.surface.banque_interne
    bi_counter_vault = bi_counter.guichet_1.encaisse_des_billets_et_monnaies.entrante
201 202
    line_list = self.line_list
    self.resetInventory(source=None, destination=bi_counter_vault, currency=self.currency_1,
203 204
                             line_list=line_list,extra_id='_reset_in')
    bi_counter_vault = bi_counter.guichet_1.encaisse_des_billets_et_monnaies.sortante
205
    self.resetInventory(source=None, destination=bi_counter_vault, currency=self.currency_1,
206 207 208 209 210 211 212 213 214 215 216
                             line_list=line_list,extra_id='_reset_out')

  def stepCheckRightStockBeforeClosingDate(self, 
               sequence=None, sequence_list=None, **kwd):
    """
    Make sure we can not close the counter date 
    when there is still some operations remaining
    """
    site = self.counter_date_2.getSiteValue()
    self.getPortal().Baobab_checkStockBeforeClosingDate(site=site)

Sebastien Robin's avatar
Sebastien Robin committed
217 218 219 220 221 222
  def stepCheckAccountInitialInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check the inventory of the bank account
223 224 225
    self.assertEqual(self.currency_1.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
    self.assertEqual(self.currency_1.getAvailableInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
    self.assertEqual(self.currency_1.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
Sebastien Robin's avatar
Sebastien Robin committed
226 227 228 229 230 231 232

  def stepCheckAccountConfirmedInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check the final inventory of the bank account
233 234 235
    self.assertEqual(self.currency_1.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
    self.assertEqual(self.currency_1.getAvailableInventory(payment=self.bank_account_1.getRelativeUrl()), 10000)
    self.assertEqual(self.currency_1.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
Sebastien Robin's avatar
Sebastien Robin committed
236 237 238 239 240 241 242

  def stepCheckAccountFinalInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check the final inventory of the bank account
243 244 245
    self.assertEqual(self.currency_1.getCurrentInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
    self.assertEqual(self.currency_1.getAvailableInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
    self.assertEqual(self.currency_1.getFutureInventory(payment=self.bank_account_1.getRelativeUrl()), 30000)
Sebastien Robin's avatar
Sebastien Robin committed
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
  def stepCheckReferenceIsIncreasedEveryDay(self, sequence=None, sequence_list=None, **kwd):
    """
    make sure the reference is increasing
    """
    counter_date = self.counter_date_1
    counter_date.setStartDate(self.date-10)
    self.assertEquals(counter_date.getReference(),'1')
    get_transaction().commit()
    self.tic()
    self.openCounterDate(site=self.paris, id='counter_date_3')
    counter_date = self.counter_date_3
    self.assertEquals(counter_date.getReference(),'2')
    counter_date.close()
    counter_date.setStartDate(self.date-9)
    get_transaction().commit()
    self.tic()
    self.openCounterDate(site=self.paris, id='counter_date_4')
    counter_date = self.counter_date_4
    self.assertEquals(counter_date.getReference(),'3')
    counter_date.close()
    counter_date.setStartDate(self.date-8)
    get_transaction().commit()
    self.tic()
    self.openCounterDate(site=self.paris, id='counter_date_5')
    counter_date = self.counter_date_5
    self.assertEquals(counter_date.getReference(),'4')

274 275 276 277 278 279 280 281 282 283 284
  def stepSetInventoryInSortVault(self, sequence=None, sequence_list=None, **kwd):
    """
    put some banknotes into the vault used for sorting
    """
    destination = self.paris.surface.salle_tri.encaisse_des_billets_et_monnaies
    self.createCashInventory(source=None, 
                             destination=destination, 
                             currency=self.currency_1,
                             line_list=self.line_list,
                             extra_id='_sort_vault')

285 286
  def stepSetInventoryInVaultForeignCurrency(self, sequence=None, 
            sequence_list=None, **kwd):
287 288 289
    """
    put some banknotes into the vault used for sorting
    """
290 291 292 293 294 295 296 297 298 299 300
    destination = self.paris.surface.caisse_courante.encaisse_des_devises.usd
    self.createCashInventory(source=None, 
                             destination=destination, 
                             currency=self.currency_2,
                             line_list=self.foreign_line_list,
                             extra_id='_vault_foreign_currency_vault')

  def stepResetInventoryInSortVault(self, sequence=None, sequence_list=None, **kwd):
    """
    reset the inventory
    """
301 302 303 304
    inventory_module = self.getPortal().cash_inventory_module
    to_delete_id_list = [x for x in inventory_module.objectIds() 
                         if x.find('_sort_vault')>=0]
    inventory_module.manage_delObjects(ids=to_delete_id_list)
Sebastien Robin's avatar
Sebastien Robin committed
305

306 307 308 309 310 311 312 313 314 315
  def stepResetInventoryInVaultForeignCurrency(self, sequence=None, 
          sequence_list=None, **kwd):
    """
    reset the inventory
    """
    inventory_module = self.getPortal().cash_inventory_module
    to_delete_id_list = [x for x in inventory_module.objectIds() 
                         if x.find('_vault_foreign_currency_vault')>=0]
    inventory_module.manage_delObjects(ids=to_delete_id_list)

Sebastien Robin's avatar
Sebastien Robin committed
316 317 318 319 320 321 322 323
  def test_01_ERP5BankingAvailabeInventory(self, quiet=QUIET, run=RUN_ALL_TEST):
    """
    Define the sequence of step that will be play
    """
    if not run: return
    sequence_list = SequenceList()
    # define the sequence
    sequence_string = 'Tic CheckObjects Tic CheckAccountInitialInventory ' \
324 325
                      'CheckOpenCounterDateTwiceFail Tic ' \
                      'CheckNoRemainingOperations Tic ' \
Sebastien Robin's avatar
Sebastien Robin committed
326 327 328 329 330 331 332 333 334
                      'CreateCheckPayment Tic ' \
                      'CheckConsistency Tic ' \
                      'CreateMoneyDeposit ' \
                      'ValidateAnotherCheckPaymentWorks Tic ' \
                      'SendToCounter ' \
                      'MoneyDepositSendToCounter ' \
                      'stepValidateAnotherCheckPaymentFails Tic ' \
                      'CheckAccountConfirmedInventory ' \
                      'stepValidateAnotherCheckPaymentFailsAgain Tic ' \
335
                      'CheckRemainingOperations Tic ' \
Sebastien Robin's avatar
Sebastien Robin committed
336 337 338 339
                      'InputCashDetails Tic ' \
                      'MoneyDepositInputCashDetails Tic ' \
                      'DeliverMoneyDeposit Tic ' \
                      'ValidateAnotherCheckPaymentWorksAgain Tic ' \
340 341
                      'Pay PayAnotherCheckPaymentFails ' \
                      'Tic ' \
342 343 344
                      'CheckAccountFinalInventory ' \
                      'CheckBadStockBeforeClosingDate ' \
                      'ResetInventory Tic ' \
345 346 347
                      'SetInventoryInSortVault Tic ' \
                      'CheckBadStockBeforeClosingDate ' \
                      'ResetInventoryInSortVault Tic ' \
348
                      'CheckRightStockBeforeClosingDate ' \
349 350 351 352
                      'SetInventoryInVaultForeignCurrency Tic ' \
                      'CheckBadStockBeforeClosingDate ' \
                      'ResetInventoryInVaultForeignCurrency Tic ' \
                      'CheckRightStockBeforeClosingDate ' \
353 354
                      'CheckReferenceIsIncreasedEveryDay ' \
                      'CheckOpenCounterDateTwiceWithOtherDateFail Tic ' 
Sebastien Robin's avatar
Sebastien Robin committed
355 356 357 358 359 360 361 362 363 364 365 366 367
    sequence_list.addSequenceString(sequence_string)
    # play the sequence
    sequence_list.play(self)

# define how we launch the unit test
if __name__ == '__main__':
  framework()
else:
  import unittest
  def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestERP5BankingAvailableInventory))
    return suite