# Calendaring is a simple CMF/Plone calendaring implementation.
# Copyright (C) 2004 Enfold Systems
#
# 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
#
# Some portions of this module are Copyright Shuttleworth Foundation.
# The original copyright statement is reproduced below.
#
# SchoolTool - common information systems platform for school administration
# Copyright (c) 2003 Shuttleworth Foundation
#
# 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
#
"""
$Id: test_calevent.py,v 1.2 2004/09/16 13:46:09 dreamcatcher Exp $
"""

import os, sys

if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

import unittest
from doctest import DocTestSuite
from Testing import ZopeTestCase

from datetime import date, timedelta, datetime
from Products.Calendaring.tests.utils import EqualsSortedMixin
from Products.Calendaring.tests.helpers import sorted
from Products.Calendaring.parser import Calendar
from Products.Calendaring.parser import CalendarEvent
from Products.Calendaring.parser import ICalReader, ICalParseError

class TestCalendar(unittest.TestCase, EqualsSortedMixin):

    def test_iter(self):
        cal = Calendar()
        self.assertEqual(list(cal), [])

        ev1 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(minutes=10),
                            "English")

        cal.addEvent(ev1)
        self.assertEqual(list(cal), [ev1])

    def makeCal(self, events):
        cal = Calendar()
        for event in events:
            cal.addEvent(event)
        return cal

    def test_byDate(self):

        ev1 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(minutes=10),
                            "English")
        ev2 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "Latin")
        ev3 = CalendarEvent(datetime(2003, 11, 26, 10, 0),
                            timedelta(minutes=10),
                            "German")

        cal = self.makeCal([ev1, ev2, ev3])
        self.assertEqual(list(cal.byDate(date(2003, 11, 26))), [ev3])

        # event end date within the period
        ev4 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(1),
                            "Solar eclipse")
        cal = self.makeCal([ev1, ev2, ev3, ev4])
        self.assertEqualSorted(list(cal.byDate(date(2003, 11, 26))),
                               [ev3, ev4])

        # calendar daterange is within the event period
        ev4 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(2),
                            "Solar eclipse")
        cal = self.makeCal([ev1, ev2, ev3, ev4])
        self.assertEqualSorted(list(cal.byDate(date(2003, 11, 26))),
                               [ev3, ev4])

        # only the event start date falls within the period
        ev4 = CalendarEvent(datetime(2003, 11, 26, 10, 0),
                            timedelta(2),
                            "Solar eclipse")
        cal = self.makeCal([ev1, ev2, ev3, ev4])
        self.assertEqualSorted(list(cal.byDate(date(2003, 11, 26))),
                               [ev3, ev4])

        # the event is after the period
        ev4 = CalendarEvent(datetime(2003, 11, 27, 10, 0),
                            timedelta(2),
                            "Solar eclipse")
        cal = self.makeCal([ev1, ev2, ev3, ev4])
        self.assertEqualSorted(list(cal.byDate(date(2003, 11, 26))),
                               [ev3])

    def test_clear(self):
        ev1 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(minutes=10),
                            "English")
        ev2 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "Latin")
        cal = self.makeCal([ev1, ev2])
        cal.clear()
        self.assertEquals(list(cal), [])

    def test_removeEvent(self):
        ev1 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(minutes=10),
                            "English")
        ev2 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "Latin")
        cal = self.makeCal([ev1, ev2])
        cal.removeEvent(CalendarEvent(datetime(2003, 11, 25, 10, 0),
                                      timedelta(minutes=10),
                                      "English"))
        self.assertEquals(list(cal), [ev2])

    def test_update(self):
        ev1 = CalendarEvent(datetime(2003, 11, 25, 10, 0),
                            timedelta(minutes=10),
                            "English")
        ev2 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "Latin")
        ev3 = CalendarEvent(datetime(2003, 11, 26, 10, 0),
                            timedelta(minutes=10),
                            "German")
        cal = self.makeCal([ev1, ev2])
        cal.update(self.makeCal([ev2, ev3]))
        self.assertEquals(sorted(list(cal)), sorted([ev1, ev2, ev3]))



class TestCalendarEvent(unittest.TestCase):

    def test(self):
        owner = object()
        context = object()

        ce = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                           timedelta(minutes=10),
                           "reality check")

        ce1 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "reality check")
        ce2 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "realty check")
        ce3 = CalendarEvent(datetime(2003, 11, 25, 12, 1),
                            timedelta(minutes=10),
                            "reality check")
        ce4 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=11),
                            "reality check")
        ce5 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "reality check", owner=owner)
        ce6 = CalendarEvent(datetime(2003, 11, 25, 12, 0),
                            timedelta(minutes=10),
                            "reality check", owner=owner,
                            context=context)
        self.assertEquals(ce, ce1)
        self.assertNotEquals(ce, ce2)
        self.assertNotEquals(ce, ce3)
        self.assertNotEquals(ce, ce4)
        self.assertNotEquals(ce, ce5)
        self.assertNotEquals(ce5, ce6)
        # self.assertRaises(AttributeError, setattr, ce, 'dtstart', 'not-ro')
        # self.assertRaises(AttributeError, setattr, ce, 'duration', 'not-ro')
        # self.assertRaises(AttributeError, setattr, ce, 'title', 'not-ro')

def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestCalendar))
    suite.addTest(unittest.makeSuite(TestCalendarEvent))
    return suite

if __name__ == '__main__':
    framework(descriptions=1, verbosity=1)
