def test__diff(self): d1 = BusinessDate.from_ymd(2016, 1, 31) d2 = BusinessDate.from_ymd(2017, 11, 1) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y9M1D', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 2, 29) d2 = BusinessDate.from_ymd(2017, 3, 1) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y1D', diff.to_string()) d2 = BusinessDate.from_ymd(2017, 2, 28) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 11, 15) d2 = BusinessDate.from_ymd(2017, 1, 15) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('2M', diff.to_string()) d1 = BusinessDate.from_ymd(2015, 7, 31) d2 = BusinessDate.from_ymd(2017, 2, 20) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y6M20D', diff.to_string())
def test__diff(self): d1 = BusinessDate.from_ymd(2016, 1, 31) d2 = BusinessDate.from_ymd(2017, 11, 1) y, m, d = BusinessDate.diff(d2, d1) diff = BusinessPeriod(years=y, months=m, days=d) self.assertEqual('1Y9M1D', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 2, 29) d2 = BusinessDate.from_ymd(2017, 3, 1) y, m, d = BusinessDate.diff(d2, d1) diff = BusinessPeriod(years=y, months=m, days=d) self.assertEqual('1Y1D', diff.to_string()) d2 = BusinessDate.from_ymd(2017, 2, 28) y, m, d = BusinessDate.diff(d2, d1) diff = BusinessPeriod(years=y, months=m, days=d) self.assertEqual('1Y', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 11, 15) d2 = BusinessDate.from_ymd(2017, 1, 15) y, m, d = BusinessDate.diff(d2, d1) diff = BusinessPeriod(years=y, months=m, days=d) self.assertEqual('2M', diff.to_string()) d1 = BusinessDate.from_ymd(2015, 7, 31) d2 = BusinessDate.from_ymd(2017, 2, 20) y, m, d = BusinessDate.diff(d2, d1) diff = BusinessPeriod(years=y, months=m, days=d) self.assertEqual('1Y6M20D', diff.to_string())
def test__diff(self): d1 = BusinessDate.from_ymd(2016, 1, 31) d2 = BusinessDate.from_ymd(2017, 11, 1) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y9M1D', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 2, 29) d2 = BusinessDate.from_ymd(2017, 3, 1) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y1D', diff.to_string()) d2 = BusinessDate.from_ymd(2017, 2, 28) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y', diff.to_string()) d1 = BusinessDate.from_ymd(2016, 11, 15) d2 = BusinessDate.from_ymd(2017, 1, 15) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('2M', diff.to_string()) d1 = BusinessDate.from_ymd(2015, 7, 31) d2 = BusinessDate.from_ymd(2017, 2, 20) diff = BusinessDate.diff(d1, d2) diff = BusinessPeriod(years=diff[0], months=diff[1], days=diff[2]) self.assertEqual('1Y6M20D', diff.to_string())
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')
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')