#!/www/python/bin/python
"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_calendar.py $
$Id: test_calendar.py 23278 2003-12-08 22:14:29Z dbinger $
"""

from mx.DateTime import DateTime, TimeDelta, now
from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.user import DulcineaUser as User
from dulcinea.calendar import Calendar, Reservation, Reservable, Resources, \
     Resource

tested_modules = ["dulcinea.calendar"]

class Test (TestScenario):

    def check_a(self):
        self.test_stmt('self.calendar = Calendar()')
        self.test_stmt('self.block1 = Reservation()')
        self.test_exc('self.calendar.add(self.block1)', TypeError)
        title = 'test1'
        self.test_stmt('self.block1.set_title(title)')
        self.test_val('self.block1.get_title()', title)
        self.test_stmt('self.block1.set_description(title)')
        self.test_val('self.block1.get_description()', title)
        resource = Resource()
        reserved = [resource]
        self.test_stmt('self.block1.set_reserved(reserved)')
        self.test_val('self.block1.get_reserved()', reserved)
        users = []
        self.test_stmt('self.block1.set_users(users)')
        self.test_val('self.block1.get_users()', users)
        start1 = DateTime(1)
        self.test_stmt('self.block1.set_start_time(start1)')
        self.test_val('self.block1.get_start_time()', start1)
        end1 = start1 + 1
        self.test_stmt('self.block1.set_end_time(end1)')
        self.test_val('self.block1.get_end_time()', end1)

        self.test_stmt('self.calendar.add(self.block1)')
        self.test_val('self.calendar.get(self.block1.get_key())', self.block1)
        self.test_val("self.calendar.get('', None)", None)
        self.test_val('self.calendar.get_mapping()',
                      { self.block1.get_key():  self.block1 })

        self.test_stmt('self.reservable = Reservable()')
        self.test_stmt('self.reservable.set_active(True)')
        self.test_val('self.reservable.is_active()', True)
        title = 'room 1'
        self.test_stmt('self.reservable.set_title(title)')
        self.test_val('self.reservable.get_title()', title)
        self.test_stmt('self.block1.set_reserved([self.reservable])')

        user = User()
        self.test_val('self.reservable.allows_control(user)', None)
        self.test_val('self.reservable.allows_reserve(user)', None)
        class AdminUser(User):
            def is_admin(self):
                return True
        admin_user = AdminUser()
        self.test_val('self.block1.can_modify(None)', False)
        self.test_val('self.block1.can_modify(admin_user)', True)

        self.test_val('self.block1.can_modify(user)', False)
        self.reservable.set_permission('control', user)
        self.test_val('self.block1.can_modify(user)', True)

        user2 = User()
        self.test_stmt('self.block1.set_users([user2])')
        self.test_val('self.block1.can_modify(user2)', True)

        self.test_stmt('Resources()')

        time_increment = TimeDelta(hours=2)
        start_time = start1
        end_time = start_time + 3
        start_time_of_day = TimeDelta(hours=8)
        end_time_of_day = TimeDelta(hours=18)
        resource = Resource()
        reservation = Reservation()
        self.test_stmt('reservation.set_title("")')
        self.test_stmt('reservation.set_description("")')
        self.test_stmt('reservation.set_users([])')
        self.test_stmt('reservation.set_reserved([resource])')
        self.test_stmt('reservation.set_start_time(start_time + start_time_of_day)')
        self.test_stmt(
            'reservation.set_end_time(start_time + start_time_of_day + '
            '2 * time_increment)')
        self.test_stmt('reservation.set_reserved([resource])')
        self.test_stmt('self.calendar.add(reservation)')
        self.test_stmt('self.calendar.get_table_map('
                       'resource, time_increment, start_time, end_time, '
                       'start_time_of_day, end_time_of_day)')


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
