# Copyright 2011 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Tests for soc.logic.helper.timeline.
"""


import unittest

from datetime import datetime
from datetime import timedelta

from soc.logic.helper import timeline
from soc.models import timeline as timeline_model

from soc.modules.seeder.logic.seeder import logic as seeder_logic


class TimelineTest(unittest.TestCase):
  """Tests for timeline helper functions.
  """

  def setUp(self):
    self.timeline = seeder_logic.seed(timeline_model.Timeline)

  def testIsBeforePeriod(self):
    """Tests if a correct bool is returned if the current DateTime is before
    a given period_start.
    """
    #program is yet to start.
    self.timeline.program_start = datetime.utcnow() + timedelta(10)
    self.assertTrue(timeline.isBeforePeriod(self.timeline, 'program'))

    #program has already started.
    self.timeline.program_start = datetime.utcnow() - timedelta(10)
    self.assertFalse(timeline.isBeforePeriod(self.timeline, 'program'))

    #student signup period is yet to start.
    self.timeline.student_signup_start = datetime.utcnow() + timedelta(10)
    self.assertTrue(timeline.isBeforePeriod(self.timeline, 'student_signup'))

    #student sign up period has already started.
    self.timeline.student_signup_start = datetime.utcnow() - timedelta(10)
    self.assertFalse(timeline.isBeforePeriod(self.timeline, 'student_signup'))

    #event not in the timeline.
    self.assertFalse(timeline.isBeforePeriod(self.timeline, 'other_event'))

  def testIsBeforeEvent(self):
    """Tests if a correct bool is returned if current DateTime
    is before a given event.
    """
    #program has not started.
    self.timeline.program_start = datetime.utcnow() + timedelta(20)
    self.assertTrue(timeline.isBeforeEvent(self.timeline, 'program_start'))

    #program has already started.
    self.timeline.program_start = datetime.utcnow() - timedelta(20)
    self.assertFalse(timeline.isBeforeEvent(self.timeline, 'program_start'))

    #program has not ended.
    self.timeline.program_end = datetime.utcnow() + timedelta(20)
    self.assertTrue(timeline.isBeforeEvent(self.timeline, 'program_end'))

    #program has ended.
    self.timeline.program_end = datetime.utcnow() - timedelta(20)
    self.assertFalse(timeline.isBeforeEvent(self.timeline, 'program_end'))

    #the deadline to announce accepted organizations has not passed.
    self.timeline.accepted_organization_announced_deadline = (datetime.utcnow()
                                                              + timedelta(20))
    self.assertTrue(timeline.isBeforeEvent(