class BusinessPeriodUnitTests(unittest.TestCase): def setUp(self): self._1y = BusinessPeriod('1y') self._3m = BusinessPeriod('1m') self._1y6m = BusinessPeriod('1y6m') self._1b = BusinessPeriod('1b') self._2y = BusinessPeriod('2y') self._3y = BusinessPeriod('3y') self._5y = BusinessPeriod('5y') self._2q = BusinessPeriod('2q') self._2w = BusinessPeriod('2w') def test_constructors(self): self.assertEqual(self._1y, BusinessPeriod(years=1)) self.assertEqual(self._1y6m, BusinessPeriod(years=1, months=6)) self.assertEqual(self._1y6m, BusinessPeriod('6m', years=1)) self.assertEqual(-1 * self._1y6m, BusinessPeriod('-6m', years=-1)) self.assertEqual(self._2q, BusinessPeriod(months=6)) def test_properties(self): self.assertEqual(self._1y.years, 1) self.assertEqual(self._1y.months, 0) self.assertEqual(self._1y.days, 0) self.assertEqual(-1 * self._1y.years, -1) self.assertEqual(-1 * self._1y.days, 0) self.assertEqual(self._1b.businessdays, 1) def test_operators(self): self.assertTrue(self._2y < BusinessPeriod('10Y')) self.assertFalse(self._3y < BusinessPeriod('1Y')) self.assertEqual(self._2y.__cmp__(BusinessPeriod('10Y')), -2922.0) self.assertNotEqual(self._2y, self._5y) self.assertEqual(BusinessPeriod('5y'), self._5y) def test_validations(self): self.assertTrue(BusinessPeriod.is_businessperiod('1y')) self.assertTrue(BusinessPeriod.is_businessperiod('-1y')) self.assertFalse(BusinessPeriod.is_businessperiod('1y-6m')) def test_calculations(self): self.assertEqual(self._2y + self._3y, self._5y) self.assertEqual(self._1y + '6m', self._1y6m) self.assertEqual(self._1y, BusinessPeriod('1y')) self.assertRaises(TypeError, lambda: '6m' + self._1y) self.assertEquals(self._1y, self._3y - self._2y) self.assertEquals(self._1y, self._3y - '2y') def test_cast(self): self.assertEqual(BusinessPeriod.from_string('1y'), BusinessPeriod(years=1)) self.assertEqual(self._1y.to_businessdate(BusinessDate(20150101)), BusinessDate(20160101)) self.assertEqual(self._1y6m.to_businessdate(BusinessDate(20150101)), BusinessDate(20160701)) self.assertEqual(self._1y.to_businessdate(BusinessDate(20160229)), BusinessDate(20170228)) self.assertEqual(self._1y.to_date(BusinessDate(20160229)), date(2017, 02, 28)) self.assertEqual(self._1y.to_businessperiod(BusinessDate(20160229)), self._1y) self.assertEqual(self._1y.to_string(), '1Y') self.assertEqual(self._2q.to_string(), '6M') self.assertEqual(self._2w.to_string(), '14D')
def test_to_date(self): p = BusinessPeriod(years=1, months=2, days=3) self.assertEqual(p.to_businessdate(BusinessDate('20160101')), BusinessDate('20170304'))
def test_to_date(self): p = BusinessPeriod(years=1, months=2, days=3) self.assertEqual(p.to_businessdate(BusinessDate('20160101')), BusinessDate('20170304'))
class BusinessPeriodUnitTests(unittest.TestCase): def setUp(self): self._1y = BusinessPeriod('1y') self._3m = BusinessPeriod('3m') self._1y6m = BusinessPeriod('1y6m') self._1b = BusinessPeriod('1b') self._2y = BusinessPeriod('2y') self._3y = BusinessPeriod('3y') self._5y = BusinessPeriod('5y') self._2q = BusinessPeriod('2q') self._2w = BusinessPeriod('2w') def test_constructors(self): self.assertEqual(self._1y, BusinessPeriod(years=1)) self.assertEqual(self._1y6m, BusinessPeriod(years=1, months=6)) self.assertEqual(self._1y6m, BusinessPeriod('6m', years=1)) self.assertEqual(self._1y6m, BusinessPeriod('18m')) self.assertEqual(-1 * self._1y6m, BusinessPeriod('-6m', years=-1)) self.assertEqual(self._2q, BusinessPeriod(months=6)) def test_properties(self): self.assertEqual(self._1y.years, 1) self.assertEqual(self._1y.months, 0) self.assertEqual(self._1y.days, 0) self.assertEqual(-1 * self._1y.years, -1) self.assertEqual(-1 * self._1y.days, 0) self.assertEqual(self._1b.businessdays, 1) def test_operators(self): self.assertNotEqual(BusinessPeriod("3M"), BusinessPeriod("1M")) self.assertNotEqual(BusinessPeriod("31D"), BusinessPeriod("1M")) self.assertEqual(BusinessPeriod("3M"), BusinessPeriod("1M") + "2m") self.assertEqual(BusinessPeriod("1Y"), BusinessPeriod("12M")) self.assertEqual(BusinessPeriod("1Y"), BusinessPeriod("12M0D")) self.assertTrue(self._2y < BusinessPeriod('10Y')) self.assertTrue(self._2y < BusinessPeriod('1m') * 12 * 2 + '1d') self.assertFalse(self._3y < BusinessPeriod('1Y')) self.assertEqual(self._2y.__cmp__(BusinessPeriod('10Y')), -2976.0) self.assertNotEqual(self._2y, self._5y) self.assertEqual(BusinessPeriod('5y'), self._5y) def test_validations(self): self.assertTrue(BusinessPeriod.is_businessperiod('1y')) self.assertTrue(BusinessPeriod.is_businessperiod('-1y')) self.assertFalse(BusinessPeriod.is_businessperiod('1y-6m')) def test_calculations(self): self.assertEqual(self._2y + self._3y, self._5y) self.assertEqual(self._1y + '6m', self._1y6m) self.assertEqual(self._1y, BusinessPeriod('1y')) self.assertRaises(TypeError, lambda: '6m' + self._1y) self.assertEqual(self._1y, self._3y - self._2y) self.assertEqual(self._1y, self._3y - '2y') def test_cast(self): self.assertEqual(BusinessPeriod.from_string('1y'), BusinessPeriod(years=1)) self.assertEqual(self._1y.to_businessdate(BusinessDate(20150101)), BusinessDate(20160101)) self.assertEqual(self._1y6m.to_businessdate(BusinessDate(20150101)), BusinessDate(20160701)) self.assertEqual(self._1y.to_businessdate(BusinessDate(20160229)), BusinessDate(20170228)) self.assertEqual(self._1y.to_date(BusinessDate(20160229)), date(2017, 2, 28)) self.assertEqual(self._1y.to_businessperiod(BusinessDate(20160229)), self._1y) self.assertEqual(self._1y.to_string(), '1Y') self.assertEqual(self._2q.to_string(), '6M') self.assertEqual(self._2w.to_string(), '14D')