"""Unit tests for time unit conversion."""
from unittest import TestCase,main
from random import choice,randint
from libjest.timeunits import convert,TIME_UNIT_LIST
class HappyPathTestCase(TestCase):
"""More of an example than a rigorous test.
This gently tests the behavior on easy, expected input.
"""
def test_from_months(self):
"""Months to everything else."""
self.assertAlmostEqual(
convert(1, to_time_units='hours'),
152.0)
self.assertAlmostEqual(
convert(1, to_time_units='days'),
20.0)
self.assertAlmostEqual(
convert(1, to_time_units='weeks'),
4.0)
self.assertAlmostEqual(
convert(1, to_time_units='months'),
1.0)
self.assertAlmostEqual(
convert(1, to_time_units='years'),
1./12)
class ConsistencyTestCase(TestCase):
"""Test that the conversions are consistent.
"""
def test_inverses(self):
"""Randomized round-trip tests."""
for _ in xrange(10**4):
time = randint(-(10**4), 10**4) / 100.
unit_a = choice(TIME_UNIT_LIST)
unit_b = choice(TIME_UNIT_LIST)
self.assertAlmostEqual(time,
convert(
convert(time, from_time_units=unit_a,
to_time_units=unit_b),
to_time_units=unit_a, from_time_units=unit_b))
if __name__ == '__main__':
main()
|