diff options
-rwxr-xr-x | debdate | 85 |
1 files changed, 78 insertions, 7 deletions
@@ -1,7 +1,9 @@ #!/usr/bin/env python3 +import argparse import datetime -import dateutil.relativedelta +from dateutil import relativedelta +import unittest # TODO: these will be read from some other file RELEASES = [ @@ -30,20 +32,89 @@ class DebDate: data['release'] = r[1] if day > epoch: break - delta = dateutil.relativedelta.relativedelta(day, epoch) - data['dody'] = 0 + if day < epoch: + raise OutsideTimeError( + "{day} happened before the beginning of time".format( + day=day, + ) + ) + delta = relativedelta.relativedelta(day, epoch) data['yodr'] = delta.years + 1 if delta.years == 0: - data['dody'] = datetime.timedelta(delta).days + data['dody'] = (day - epoch).days else: data['dody'] = day.timetuple().tm_yday return data - def main(self): + +class OutsideTimeError(ValueError): + """ + Exception raised before the beginning of time + """ + + +class TestDebDate(unittest.TestCase): + def setUp(self): + self.debdate = DebDate() + + def testFirstYearRelease(self): + r = RELEASES[-1] + d = r[0] + relativedelta.relativedelta(days=10) + data = self.debdate.get_release(d) + self.assertEqual(data['dody'], 10) + self.assertEqual(data['yodr'], 1) + self.assertEqual(data['release'], r[1]) + + def testSecondYearRelease(self): + r = RELEASES[-6] + d = r[0] + relativedelta.relativedelta(years=1) + data = self.debdate.get_release(d) + self.assertEqual(data['dody'], 227) + self.assertEqual(data['yodr'], 2) + self.assertEqual(data['release'], r[1]) + + def testPastLatestRelease(self): + r = RELEASES[0] + d = r[0] + relativedelta.relativedelta(years=5) + data = self.debdate.get_release(d) + # Day Of The Year will change depending on when is the latest + # release, so we do not check it + self.assertEqual(data['yodr'], 6) + self.assertEqual(data['release'], r[1]) + + def testBeforeBeginningOfTime(self): + r = RELEASES[-1] + d = r[0] + relativedelta.relativedelta(days=-1) + with self.assertRaises(OutsideTimeError): + self.debdate.get_release(d) + + +class Command: + def setup_parser(self): + self.parser = argparse.ArgumentParser(description='Debian Regnal Date') + self.parser.set_defaults(func=self.print_date) + self.subparsers = self.parser.add_subparsers() + self.test_parser = self.subparsers.add_parser( + 'test', + help='Run unit tests', + ) + self.test_parser.set_defaults(func=self.run_tests) + + def run_tests(self, args): + suite = unittest.TestLoader().loadTestsFromTestCase(TestDebDate) + unittest.TextTestRunner(verbosity=1).run(suite) + + def print_date(self, args): today = datetime.date.today() - data = self.get_release(today) + debdate = DebDate() + data = debdate.get_release(today) print(MSG.format(**data)) + def main(self): + self.setup_parser() + args = self.parser.parse_args() + args.func(args) + if __name__ == '__main__': - DebDate().main() + Command().main() |