testERP5BankingMonetaryIssue.py 21.8 KB
Newer Older
Aurel's avatar
Aurel 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 44 45 46
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Aurelien Calonne <aurel@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 Products.ERP5Type.tests.Sequence import SequenceList
from Products.DCWorkflow.DCWorkflow import Unauthorized, ValidationFailed
from Products.ERP5Banking.tests.TestERP5BankingMixin import TestERP5BankingMixin
from DateTime import DateTime

# 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'))


47
class TestERP5BankingMonetaryReceptionMixin(TestERP5BankingMixin):
Aurel's avatar
Aurel committed
48

Sebastien Robin's avatar
Sebastien Robin committed
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  def stepCreateMonetaryReception(self, sequence=None, sequence_list=None, **kw):
    """
    Create a monetary reception to have some cash container to use in the issue test
    """
    self.monetary_reception = self.monetary_reception_module.newContent(id='monetary_reception', portal_type='Monetary Reception',
                                                                        source_value=None, destination_value=self.reception_site,
                                                                        resource_value=self.currency_1, start_date=self.current_date)
    self.stepTic()
    # now create container
    global_dict = {}
    global_dict['emission_letter'] = 'p'
    global_dict['variation'] = '2003'
    global_dict['cash_status'] = 'new_not_emitted'
    global_dict['resource'] = self.billet_10000
    line_list = []
    line_1 = {}
    line_1['id'] = '1'
    line_1['reference'] = 'unit_test_1'
    line_1['range_start'] = 0
    line_1['range_stop'] = 100
    line_1['quantity'] = 100
    line_list.append(line_1)
    line_2 = {}
    line_2['id'] = '2'
    line_2['reference'] = 'unit_test_2'
    line_2['range_start'] = 100
    line_2['range_stop'] = 200
    line_2['quantity'] = 100
    line_list.append(line_2)
    self.createCashContainer(self.monetary_reception, 'Cash Container Item', global_dict, line_list)
    self.stepTic()
    self.workflow_tool.doActionFor(self.monetary_reception, 'confirm_action', wf_id='monetary_reception_workflow')
    self.stepTic()
    self.workflow_tool.doActionFor(self.monetary_reception, 'deliver_action', wf_id='monetary_reception_workflow')
    self.stepTic()
    # check that state is delivered, all other check are done in a separate unit test
    self.assertEqual(self.monetary_reception.getSimulationState(), 'delivered')


  def stepCheckInitialInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check the initial inventory before any operations
    """
    self.simulation_tool = self.getSimulationTool()
    # check source
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.reception_site.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.reception_site.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)
    # check destination
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.destination_site.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.destination_site.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)


  def stepCheckInitialContainerInventory(self, sequence=None, sequence_list=None, **kw):
    """
    Check initial cash container on source
    """
    self.assertEqual(len(self.simulation_tool.getCurrentTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 2)
    self.assertEqual(len(self.simulation_tool.getFutureTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 2)


109
class TestERP5BankingMonetaryIssue(TestERP5BankingMonetaryReceptionMixin):
Aurel's avatar
Aurel committed
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
  """
    This class is a unit test to check the module of Monetary Issue

    Here are the following step that will be done in the test :

    XXX to be completed

  """


  # 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 "ERP5BankingMonetaryIssue"

  def getMonetaryIssueModule(self):
    """
    Return the Cash Inventory Module
    """
    return getattr(self.getPortal(), 'monetary_issue_module', None)


  def getMonetaryReceptionModule(self):
    """
    Return the Cash Inventory Module
    """
    return getattr(self.getPortal(), 'monetary_reception_module', None)


  def afterSetUp(self):
    """
      Method called before the launch of the test to initialize some data
    """
    # Set some variables :
    self.initDefaultVariable()
    # get module
    self.monetary_issue_module = self.getMonetaryIssueModule()
    self.monetary_reception_module = self.getMonetaryReceptionModule()
    self.current_date = DateTime()
    self.createManagerAndLogin()
156
    self.createFunctionGroupSiteCategory(site_list=['paris',])
Aurel's avatar
Aurel committed
157 158 159
    self.createBanknotesAndCoins()
    self.issue = self.paris.caveau.reserve.encaisse_des_billets_et_monnaies
    self.reception = self.paris.caveau.serre.encaisse_des_billets_neufs_non_emis
Sebastien Robin's avatar
Sebastien Robin committed
160 161
    self.reception_site = self.reception
    self.destination_site = self.issue
Aurel's avatar
Aurel committed
162 163 164 165 166 167 168 169 170 171 172
    self.checkUserFolderType()
    self.organisation = self.organisation_module.newContent(id='baobab_org', portal_type='Organisation',
                                                            function='banking', group='baobab',  site='testsite/paris')
    # define the user
    user_dict = {
        'super_user' : [['Manager'], self.organisation, 'banking/comptable', 'baobab', 'testsite/paris']
      }
    # call method to create this user
    self.createERP5Users(user_dict)
    self.logout()
    self.login('super_user')
173
    self.openCounterDate(site=self.paris)
Sebastien Robin's avatar
Sebastien Robin committed
174 175
    # this is required in order to have some items
    # in the source
Aurel's avatar
Aurel committed
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


  def stepCheckObjects(self, sequence=None, sequence_list=None, **kwd):
    """
    Check that all the objects we created in afterSetUp or
    that were added by the business template and that we rely
    on are really here.
    """
    self.checkResourceCreated()
    # check that MonetaryIssue Module was created
    self.assertEqual(self.monetary_issue_module.getPortalType(), 'Monetary Issue Module')
    # check cash inventory module is empty
    self.assertEqual(len(self.monetary_issue_module.objectValues()), 0)






  def stepCreateMonetaryIssue(self, sequence=None, sequence_list=None, **kwd):
    """
    Create a cash inventory document and check it
    """
    # issue date must be a bit in the future so regenerate date
    self.current_date = DateTime()
    # Cash inventory has caisse_1 for source, caisse_2 for destination, and a price cooreponding to the sum of banknote of 10000 abd coin of 200 ( (2+3) * 1000 + (5+7) * 200 )
202 203 204 205 206
    self.monetary_issue = self.monetary_issue_module.newContent(
                                id='monetary_issue', 
                                portal_type='Monetary Issue', 
                                description='test',
                                start_date=self.current_date)
Aurel's avatar
Aurel committed
207 208
    # execute tic
    self.stepTic()
209 210 211 212 213
    # set source reference
    self.setDocumentSourceReference(self.monetary_issue)
    # check source reference
    self.assertNotEqual(self.monetary_issue.getSourceReference(), '')
    self.assertNotEqual(self.monetary_issue.getSourceReference(), None)
Aurel's avatar
Aurel committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    # get the cash inventory document
    self.monetary_issue = getattr(self.monetary_issue_module, 'monetary_issue')
    # check its portal type
    self.assertEqual(self.monetary_issue.getPortalType(), 'Monetary Issue')
    # check that its source is caisse_1
    self.assertEqual(self.monetary_issue.getSource(), self.reception.getRelativeUrl())
    # check that its destination is caisse_2
    self.assertEqual(self.monetary_issue.getDestination(), self.issue.getRelativeUrl())


  def stepCreateCashContainer(self, sequence=None, sequence_list=None, **kwd):
    """
    Create the cash container
    """
    # get the cash container item from the monetary reception
    cash_container_item_list = [x.getObject() for x in self.simulation_tool.getCurrentTrackingList(node=self.reception.getRelativeUrl())]
    self.assertEqual(len(cash_container_item_list), 2)
231 232 233
    def reference_sort(a,b):                                                                                   return cmp(a.getReference(),b.getReference())
    cash_container_item_list.sort(reference_sort)

Aurel's avatar
Aurel committed
234 235 236 237 238 239 240
    # contruct list of dict to create cash container
    new_cash_container_list = []
    i = 1
    for cash_container in cash_container_item_list:
      # register cash container on self to check aggregate value later
      setattr(self, 'cash_container_item_%s' %(str(i),), cash_container)
      container_dict = {}
241
      container_dict['id'] = str(i)
Aurel's avatar
Aurel committed
242 243 244 245 246 247 248 249
      container_dict['reference'] = cash_container.getReference()
      container_dict['range_start'] = cash_container.getCashNumberRangeStart()
      container_dict['range_stop'] = cash_container.getCashNumberRangeStop()
      cash_container_line = cash_container.objectValues()[0]
      container_dict['quantity'] = cash_container_line.getQuantity()
      container_dict['aggregate'] = cash_container
      new_cash_container_list.append(container_dict)
      i+=1
250 251 252 253
    def reference_sort(a,b):
      return cmp(a['reference'],b['reference'])
    new_cash_container_list.sort(reference_sort)

Aurel's avatar
Aurel committed
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

    global_dict = {}
    global_dict['emission_letter'] = 'p'
    global_dict['variation'] = '2003'
    global_dict['cash_status'] = 'new_not_emitted'
    global_dict['resource'] = self.billet_10000

    self.createCashContainer(self.monetary_issue, 'Monetary Issue Container', global_dict, new_cash_container_list, 'Monetary Issue Line')
    # execute tic
    self.stepTic()
    # check there is two line created
    self.assertEqual(len(self.monetary_issue.objectValues()), 3)


  def stepCheckCashDeliveryLine(self, sequence=None, sequence_list=None):
    """
    Check the cash delivery line
    """
    # check the cash delivery line
    self.valid_line_1 = getattr(self.monetary_issue, 'movement')
    # check its portal type
    self.assertEqual(self.valid_line_1.getPortalType(), 'Monetary Issue Line')
    # check the resource is banknotes of 10000
    self.assertEqual(self.valid_line_1.getResourceValue(), self.billet_10000)
    # chek the value of the banknote
    self.assertEqual(self.valid_line_1.getPrice(), 10000.0)
    # check the unit of banknote
Sebastien Robin's avatar
Sebastien Robin committed
281
    self.assertEqual(self.valid_line_1.getQuantityUnit(), 'unit')
Aurel's avatar
Aurel committed
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 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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    # check the destination variation text is redefine on destination
    self.assertEqual(self.valid_line_1.getBaobabDestinationVariationText(), 'cash_status/new_emitted\nemission_letter/p\nvariation/2003')
    # check we have one delivery cells
    self.assertEqual(len(self.valid_line_1.objectValues()), 1)
    # now check for variation 2003
    variation = 'variation/2003'
    # get the delivery cell
    cell = self.valid_line_1.getCell('emission_letter/p', variation, 'cash_status/new_not_emitted')
    # chek portal types
    self.assertEqual(cell.getPortalType(), 'Cash Delivery Cell')
    # check the banknote of the cell is banknote of 10000
    self.assertEqual(cell.getResourceValue(), self.billet_10000)
    # check the source vault is caisse_1
    self.assertEqual(cell.getSourceValue(), self.reception)
    # check the destination vault is caisse_2
    self.assertEqual(cell.getDestinationValue(), self.issue)
    self.assertEqual(cell.getQuantity(), 200.0)


  def stepCheckCashContainer1(self, sequence=None, sequence_list=None):
    """
    Check the cash container
    """
    # check the cash delivery line
    self.container_1 = getattr(self.monetary_issue, '1')
    # check its portal type
    self.assertEqual(self.container_1.getPortalType(), 'Monetary Issue Container')
    self.assertEqual(self.container_1.getReference(), 'unit_test_1')
    self.assertEqual(self.container_1.getCashNumberRangeStart(), '0')
    self.assertEqual(self.container_1.getCashNumberRangeStop(), '100')
    self.assertEqual(len(self.container_1.getAggregateValueList()), 1)
    self.assertEqual(self.container_1.getAggregateValueList()[0], self.cash_container_item_1)
#    self.failUnless(self.container_1.getAggregateValueList()[0] == self.cash_container_item_1 or self.container_1.getAggregateValueList()[0] == self.cash_container_item_2)
    self.assertEqual(len(self.container_1.objectIds()), 1)
    # now get the line and check it
    self.container_line_1 = self.container_1.objectValues()[0]
    self.assertEqual(self.container_line_1.getPortalType(), 'Container Line')
    # check the resource is banknotes of 10000
    self.assertEqual(self.container_line_1.getResourceValue(), self.billet_10000)
    # check we have one delivery cells
    self.assertEqual(len(self.container_1.objectValues()), 1)
    # now check for variation 2003
    cell = self.container_line_1.objectValues()[0]
    # chek portal types
    self.assertEqual(cell.getPortalType(), 'Container Cell')
    # check the banknote of the cell is banknote of 10000
    self.assertEqual(cell.getResourceValue(), self.billet_10000)
    # chek the value of the banknote
    self.assertEqual(cell.getPrice(), 10000.0)
    # check the source vault is caisse_1
    self.assertEqual(cell.getSourceValue(), self.reception)
    # check the destination vault is caisse_2
    self.assertEqual(cell.getDestinationValue(), self.issue)
    self.assertEqual(cell.getQuantity(), 100.0)


  def stepCheckCashContainer2(self, sequence=None, sequence_list=None):
    """
    Check the cash container
    """
    # check the cash delivery line
    self.container_2 = getattr(self.monetary_issue, '2')
    # check its portal type
    self.assertEqual(self.container_2.getPortalType(), 'Monetary Issue Container')
    self.assertEqual(self.container_2.getReference(), 'unit_test_2')
    self.assertEqual(self.container_2.getCashNumberRangeStart(), '100')
    self.assertEqual(self.container_2.getCashNumberRangeStop(), '200')
    self.assertEqual(len(self.container_2.getAggregateValueList()), 1)
    self.assertEqual(self.container_2.getAggregateValueList()[0], self.cash_container_item_2)
#    self.failUnless(self.container_2.getAggregateValueList()[0] == self.cash_container_item_2 or self.container_2.getAggregateValueList()[0] == self.cash_container_item_1)
    self.assertEqual(len(self.container_2.objectIds()), 1)
    # now get the line and check it
    self.container_line_2 = self.container_2.objectValues()[0]
    self.assertEqual(self.container_line_2.getPortalType(), 'Container Line')
    # check the resource is banknotes of 10000
    self.assertEqual(self.container_line_2.getResourceValue(), self.billet_10000)
    # check we have one delivery cells
    self.assertEqual(len(self.container_2.objectValues()), 1)
    # now check for variation 2003
    cell = self.container_line_2.objectValues()[0]
    # chek portal types
    self.assertEqual(cell.getPortalType(), 'Container Cell')
    # check the banknote of the cell is banknote of 10000
    self.assertEqual(cell.getResourceValue(), self.billet_10000)
    # chek the value of the banknote
    self.assertEqual(cell.getPrice(), 10000.0)
    # check the source vault is caisse_1
    self.assertEqual(cell.getSourceValue(), self.reception)
    # check the destination vault is caisse_2
    self.assertEqual(cell.getDestinationValue(), self.issue)
    self.assertEqual(cell.getQuantity(), 100.0)


  def stepConfirmMonetaryIssue(self, sequence=None, sequence_list=None, **kwd):
    """
    confirm the monetary issue
    """
    self.monetary_issue.setSourceTotalAssetPrice('2000000.0')
    state = self.monetary_issue.getSimulationState()
    # check that state is draft
382
    self.assertEqual(state, 'empty')
Aurel's avatar
Aurel committed
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
    self.workflow_tool.doActionFor(self.monetary_issue, 'confirm_action', wf_id='monetary_issue_workflow')
    self.assertEqual(self.monetary_issue.getSimulationState(), 'confirmed')
    workflow_history = self.workflow_tool.getInfoFor(ob=self.monetary_issue, name='history', wf_id='monetary_issue_workflow')
    # check len of len workflow history is 6
    self.assertEqual(len(workflow_history), 3)


  def stepCheckConfirmedInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check that compution of inventory at vault caisse_2 is right after confirm and before deliver
    """
    # check source
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.reception.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.reception.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)
    # check destination
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.issue.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.issue.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)


  def stepCheckConfirmedContainerInventory(self, sequence=None, sequence_list=None, **kw):
    """
    Check cash container in item table
    """
    self.assertEqual(len(self.simulation_tool.getCurrentTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 2)
    self.assertEqual(len(self.simulation_tool.getFutureTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 0)


  def stepDeliverMonetaryIssue(self, sequence=None, sequence_list=None, **kw):
    """
    Deliver the monetary issue
    """
    state = self.monetary_issue.getSimulationState()
    # check that state is draft
    self.assertEqual(state, 'confirmed')
    self.workflow_tool.doActionFor(self.monetary_issue, 'deliver_action', wf_id='monetary_issue_workflow')
    # execute tic
    self.stepTic()
    # get state of cash sorting
    state = self.monetary_issue.getSimulationState()
    # check that state is delivered
    self.assertEqual(state, 'delivered')
    # get workflow history
    workflow_history = self.workflow_tool.getInfoFor(ob=self.monetary_issue, name='history', wf_id='monetary_issue_workflow')
    # check len of len workflow history is 6
    self.assertEqual(len(workflow_history), 5)


  def stepCheckFinalInventory(self, sequence=None, sequence_list=None, **kwd):
    """
    Check that compution of inventory at vault caisse_2 is right after confirm and before deliver
    """
    # check source
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.reception.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.reception.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 0.0)
    # check destination
    self.assertEqual(self.simulation_tool.getCurrentInventory(node=self.issue.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)
    self.assertEqual(self.simulation_tool.getFutureInventory(node=self.issue.getRelativeUrl(), resource = self.billet_10000.getRelativeUrl()), 200.0)


  def stepCheckFinalContainerInventory(self, sequence=None, sequence_list=None, **kw):
    """
    Check cash container in item table
    """
    self.assertEqual(len(self.simulation_tool.getCurrentTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 0)
    self.assertEqual(len(self.simulation_tool.getFutureTrackingList(at_date=self.current_date, node=self.reception.getRelativeUrl())), 0)


  ##################################
  ##  Tests
  ##################################

  def test_01_ERP5BankingMonetaryIssue(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
Sebastien Robin's avatar
Sebastien Robin committed
461 462 463
    sequence_string = 'Tic CheckObjects  CreateMonetaryReception Tic ' \
                      + 'CheckInitialInventory ' \
                      + 'CheckInitialContainerInventory ' \
Aurel's avatar
Aurel committed
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
                      + 'CreateMonetaryIssue Tic ' \
                      + 'CreateCashContainer Tic CheckCashDeliveryLine ' \
                      + 'CheckCashContainer1 CheckCashContainer2 ' \
                      + 'ConfirmMonetaryIssue Tic CheckConfirmedInventory CheckConfirmedContainerInventory ' \
                      + 'DeliverMonetaryIssue Tic CheckFinalInventory CheckFinalContainerInventory'
    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(TestERP5BankingMonetaryIssue))
    return suite