blob: a732d7f2f1c34d0fbe552afcb0e37ee04a04755f [file] [log] [blame]
# Copyright 2013 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 time utility functions."""
import calendar
import datetime
import unittest
from melange.utils import time
from tests import timeline_utils
class IsBeforeTest(unittest.TestCase):
"""Unit tests for isBefore function."""
def testIsBefore(self):
"""Tests that True is returned if it is before the examined date."""
self.assertTrue(time.isBefore(timeline_utils.future()))
def testIsNotBefore(self):
"""Tests that False is returned if it is after the examined date."""
self.assertFalse(time.isBefore(timeline_utils.past()))
def testForNone(self):
"""Tests that False is returned for None."""
self.assertFalse(time.isBefore(None))
class IsAfterTest(unittest.TestCase):
"""Unit tests for isAfter function."""
def testIsAfter(self):
"""Tests that True is returned if it is after the examined date."""
self.assertTrue(time.isAfter(timeline_utils.past()))
def testIsNotAfter(self):
"""Tests that False is returned if it is before the examined date."""
self.assertFalse(time.isAfter(timeline_utils.future()))
def testForNone(self):
"""Tests that False is returned for None."""
self.assertFalse(time.isAfter(None))
class IsBetweenTest(unittest.TestCase):
"""Unit tests for isBetween function."""
def testIsAfter(self):
"""Tests that False is returned if it is after the examined period."""
self.assertFalse(time.isBetween(
timeline_utils.past(delta=10), timeline_utils.past(delta=5)))
def testIsBefore(self):
"""Tests that False is returned if it is before the examined period."""
self.assertFalse(time.isBetween(
timeline_utils.future(delta=5), timeline_utils.future(delta=10)))
def testIsBetween(self):
"""Tests that True is returned if it is within the examined perioed."""
self.assertTrue(time.isBetween(
timeline_utils.past(delta=10), timeline_utils.future(delta=10)))
class PeriodStateTest(unittest.TestCase):
"""Unit tests for Period class."""
def testForUnboundPeriod(self):
"""Tests state for unbound period."""
period = time.Period()
self.assertEqual(period.state, time.IN_PERIOD_STATE)
def testForLeftUnboundPeriod(self):
"""Tests state for periods with no start date."""
# set the end of period to the past so the period is already over
period = time.Period(end=timeline_utils.past())
self.assertEqual(period.state, time.POST_PERIOD_STATE)
# set the end of period to the future so we are currently in
period = time.Period(end=timeline_utils.future())
self.assertEqual(period.state, time.IN_PERIOD_STATE)
def testForRightUnboundPeriod(self):
"""Tests state for periods with no end date."""
# set the start of period to the past so that we are currently in
period = time.Period(start=timeline_utils.past())
self.assertEqual(period.state, time.IN_PERIOD_STATE)
# set the start of period to the future so that is has yet to start
period = time.Period(start=timeline_utils.future())
self.assertEqual(period.state, time.PRE_PERIOD_STATE)
def testForBoundPeriod(self):
"""Tests state for periods with both start and end dates."""
# set the start and end dates to the past so it is after the period
period = time.Period(start=timeline_utils.past(),
end=timeline_utils.past())
self.assertEqual(period.state, time.POST_PERIOD_STATE)
# set the start date to the past and the end date to the future
# so it is in the period
period = time.Period(
start=timeline_utils.past(), end=timeline_utils.future())
self.assertEqual(period.state, time.IN_PERIOD_STATE)
# set the start and end dates to the past so it is before the period
period = time.Period(
start=timeline_utils.future(), end=timeline_utils.future())
self.assertEqual(period.state, time.PRE_PERIOD_STATE)
# NOTE: because of how unit tests for getAge function are implemented,
# not all dates are acceptable here
REF_YEAR = 2014
REF_MONTH = 6
REF_DAY = 10
TEST_YEAR = 2000
class GetAgeTest(unittest.TestCase):
"""Unit tests for getAge function."""
def testDates(self):
"""Tests that the correct age is returned for various dates."""
as_of = datetime.date(REF_YEAR, REF_MONTH, REF_DAY)
# check for months before the "as of" month
for month in range(1, REF_MONTH):
date = datetime.date(TEST_YEAR, month, REF_DAY)
self.assertEqual(REF_YEAR - TEST_YEAR, time.getAge(date, as_of=as_of))
# check during the "as of" month but before or on the "as of" day
for day in range(1, REF_DAY + 1):
date = datetime.date(TEST_YEAR, REF_MONTH, day)
self.assertEqual(REF_YEAR - TEST_YEAR, time.getAge(date, as_of=as_of))
# check during the "as of" month but after the "as of" day
max_day = calendar.monthrange(TEST_YEAR, REF_MONTH)[1]
for day in range(REF_DAY + 1, max_day + 1):
date = datetime.date(TEST_YEAR, REF_MONTH, day)
self.assertEqual(REF_YEAR - TEST_YEAR - 1, time.getAge(date, as_of=as_of))
# check for months after the "as of" month
for month in range(REF_MONTH + 1, 13):
date = datetime.date(TEST_YEAR, month, REF_DAY)
self.assertEqual(REF_YEAR - TEST_YEAR - 1, time.getAge(date, as_of=as_of))