def test_duration_floordiv(self): """Test the existing dunder floordir, which will be removed when we move to Python 3""" duration = data.Duration(years=4, months=4, days=4, hours=4, minutes=4, seconds=4) expected = data.Duration(years=2, months=2, days=2, hours=2, minutes=2, seconds=2) duration //= 2 self.assertEqual(duration, expected)
def test_duration_float_args(self): """Test that floats passed to Duration() init are handled correctly.""" for kwarg in ["years", "months", "weeks", "days"]: with self.assertRaises(BadInputError): data.Duration(**{kwarg: 1.5}) for kwarg, expected_secs in [("hours", 5400), ("minutes", 90), ("seconds", 1.5)]: self.assertEqual(data.Duration(**{kwarg: 1.5}).get_seconds(), expected_secs)
def test_duration_in_weeks(self): """Test the Duration class when the week arg is supplied.""" dur = data.Duration(weeks=4) self.assertEqual(dur.get_is_in_weeks(), True) for kwarg, expected_days in [ # 1 unit of each property + 4 weeks ("years", 365 + 28), ("months", 30 + 28), ("days", 1 + 28), ("hours", 28), ("minutes", 28), ("seconds", 28)]: dur = data.Duration(weeks=4, **{kwarg: 1}) self.assertFalse(dur.get_is_in_weeks()) self.assertIsNone(dur.weeks) self.assertEqual(dur.get_days_and_seconds()[0], expected_days)
def test_timepoint_add_duration_without_minute(self): """Test adding a duration to a timepoint""" seconds_added = 5 timepoint = data.TimePoint(year=1900, month_of_year=1, day_of_month=1, hour_of_day=1) duration = data.Duration(seconds=seconds_added) t = timepoint + duration self.assertEqual(seconds_added, t.second_of_minute)
def test_duration_comparison(self): """Test the Duration rich comparison methods and hashing.""" run_comparison_tests(data.Duration, get_duration_comparison_tests()) dur = data.Duration(days=1) for var in [7, 'foo', (1, 2), data.TimePoint(year=2000)]: self.assertFalse(dur == var) with self.assertRaises(TypeError): dur < var
def test_duration_subtract(self): """Test subtracting a duration from a timepoint.""" for test in get_duration_subtract_tests(): start_point = data.TimePoint(**test["start"]) test_duration = data.Duration(**test["duration"]) end_point = data.TimePoint(**test["result"]) test_subtract = (start_point - test_duration).to_calendar_date() self.assertEqual(test_subtract, end_point, "%s - %s" % (start_point, test_duration))
def test_timeduration(self): """Test the Duration class methods.""" for test_props, method, method_args, ctrl_results in ( get_timeduration_tests()): duration = data.Duration(**test_props) duration_method = getattr(duration, method) test_results = duration_method(*method_args) self.assertEqual( test_results, ctrl_results, "%s -> %s(%s)" % (test_props, method, method_args) )
def test_timepoint_comparison(self): """Test the TimePoint rich comparison methods and hashing.""" run_comparison_tests(data.TimePoint, get_timepoint_comparison_tests()) point = data.TimePoint(year=2000) for var in [7, 'foo', (1, 2), data.Duration(days=1)]: self.assertFalse(point == var) with self.assertRaises(TypeError): point < var # Cannot use "<", ">=" etc truncated TimePoints of different modes: day_month_point = data.TimePoint(month_of_year=2, day_of_month=5, truncated=True) ordinal_point = data.TimePoint(day_of_year=36, truncated=True) with self.assertRaises(TypeError): # TODO: should be ValueError? day_month_point < ordinal_point
def test_timepoint_plus_float_time_duration_day_of_month_type(self): """Test (TimePoint + Duration).day_of_month is an int.""" time_point = data.TimePoint(year=2000) + data.Duration(seconds=1.0) self.assertEqual(type(time_point.day_of_month), int)
def test_duration_in_weeks_floordiv(self): """Test the existing dunder floordir, which will be removed when we move to Python 3""" duration = data.Duration(weeks=4) duration //= 2 self.assertEqual(2, duration.weeks)
def test_timeduration_add_week(self): """Test the Duration not in weeks add Duration in weeks.""" self.assertEqual( str(data.Duration(days=7) + data.Duration(weeks=1)), "P14D")
def test_duration_to_days(self): """Test converting Duration in weeks to Duration in days""" dur = data.Duration(weeks=4) self.assertEqual(dur.to_days().days, 28)
def test_duration_to_weeks(self): """Test converting Duration in days to Duration in weeks""" duration_in_days = data.Duration(days=365).to_weeks() duration_in_weeks = data.Duration(weeks=52) # 364 days (!) self.assertEqual(duration_in_days.weeks, duration_in_weeks.weeks)