testCalendar.py 59.8 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
##############################################################################
#
# Copyright (c) 2007 Nexedi SA 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

30
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5ReportTestCase
31 32
from AccessControl.SecurityManagement import newSecurityManager
from Products.ERP5Type.tests.Sequence import SequenceList
33 34
from DateTime import DateTime

35
class TestCalendar(ERP5ReportTestCase):
36 37 38 39 40 41 42

  run_all_test = 1
  person_portal_type = "Person"
  group_calendar_portal_type = "Group Calendar"
  leave_request_portal_type = "Leave Request"
  group_presence_period_portal_type = "Group Presence Period"
  leave_request_period_portal_type = "Leave Request Period"
43
  start_date = DateTime(DateTime().ISO8601())
44 45 46 47 48 49 50 51 52 53
  stop_date = start_date + 0.5
  middle_date = start_date + 0.25
  periodicity_stop_date = start_date + 2

  def getTitle(self):
    return "Calendar"

  def getBusinessTemplateList(self):
    """
    """
54
    return ('erp5_base', 'erp5_pdm', 'erp5_calendar', 'erp5_core_proxy_field_legacy')
55 56 57

  def login(self, quiet=0, run=run_all_test):
    uf = self.getPortal().acl_users
58
    uf._doAddUser('rc', '', ['Manager', 'Author', 'Assignor',
59 60 61 62 63 64 65 66 67
                             'Assignee', 'Auditor'], [])
    user = uf.getUserById('rc').__of__(uf)
    newSecurityManager(None, user)

  def createCategories(self):
    """
      Light install create only base categories, so we create
      some categories for testing them
    """
68
    cal_type_category_list = ['type1', 'type2', 'type3']
69 70 71 72 73 74
    if len(self.category_tool.calendar_period_type.contentValues()) == 0 :
      for category_id in cal_type_category_list:
        o = self.category_tool.calendar_period_type.newContent(
                         portal_type='Category',
                         id=category_id)

75 76 77 78
    if 'my_group' not in self.category_tool.group.contentIds():
      self.category_tool.group.newContent(portal_type='Category',
                                          id='my_group')

79 80 81 82 83 84 85 86
  def createService(self):
    """
    Create service that will be used to fill stock
    """
    module = self.portal.service_module
    if getattr(module, 'consulting_service', None) is None:
      module.newContent(id='consulting_service', title='Consulting Service')

87

88
  def afterSetUp(self):
89 90 91 92 93
    """
    Fake after setup
    """
    self.category_tool = self.getCategoryTool()
    self.createCategories()
94
    self.createService()
95 96 97 98 99 100 101 102 103 104 105 106
    # activate constraints
    self._addPropertySheet('Group Calendar', 'CalendarConstraint')
    self._addPropertySheet('Presence Request', 'CalendarConstraint')
    self._addPropertySheet('Leave Request', 'CalendarConstraint')

    self._addPropertySheet('Presence Request', 'IndividualCalendarConstraint')
    self._addPropertySheet('Leave Request', 'IndividualCalendarConstraint')

    self._addPropertySheet('Leave Request Period', 'CalendarPeriodConstraint')
    self._addPropertySheet('Presence Request Period', 'CalendarPeriodConstraint')
    self._addPropertySheet('Group Presence Period', 'CalendarPeriodConstraint')

107
    # regenerate accessors after category changes & portal type changes
108
    self.commit()
109

110
  def beforeTearDown(self):
111
    self.abort()
112 113 114 115 116
    for module in (self.portal.group_calendar_module,
                   self.portal.leave_request_module,
                   self.portal.presence_request_module,):
      module.manage_delObjects(list(module.objectIds()))
    self.tic()
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
  def stepCreatePerson(self, sequence=None, sequence_list=None, **kw):
    """
    Create an person
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.person_portal_type)
    person = module.newContent(portal_type=self.person_portal_type)
    sequence.edit(
        person=person,
    )

  def stepGetLastCreatedPerson(self, sequence=None, sequence_list=None, **kw):
    """
    Create an person
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.person_portal_type)
    id_list = module.contentIds()
    # More than last, random...
    person = getattr(module, id_list[-1])
    sequence.edit(
        person=person,
    )

142
  def stepCreateGroupCalendar(self, sequence=None,
143 144 145 146 147 148 149 150 151 152 153
                                 sequence_list=None, **kw):
    """
    Create an personal calendar
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.group_calendar_portal_type)
    pc = module.newContent(portal_type=self.group_calendar_portal_type)
    sequence.edit(
        group_calendar=pc,
    )

154
  def stepSetGroupCalendarAssignment(self, sequence=None,
155 156 157 158 159 160
                                    sequence_list=None, **kw):
    """
    Set the source
    """
    group_calendar = sequence.get('group_calendar')
    person = sequence.get('person')
161 162 163 164 165 166 167 168
    assignment = self.portal.group_calendar_assignment_module.newContent(
                      destination_value=person,
                      resource_value=self.portal.service_module.consulting_service,
                      start_date=self.start_date,
                      stop_date=self.stop_date,
                      specialise_value=group_calendar)
    assignment.confirm()
    sequence.edit(assignment=assignment)
169

170
  def stepCreateGroupPresencePeriod(self, sequence=None,
171 172 173 174 175 176 177 178 179 180 181 182 183
                                      sequence_list=None, **kw):
    """
    Create an personal calendar period
    """
    group_calendar = sequence.get('group_calendar')
    group_presence_period = group_calendar.newContent(
        portal_type=self.group_presence_period_portal_type,
        resource='calendar_period_type/type1',
    )
    sequence.edit(
        group_presence_period=group_presence_period,
    )

184
  def stepSetGroupPresencePeriodValues(self, sequence=None,
185 186 187 188 189 190
                                         sequence_list=None, **kw):
    """
    Set values on personal calendar period
    """
    group_presence_period = sequence.get('group_presence_period')

191
  def stepSetGroupPresencePeriodDates(self, sequence=None,
192 193 194 195 196 197 198 199 200
                                        sequence_list=None, **kw):
    """
    Set values on personal calendar period
    """
    group_presence_period = sequence.get('group_presence_period')
    group_presence_period.edit(
      start_date=self.start_date,
      stop_date=self.stop_date,
    )
201 202
    # XXX(Seb), replace by interaction workflow
    #sequence.get("assignment").reindexObject()
203

204
  def stepSetGroupPresencePeriodPerStopDate(self, sequence=None,
205 206 207 208 209 210 211 212 213
                                        sequence_list=None, **kw):
    """
    Set values on personal calendar period
    """
    group_presence_period = sequence.get('group_presence_period')
    group_presence_period.edit(
      periodicity_stop_date=self.periodicity_stop_date,
    )

214
  def stepSetGroupCalendarAssignmentToCheck(self, sequence=None,
215 216 217 218
                                          sequence_list=None, **kw):
    """
    Set personal calendar period to check
    """
219 220
    assignment = sequence.get('assignment')
    sequence.edit(obj_to_check=assignment)
221

222
  def stepSetGroupCalendarEventPerStopDate(self, sequence=None,
223 224 225 226 227 228 229 230 231
                                        sequence_list=None, **kw):
    """
    Set values on personal calendar event
    """
    group_calendar_event = sequence.get('group_calendar_event')
    group_calendar_event.edit(
      periodicity_stop_date=self.periodicity_stop_date,
    )

232
  def stepConfirmGroupCalendar(self, sequence=None,
233 234 235 236 237
                               sequence_list=None, **kw):
    """
    Confirm group calendar
    """
    group_calendar = sequence.get('group_calendar')
238
    group_calendar.confirm()
239
    self.assertEqual('confirmed', group_calendar.getSimulationState())
240

241
  def stepConfirmActionGroupCalendar(self, sequence=None,
242 243 244 245 246
                               sequence_list=None, **kw):
    """
    Confirm group calendar with user interface transition
    """
    group_calendar = sequence.get('group_calendar')
247 248 249 250
    self.portal.portal_workflow.doActionFor(
                          group_calendar,
                          'confirm_action',
                          'group_calendar_workflow')
251
    self.assertEqual('confirmed', group_calendar.getSimulationState())
252 253


254
  def stepCreateLeaveRequest(self, sequence=None,
255 256 257 258 259 260 261 262 263 264 265
                                 sequence_list=None, **kw):
    """
    Create a personal calendar
    """
    portal = self.getPortal()
    module = portal.getDefaultModule(self.leave_request_portal_type)
    pc = module.newContent(portal_type=self.leave_request_portal_type)
    sequence.edit(
        leave_request=pc,
    )

266
  def stepSetLeaveRequestDestination(self, sequence=None,
267 268 269 270 271 272 273 274
                                         sequence_list=None, **kw):
    """
    Set the destination
    """
    leave_request = sequence.get('leave_request')
    person = sequence.get('person')
    leave_request.setDestinationValue(person)

275
  def stepCreatePersonalLeavePeriod(self, sequence=None,
276 277 278 279 280 281 282 283 284 285 286 287 288
                                    sequence_list=None, **kw):
    """
    Create an personal calendar period
    """
    leave_request = sequence.get('leave_request')
    personal_leave_period = leave_request.newContent(
        portal_type=self.leave_request_period_portal_type,
        resource='calendar_period_type/type1',
    )
    sequence.edit(
        personal_leave_period=personal_leave_period,
    )

289
  def stepSetPersonalLeavePeriodToCheck(self, sequence=None,
290 291 292 293 294 295 296
                                        sequence_list=None, **kw):
    """
    Set personal leave period to check
    """
    personal_leave_period = sequence.get('personal_leave_period')
    sequence.edit(obj_to_check=personal_leave_period)

297
  def stepSetPersonalLeavePeriodValues(self, sequence=None,
298 299 300 301 302 303
                                       sequence_list=None, **kw):
    """
    Set values on personal calendar event
    """
    personal_leave_period = sequence.get('personal_leave_period')

304
  def stepSetPersonalLeavePeriodDates(self, sequence=None,
305 306 307 308 309 310 311 312 313 314
                                      sequence_list=None, **kw):
    """
    Set values on personal calendar
    """
    personal_leave_period = sequence.get('personal_leave_period')
    personal_leave_period.edit(
      start_date=self.start_date,
      stop_date=self.stop_date,
    )

315
  def stepSetPersonalLeavePeriodPerStopDate(self, sequence=None,
316 317 318 319 320 321 322 323 324
                                            sequence_list=None, **kw):
    """
    Set values on personal calendar event
    """
    personal_leave_period = sequence.get('personal_leave_period')
    personal_leave_period.edit(
      periodicity_stop_date=self.periodicity_stop_date,
    )

325
  def stepPlanLeaveRequest(self, sequence=None,
326 327 328 329 330
                               sequence_list=None, **kw):
    """
    Plan personal calendar
    """
    leave_request = sequence.get('leave_request')
331
    leave_request.plan()
332
    self.assertEqual('planned', leave_request.getSimulationState())
333

334
  def stepConfirmLeaveRequest(self, sequence=None,
335 336 337 338 339
                               sequence_list=None, **kw):
    """
    Confirm personal calendar
    """
    leave_request = sequence.get('leave_request')
340
    leave_request.confirm()
341
    self.assertEqual('confirmed', leave_request.getSimulationState())
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

  def getSqlUidList(self):
    """
    Give the full list of path in the catalog
    """
    sql_connection = self.getSQLConnection()
    sql = 'select uid from stock'
    result = sql_connection.manage_test(sql)
    uid_list = [x.uid for x in result]
    return uid_list

  def getSqlMovementUidList(self):
    """
    Give the full list of path in the catalog
    """
    sql_connection = self.getSQLConnection()
    sql = 'select uid from movement'
    result = sql_connection.manage_test(sql)
    uid_list = [x.uid for x in result]
    return uid_list

363
  def stepCheckNotCatalogued(self, sequence=None,
364 365 366 367 368 369 370 371
                             sequence_list=None, **kw):
    """
    Create an personal calendar period
    """
    uid_list = self.getSqlUidList()
    obj_to_check = sequence.get('obj_to_check')
    self.assertFalse(obj_to_check.getUid() in uid_list)

372
  def stepCheckCatalogued(self, sequence=None,
373 374 375 376 377 378 379 380
                          sequence_list=None, **kw):
    """
    Create an personal calendar period
    """
    uid_list = self.getSqlUidList()
    obj_to_check = sequence.get('obj_to_check')
    self.assertTrue(obj_to_check.getUid() in uid_list)

381
#     self.assertEqual(len(obj_to_check.getDatePeriodList()),
382 383
#                       uid_list.count(obj_to_check.getUid()))

384
  def stepCheckCataloguedAsMovement(self, sequence=None,
385 386 387 388 389 390 391
                                    sequence_list=None, **kw):
    """
    Create an personal calendar period
    """
    uid_list = self.getSqlMovementUidList()
    obj_to_check = sequence.get('obj_to_check')
    self.assertTrue(obj_to_check.getUid() in uid_list)
392
#     self.assertEqual(len(obj_to_check.getDatePeriodList()),
393 394 395 396 397 398 399
#                       uid_list.count(obj_to_check.getUid()))

  def test_01_CatalogCalendarPeriod(self, quiet=0, run=run_all_test):
    """
    Test indexing
    """
    if not run: return
400

401 402 403 404
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
405
              SetGroupCalendarAssignment \
406 407 408
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
409
              SetGroupCalendarAssignmentToCheck \
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
              CheckNotCatalogued \
              ConfirmGroupCalendar \
              Tic \
              CheckNotCatalogued \
              SetGroupPresencePeriodDates \
              Tic \
              CheckCatalogued \
              SetGroupPresencePeriodPerStopDate \
              Tic \
              CheckCatalogued \
              '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

  def test_02_CatalogLeaveRequest(self, quiet=0, run=run_all_test):
    """
    Test indexing
    """
    if not run: return
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
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateLeaveRequest \
              SetLeaveRequestDestination \
              CreatePersonalLeavePeriod \
              SetPersonalLeavePeriodValues \
              Tic \
              SetPersonalLeavePeriodToCheck \
              CheckNotCatalogued \
              PlanLeaveRequest \
              ConfirmLeaveRequest \
              Tic \
              CheckNotCatalogued \
              SetPersonalLeavePeriodDates \
              Tic \
              CheckCatalogued \
              SetPersonalLeavePeriodPerStopDate \
              Tic \
              CheckCatalogued \
              '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

454
  def stepCheckGetTimeAvailability(self, sequence=None,
455 456 457 458 459 460 461 462 463 464 465 466
                                   sequence_list=None, **kw):
    """
    Check getTimeAvailability
    """
    obj_to_check = sequence.get('obj_to_check')
    person = sequence.get('person')
    start_date = self.start_date
    stop_date = self.stop_date
    second_availability = int(stop_date) - int(start_date)
    date_period_list = obj_to_check._getDatePeriodList()

    # Check 1 period
467
    self.assertEqual(second_availability,
468
                      person.getAvailableTime(from_date=start_date,
469
                                              to_date=stop_date))
470
    self.assertEqual(second_availability,
471
                      person.getAvailableTime(from_date=start_date,
472
                                              to_date=stop_date))
473
    self.assertEqual(second_availability / 2,
474
                      person.getAvailableTime(from_date=start_date,
475
                                              to_date=self.middle_date))
476
    self.assertEqual(second_availability / 2,
477
                      person.getAvailableTime(from_date=self.middle_date,
478 479
                                                     to_date=stop_date))
    # Check 2 periods
480
    self.assertEqual(2 * second_availability,
481
                      person.getAvailableTime(
482
                                         from_date=start_date,
483 484
                                         to_date=date_period_list[1][1]))
#     # Check all periods
485
#     self.assertEqual(len(date_period_list) * second_availability,
486 487
#                       person.getAvailableTime())

488
  def stepCheckDoubleGetTimeAvailability(self, sequence=None,
489 490 491 492 493 494 495 496 497 498 499 500 501 502
                                         sequence_list=None, **kw):
    """
    Check getTimeAvailability
    """
    obj_to_check = sequence.get('obj_to_check')
    person = sequence.get('person')
    start_date = self.start_date
    stop_date = self.stop_date
    second_availability = int(stop_date) - int(start_date)
    # Hop, here is the trick
    second_availability = 2 * second_availability
    date_period_list = obj_to_check._getDatePeriodList()

    # Check 1 period
503
    self.assertEqual(second_availability,
504
                      person.getAvailableTime(from_date=start_date,
505 506
                                                 to_date=stop_date))
    # Check 2 periods
507
    self.assertEqual(2 * second_availability,
508
                      person.getAvailableTime(
509
                                         from_date=start_date,
510 511
                                         to_date=date_period_list[1][1]))
#     # Check all periods
512
#     self.assertEqual(len(date_period_list) * second_availability,
513 514
#                       person.getAvailableTime())

515
  def stepCheckPersonalTimeAvailability(self, sequence=None,
516 517 518 519 520 521 522 523 524 525 526 527
                                   sequence_list=None, **kw):
    """
    Check getTimeAvailability
    """
    obj_to_check = sequence.get('obj_to_check')
    person = sequence.get('person')
    start_date = self.start_date
    stop_date = self.stop_date
    second_availability = 0
    date_period_list = obj_to_check._getDatePeriodList()

    # Check 1 period
528
    self.assertEqual(second_availability,
529
                      person.getAvailableTime(from_date=start_date,
530
                                              to_date=stop_date))
531
    self.assertEqual(second_availability,
532
                      person.getAvailableTime(from_date=start_date,
533
                                              to_date=stop_date))
534
    self.assertEqual(second_availability / 2,
535
                      person.getAvailableTime(from_date=start_date,
536
                                              to_date=self.middle_date))
537
    self.assertEqual(second_availability / 2,
538
                      person.getAvailableTime(from_date=self.middle_date,
539 540
                                                     to_date=stop_date))
    # Check 2 periods
541
    self.assertEqual(2 * second_availability,
542
                      person.getAvailableTime(
543
                                         from_date=start_date,
544 545
                                         to_date=date_period_list[1][1]))
#     # Check all periods
546
#     self.assertEqual(len(date_period_list) * second_availability,
547 548
#                       person.getAvailableTime())

549
  def stepCheckCumulativeTimeAvailability(self, sequence=None,
550 551 552 553 554 555 556 557 558 559 560 561
                                          sequence_list=None, **kw):
    """
    Check getTimeAvailability
    """
    obj_to_check = sequence.get('obj_to_check')
    person = sequence.get('person')
    start_date = self.start_date
    stop_date = self.stop_date
    second_availability = int(stop_date) - int(start_date)
    date_period_list = obj_to_check._getDatePeriodList()

    # Check 1 period
562
    self.assertEqual(0,
563
                      person.getAvailableTime(from_date=start_date,
564 565
                                              to_date=stop_date))
    # Check 2 periods
566
    self.assertEqual(second_availability,
567
                      person.getAvailableTime(
568
                                         from_date=start_date,
569 570
                                         to_date=date_period_list[1][1]))
#     # Check all periods
571
#     self.assertEqual(len(date_period_list) * second_availability,
572 573 574 575 576 577 578
#                       person.getAvailableTime())

  def test_03_getAvailableTime(self, quiet=0, run=run_all_test):
    """
    Test indexing
    """
    if not run: return
579

580 581 582 583 584
    # Test that calendar group increase time availability
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
585
              SetGroupCalendarAssignment \
586 587 588
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
589
              SetGroupCalendarAssignmentToCheck \
590 591
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
592
              ConfirmActionGroupCalendar \
593 594 595 596 597
              Tic \
              CheckCatalogued \
              CheckGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
598

599 600 601 602 603
    # Test getTimeAvailability does not interfere with other documents
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
604
              SetGroupCalendarAssignment \
605 606 607
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
608
              SetGroupCalendarAssignmentToCheck \
609 610
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
611
              ConfirmActionGroupCalendar \
612 613 614 615 616 617
              Tic \
              CheckCatalogued \
              CheckGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)
618

619 620 621 622 623
    # Test getTimeAvailability is cumulative
    sequence_list = SequenceList()
    sequence_string = '\
              GetLastCreatedPerson \
              CreateGroupCalendar \
624
              SetGroupCalendarAssignment \
625 626 627
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
628
              SetGroupCalendarAssignmentToCheck \
629 630 631
              PlanGroupCalendar \
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
632
              ConfirmActionGroupCalendar \
633 634 635 636 637
              Tic \
              CheckCatalogued \
              CheckDoubleGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
638

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    # Test that leave period decrease time availability
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateLeaveRequest \
              SetLeaveRequestDestination \
              CreatePersonalLeavePeriod \
              SetPersonalLeavePeriodValues \
              Tic \
              SetPersonalLeavePeriodToCheck \
              PlanLeaveRequest \
              ConfirmLeaveRequest \
              SetPersonalLeavePeriodDates\
              SetPersonalLeavePeriodPerStopDate\
              Tic \
              CheckCatalogued \
              CheckPersonalTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
658

659 660 661 662 663
    # Combine object
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
664
              SetGroupCalendarAssignment \
665 666 667
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
668
              SetGroupCalendarAssignmentToCheck \
669 670
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
671
              ConfirmActionGroupCalendar \
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
              Tic \
              CheckCatalogued \
              CheckGetTimeAvailability \
              \
              CreateLeaveRequest \
              SetLeaveRequestDestination \
              CreatePersonalLeavePeriod \
              SetPersonalLeavePeriodValues \
              Tic \
              PlanLeaveRequest \
              ConfirmLeaveRequest \
              SetPersonalLeavePeriodDates\
              Tic \
              CheckCumulativeTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)

    sequence_list.play(self)

  def test_04_getCapacityAvailability(self, quiet=0, run=0):
    """
    Test getCapacityAvailability
    """
    if not run: return
696
    raise NotImplementedError
697

698 699 700 701 702
    # Test that calendar group increase time availability
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
703
              SetGroupCalendarAssignment \
704 705 706
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
707
              SetGroupCalendarAssignmentToCheck \
708 709 710 711 712 713 714 715
              ConfirmGroupCalendar \
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
              Tic \
              CheckCatalogued \
              CheckGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
716

717 718 719 720 721
    # Test getTimeAvailability does not interfere with other documents
    sequence_list = SequenceList()
    sequence_string = '\
              CreatePerson \
              CreateGroupCalendar \
722
              SetGroupCalendarAssignment \
723 724 725
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
726
              SetGroupCalendarAssignmentToCheck \
727 728 729 730 731 732 733 734 735
              ConfirmGroupCalendar \
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
              Tic \
              CheckCatalogued \
              CheckGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)
736

737 738 739 740 741
    # Test getTimeAvailability is cumulative
    sequence_list = SequenceList()
    sequence_string = '\
              GetLastCreatedPerson \
              CreateGroupCalendar \
742
              SetGroupCalendarAssignment \
743 744 745
              CreateGroupPresencePeriod \
              SetGroupPresencePeriodValues \
              Tic \
746
              SetGroupCalendarAssignmentToCheck \
747 748 749 750 751 752 753 754 755 756
              ConfirmGroupCalendar \
              SetGroupPresencePeriodDates \
              SetGroupPresencePeriodPerStopDate \
              Tic \
              CheckCatalogued \
              CheckDoubleGetTimeAvailability \
              '
    sequence_list.addSequenceString(sequence_string)
    sequence_list.play(self)

757 758 759 760
  def test_GroupCalendarConstraint(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    # no lines
761
    self.assertEqual(1, len(group_calendar.checkConsistency()))
762 763 764
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    # invalid line (no dates, no resource)
765
    self.assertEqual(3, len(group_calendar.checkConsistency()))
766 767
    group_calendar_period.setStartDate(self.start_date)
    group_calendar_period.setStopDate(self.stop_date)
768
    self.assertEqual(1, len(group_calendar.checkConsistency()))
769 770
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
771
    self.assertEqual(0, len(group_calendar.checkConsistency()))
772 773 774 775 776

  def test_LeaveRequestCalendarConstraint(self):
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    # no lines, no person
777
    self.assertEqual(2, len(leave_request.checkConsistency()))
778 779 780
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    # no person, invalid line (no dates, no resource)
781
    self.assertEqual(4, len(leave_request.checkConsistency()))
782 783
    leave_request_period.setStartDate(self.start_date)
    leave_request_period.setStopDate(self.stop_date)
784
    self.assertEqual(2, len(leave_request.checkConsistency()))
785 786
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
787
    self.assertEqual(1, len(leave_request.checkConsistency()))
788 789
    person = self.portal.person_module.newContent(portal_type='Person')
    leave_request.setDestinationValue(person)
790
    self.assertEqual(0, len(leave_request.checkConsistency()))
791 792 793 794 795

  def test_PresenceRequestCalendarConstraint(self):
    presence_request = self.portal.presence_request_module.newContent(
                                  portal_type='Presence Request')
    # no lines, no person
796
    self.assertEqual(2, len(presence_request.checkConsistency()))
797 798 799
    presence_request_period = presence_request.newContent(
                                  portal_type='Presence Request Period')
    # no person, invalid line (no dates, no resource)
800
    self.assertEqual(4, len(presence_request.checkConsistency()))
801 802
    presence_request_period.setStartDate(self.start_date)
    presence_request_period.setStopDate(self.stop_date)
803
    self.assertEqual(2, len(presence_request.checkConsistency()))
804 805
    presence_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
806
    self.assertEqual(1, len(presence_request.checkConsistency()))
807 808
    person = self.portal.person_module.newContent(portal_type='Person')
    presence_request.setDestinationValue(person)
809
    self.assertEqual(0, len(presence_request.checkConsistency()))
810

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
  def test_SimpleLeaveRequestWithSameDateAsGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate(self.start_date)
    group_calendar_period.setStopDate(self.stop_date)
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()

    # there is 43200 seconds between self.start_date and self.stop_date
    total_time = 43200

830
    self.assertEqual(total_time, person.getAvailableTime(
831 832
                                from_date=self.start_date-1,
                                to_date=self.stop_date+1))
833
    self.assertEqual([total_time], [x.total_quantity for x in
834 835 836 837 838 839 840 841
                              person.getAvailableTimeSequence(
                                year=1,
                                from_date=self.start_date-1,
                                to_date=self.stop_date+1)])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                                            from_date=self.start_date-1,
                                            to_date=self.stop_date+1)
842 843
    self.assertEqual(1, len(available_time_movement_list))
    self.assertEqual([(self.start_date.ISO(), self.stop_date.ISO())],
844 845 846 847 848 849 850 851 852 853 854 855 856 857
                      [(x.getStartDate().ISO(), x.getStopDate().ISO()) for x in
                          available_time_movement_list])


    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period.setStartDate(self.start_date)
    leave_request_period.setStopDate(self.stop_date)
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
858

859
    self.tic()
860

861
    self.assertEqual(0, person.getAvailableTime(
862 863
                                from_date=self.start_date-1,
                                to_date=self.stop_date+1))
864
    self.assertEqual([0], [x.total_quantity for x in
865 866 867 868
                              person.getAvailableTimeSequence(
                                year=1,
                                from_date=self.start_date-1,
                                to_date=self.stop_date+1)])
869

870 871 872
    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                                            from_date=self.start_date-1,
                                            to_date=self.stop_date+1)
873
    self.assertEqual(0, len(available_time_movement_list))
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897


  def test_LeaveRequestWithSameDateAsGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period_am = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period_am.setStartDate('2008/01/01 08:00')
    group_calendar_period_am.setStopDate('2008/01/01 12:00')
    group_calendar_period_am.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar_period_pm = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period_pm.setStartDate('2008/01/01 14:00')
    group_calendar_period_pm.setStopDate('2008/01/01 18:00')
    group_calendar_period_pm.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)

    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
898

899
    self.assertEqual((18 - 14 + 12 - 8) * 60 * 60, person.getAvailableTime(
900 901
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
902

903
    self.assertEqual([(18 - 14 + 12 - 8) * 60 * 60],
904 905 906 907 908 909 910 911
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
912 913
    self.assertEqual(2, len(available_time_movement_list))
    self.assertEqual(
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
        [(DateTime('2008/01/01 08:00'), DateTime('2008/01/01 12:00')),
         (DateTime('2008/01/01 14:00'), DateTime('2008/01/01 18:00'))],
        [(m.getStartDate(), m.getStopDate()) for m in
          available_time_movement_list])

    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period.setStartDate('2008/01/01 08:00')
    leave_request_period.setStopDate('2008/01/01 18:00')
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
929

930
    self.tic()
931

932
    self.assertEqual(0, person.getAvailableTime(
933 934
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
935

936
    self.assertEqual([0],
937 938 939 940 941 942 943 944
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
945
    self.assertEqual(0, len(available_time_movement_list))
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966


  def test_LeaveRequestWithSameDateAsRepeatedGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    # note that 2008/01/01 was a Tuesday
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar_period.setPeriodicityStopDate('2008/01/30')
    group_calendar_period.setPeriodicityWeekDayList(['Monday'])

    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
967

968
    # 2008/01/07 was a Monday
969
    self.assertEqual((18 - 8) * 60 * 60, person.getAvailableTime(
970 971 972
                            from_date=DateTime(2008, 1, 6).earliestTime(),
                            to_date=DateTime(2008, 1, 7).latestTime()))

973
    self.assertEqual([(18 - 8) * 60 * 60],
974 975 976 977 978 979 980 981
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=2,
                  from_date=DateTime(2008, 1, 6).earliestTime(),
                  to_date=DateTime(2008, 1, 7).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 6).earliestTime(),
                  to_date=DateTime(2008, 1, 7).latestTime())
982 983
    self.assertEqual(1, len(available_time_movement_list))
    self.assertEqual(
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
        [(DateTime('2008/01/07 08:00'), DateTime('2008/01/07 18:00'))],
        [(m.getStartDate(), m.getStopDate()) for m in
          available_time_movement_list])

    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')

    leave_request_period.setStartDate('2008/01/06 08:00')
    leave_request_period.setStopDate('2008/01/07 18:00')
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
999

1000
    self.tic()
1001

1002
    self.assertEqual(0, person.getAvailableTime(
1003 1004
                            from_date=DateTime(2008, 1, 6).earliestTime(),
                            to_date=DateTime(2008, 1, 7).latestTime()))
1005

1006
    self.assertEqual([0],
1007 1008 1009 1010 1011 1012 1013
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=2,
                  from_date=DateTime(2008, 1, 6).earliestTime(),
                  to_date=DateTime(2008, 1, 7).latestTime())])
    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 6).earliestTime(),
                  to_date=DateTime(2008, 1, 7).latestTime())
1014
    self.assertEqual(0, len(available_time_movement_list))
1015

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  def test_LeaveRequestOverlappingGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
1031

1032
    self.assertEqual((18 - 8) * 60 * 60, person.getAvailableTime(
1033 1034
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1035

1036
    self.assertEqual([(18 - 8) * 60 * 60],
1037 1038 1039 1040 1041 1042 1043 1044
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1045 1046
    self.assertEqual(1, len(available_time_movement_list))
    self.assertEqual(
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
        [(DateTime('2008/01/01 08:00'), DateTime('2008/01/01 18:00'))],
        [(m.getStartDate(), m.getStopDate()) for m in
          available_time_movement_list])

    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period.setStartDate('2008/01/01 09:00')
    leave_request_period.setStopDate('2008/01/01 17:00')
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1061

1062
    self.tic()
1063

1064
    self.assertEqual((9-8 + 18-17) * 60 * 60, person.getAvailableTime(
1065 1066
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1067

1068
    self.assertEqual([(9-8 + 18-17) * 60 * 60],
1069 1070 1071 1072 1073 1074 1075 1076
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1077 1078
    self.assertEqual(2, len(available_time_movement_list))
    self.assertEqual(
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
        [(DateTime('2008/01/01 08:00'),
          DateTime('2008/01/01 09:00')),
         (DateTime('2008/01/01 17:00'),
          DateTime('2008/01/01 18:00'))],
        [(m.getStartDate(), m.getStopDate()) for m in
                        available_time_movement_list])

  def test_LeaveRequestOverlappingBeforeGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period.setStartDate('2008/01/01 07:00')
    leave_request_period.setStopDate('2008/01/01 09:00')
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1111

1112
    self.tic()
1113

1114
    self.assertEqual((18-9) * 60 * 60, person.getAvailableTime(
1115 1116
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1117

1118
    self.assertEqual([(18-9) * 60 * 60],
1119 1120 1121 1122 1123 1124 1125 1126
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1127
    self.assertEqual(
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
        [(DateTime('2008/01/01 09:00'),
          DateTime('2008/01/01 18:00'),)],
        [(m.getStartDate(), m.getStopDate()) for m in
                        available_time_movement_list])

  def test_LeaveRequestOverlappingAfterGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period.setStartDate('2008/01/01 17:00')
    leave_request_period.setStopDate('2008/01/01 19:00')
    leave_request_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1158

1159
    self.tic()
1160

1161
    self.assertEqual((17-8) * 60 * 60, person.getAvailableTime(
1162 1163
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1164

1165
    self.assertEqual([(17-8) * 60 * 60],
1166 1167 1168 1169 1170 1171 1172 1173
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1174
    self.assertEqual(
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
        [(DateTime('2008/01/01 08:00'),
          DateTime('2008/01/01 17:00'),)],
        [(m.getStartDate(), m.getStopDate()) for m in
                        available_time_movement_list])

  def test_2LeaveRequestOverlappingAfterGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period_1 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_1.setStartDate('2008/01/01 09:00')
    leave_request_period_1.setStopDate('2008/01/01 10:00')
    leave_request_period_1.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request_period_2 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_2.setStartDate('2008/01/01 12:00')
    leave_request_period_2.setStopDate('2008/01/01 13:00')
    leave_request_period_2.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1211

1212
    self.tic()
1213

1214
    self.assertEqual((18-13 + 12-10 + 9-8) * 60 * 60, person.getAvailableTime(
1215 1216
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1217

1218
    self.assertEqual([(18-13 + 12-10 + 9-8) * 60 * 60],
1219 1220 1221 1222 1223 1224 1225 1226
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1227
    self.assertEqual(
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
        [(DateTime('2008/01/01 08:00'),
          DateTime('2008/01/01 09:00'),),
         (DateTime('2008/01/01 10:00'),
          DateTime('2008/01/01 12:00'),),
         (DateTime('2008/01/01 13:00'),
          DateTime('2008/01/01 18:00'),)],
        [(m.getStartDate(), m.getStopDate()) for m in
                        available_time_movement_list])

  def test_2ConsecutiveLeaveRequestOverlappingAfterGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period_1 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_1.setStartDate('2008/01/01 09:00')
    leave_request_period_1.setStopDate('2008/01/01 10:00')
    leave_request_period_1.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request_period_2 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_2.setStartDate('2008/01/01 10:00')
    leave_request_period_2.setStopDate('2008/01/01 11:00')
    leave_request_period_2.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1268

1269
    self.tic()
1270

1271
    self.assertEqual((18-11 + 9-8) * 60 * 60, person.getAvailableTime(
1272 1273
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1274

1275
    self.assertEqual([(18-11 + 9-8) * 60 * 60],
1276 1277 1278 1279 1280 1281 1282 1283
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1284
    self.assertEqual(
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
        [(DateTime('2008/01/01 08:00'),
          DateTime('2008/01/01 09:00'),),
         (DateTime('2008/01/01 11:00'),
          DateTime('2008/01/01 18:00'),),],
        [(m.getStartDate(), m.getStopDate()) for m in
                        available_time_movement_list])

  def test_2OverlappedLeaveRequestOverlappingAfterGroupCalendar(self):
    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person = self.portal.person_module.newContent(portal_type='Person')
    assignment = person.newContent(portal_type='Assignment',
                                   calendar_value=group_calendar)
    self.tic()
    leave_request = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request')
    leave_request_period_1 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_1.setStartDate('2008/01/01 09:00')
    leave_request_period_1.setStopDate('2008/01/01 10:00')
    leave_request_period_1.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request_period_2 = leave_request.newContent(
                                  portal_type='Leave Request Period')
    leave_request_period_2.setStartDate('2008/01/01 09:30')
    leave_request_period_2.setStopDate('2008/01/01 11:00')
    leave_request_period_2.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    leave_request.setDestinationValue(person)
    leave_request.confirm()
1323

1324
    self.tic()
1325

1326
    self.assertEqual((18-11 + 9-8) * 60 * 60, person.getAvailableTime(
1327 1328
                            from_date=DateTime(2008, 1, 1).earliestTime(),
                            to_date=DateTime(2008, 1, 1).latestTime()))
1329

1330
    self.assertEqual([(18-11 + 9-8) * 60 * 60],
1331 1332 1333 1334 1335 1336 1337 1338
      [x.total_quantity for x in person.getAvailableTimeSequence(
                  day=1,
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())])

    available_time_movement_list = person.Person_getAvailableTimeMovementList(
                  from_date=DateTime(2008, 1, 1).earliestTime(),
                  to_date=DateTime(2008, 1, 1).latestTime())
1339
    self.assertEqual(
1340 1341 1342 1343 1344
        [(DateTime('2008/01/01 08:00'),
          DateTime('2008/01/01 09:00'),),
         (DateTime('2008/01/01 11:00'),
          DateTime('2008/01/01 18:00'),),],
        [(m.getStartDate(), m.getStopDate()) for m in
1345 1346
                        available_time_movement_list])

1347

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
  def test_PersonModule_viewLeaveRequestReport(self):
    # in this test, type1 is the type for presences, type2 & type3 are types
    # for leaves.
    organisation = self.portal.organisation_module.newContent(
                                portal_type='Organisation',
                                group='my_group')

    group_calendar = self.portal.group_calendar_module.newContent(
                                  portal_type='Group Calendar')
    group_calendar_period = group_calendar.newContent(
                                  portal_type='Group Presence Period')
    group_calendar_period.setStartDate('2008/01/01 08:00')
    group_calendar_period.setStopDate('2008/01/01 18:00')
    group_calendar_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type1)
    group_calendar.confirm()

    person1 = self.portal.person_module.newContent(
                                portal_type='Person',
                                title='Person 1',
                                career_reference='1',
                                subordination_value=organisation)
    assignment = person1.newContent(portal_type='Assignment',
                                    calendar_value=group_calendar)
    leave_request1 = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request',
                                  destination_value=person1)
    leave_request1_period = leave_request1.newContent(
                                  portal_type='Leave Request Period')
    leave_request1_period.setStartDate('2008/01/01 09:00')
    leave_request1_period.setStopDate('2008/01/01 10:00')
    leave_request1_period.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type2)
    leave_request1.confirm()

    person2 = self.portal.person_module.newContent(
                                portal_type='Person',
                                title='Person 2',
                                career_reference='2',
                                subordination_value=organisation)
    assignment = person2.newContent(portal_type='Assignment',
                                    calendar_value=group_calendar)
    leave_request2 = self.portal.leave_request_module.newContent(
                                  portal_type='Leave Request',
                                  destination_value=person2)
    leave_request2_period1 = leave_request2.newContent(
                                  portal_type='Leave Request Period')
    leave_request2_period1.setStartDate('2008/01/01 09:00')
    leave_request2_period1.setStopDate('2008/01/01 10:00')
    leave_request2_period1.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type2)
    leave_request2_period2 = leave_request2.newContent(
                                  portal_type='Leave Request Period')
    leave_request2_period2.setStartDate('2008/01/01 10:00')
    leave_request2_period2.setStopDate('2008/01/01 11:30')
    leave_request2_period2.setResourceValue(
          self.portal.portal_categories.calendar_period_type.type3)
    leave_request2.confirm()

    self.tic()

1409
    # set request variables and render
1410 1411 1412 1413
    request_form = self.portal.REQUEST
    request_form['from_date'] = DateTime(2008, 1, 1)
    request_form['to_date'] = DateTime(2009, 1, 1)
    request_form['node_category'] = 'group/my_group'
1414

1415 1416 1417
    report_section_list = self.getReportSectionList(
                             self.portal.person_module,
                             'PersonModule_viewLeaveRequestReport')
1418
    self.assertEqual(1, len(report_section_list))
1419

1420 1421
    line_list = self.getListBoxLineList(report_section_list[0])
    data_line_list = [l for l in line_list if l.isDataLine()]
1422
    self.assertEqual(2, len(data_line_list))
1423

1424
    self.assertEqual(data_line_list[0].column_id_list,
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
        ['person_career_reference', 'person_title',
         'calendar_period_type/type2', 'calendar_period_type/type3', 'total'])

    self.checkLineProperties(data_line_list[0],
                             person_career_reference='1',
                             person_title='Person 1',
                             total=1.0,
                             **{'calendar_period_type/type2': 1.0,})
    self.checkLineProperties(data_line_list[1],
                             person_career_reference='2',
                             person_title='Person 2',
                             total=2.5,
                             **{'calendar_period_type/type2': 1.0,
                                'calendar_period_type/type3': 1.5,})

1440
    self.assertTrue(line_list[-1].isStatLine())
1441 1442 1443 1444
    self.checkLineProperties(line_list[-1],
                             total=3.5,
                             **{'calendar_period_type/type2': 2.0,
                                'calendar_period_type/type3': 1.5,})
1445

1446 1447


Romain Courteaud's avatar
Romain Courteaud committed
1448 1449 1450 1451 1452
import unittest
def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestCalendar))
  return suite