"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/utest_calendar.py $
$Id: utest_calendar.py 31118 2008-09-16 14:55:40Z dbinger $
"""
from datetime import datetime, timedelta
from dulcinea.calendar import Calendar, Reservation, Reservable, Resources
from dulcinea.calendar import Resource, weekdays
from dulcinea.user import DulcineaUser
from sancho.utest import UTest, raises
from qp.lib.tz import Eastern
from dulcinea.base import DulcineaPersistent

class PersistentReservable(Reservable, DulcineaPersistent):
    pass


class Test (UTest):

    def check_a(self):
        calendar = Calendar()
        raises(TypeError, calendar.add, Reservation())
        block1 = Reservation()
        title = 'test1'
        block1.set_title(title)
        assert block1.get_title() == title
        block1.set_description(title)
        assert block1.get_description() == title
        reserved = Resource()
        block1.set_reserved(reserved)
        assert block1.get_reserved() == reserved
        user = DulcineaUser(None)
        block1.set_reserver(user)
        start1 = datetime(1,1,1, tzinfo=Eastern)
        block1.set_start_time(start1)
        assert block1.get_start_time() == start1
        end1 = start1 + timedelta(days=1)
        block1.set_end_time(end1)
        assert block1.get_end_time() == end1
        calendar.add(block1)
        assert calendar.get(block1.get_key()) == block1
        assert calendar.get(-1, None) == None
        assert dict(calendar.get_mapping()) == { block1.get_key():  block1 }
        for entry in calendar:
            assert entry is block1
        reservable = PersistentReservable()
        reservable.set_active(True)
        assert reservable.is_active() == True
        title = 'room 1'
        reservable.set_title(title)
        assert reservable.get_title() == title
        block1.set_reserved(reservable)
        assert reservable.allows_control(user) == False
        assert reservable.allows_reserve(user) == False
        class AdminUser(DulcineaUser):
            def is_admin(self):
                return True
        admin_user = AdminUser('me')
        assert block1.can_modify(None) == False
        assert block1.can_modify(admin_user) == True
        block1.set_reserver(admin_user)
        assert block1.can_modify(user) == False
        user.get_permissions().grant('control', reservable)
        assert block1.can_modify(user) == True
        user2 = DulcineaUser('u')
        block1.set_reserver(user2)
        assert block1.can_modify(user2) == True
        Resources()
        time_increment = timedelta(hours=2)
        start_time = start1
        end_time = start_time + timedelta(days=3)
        start_time_of_day = timedelta(hours=8)
        end_time_of_day = timedelta(hours=18)
        resource = Resource()
        reservation = Reservation()
        reservation.set_title("")
        reservation.set_description("")
        reservation.set_reserved(resource)
        reservation.set_reserver(user)
        reservation.set_start_time(start_time + start_time_of_day)
        reservation.set_end_time(start_time + start_time_of_day + 
                                 2 * time_increment)
        reservation.set_reserved(resource)
        calendar.add(reservation)
        calendar.get_table_map(
            resource, time_increment, start_time, end_time, 
            start_time_of_day, end_time_of_day)

    def check_weekdays(self):
        failures = False
        for length in range(9):
            for offset in range(9):
                start = datetime(
                    2000, 1, 1, tzinfo=Eastern) + timedelta(days=offset)
                end = start + timedelta(days=length)
                weekdays1 = weekdays(start, end)
                currentday = start
                weekdays2 = 0
                for i in range(length):
                    if 5 <= currentday.weekday() <= 6:
                        pass
                    else:
                        weekdays2 += 1
                    currentday += timedelta(days=1)
                if weekdays1 != weekdays2:
                    failures = True
        assert not failures

if __name__ == "__main__":
    Test()
