def get_computed_data_type(self, table): before_column = table.columns[self._before_column_name] if isinstance(before_column.data_type, Date): return TimeDelta() elif isinstance(before_column.data_type, DateTime): return TimeDelta() elif isinstance(before_column.data_type, TimeDelta): return TimeDelta() elif isinstance(before_column.data_type, Number): return Number()
def get_computed_data_type(self, table): before_column = self._validate(table) if isinstance(before_column.data_type, Date): return TimeDelta() elif isinstance(before_column.data_type, DateTime): return TimeDelta() elif isinstance(before_column.data_type, TimeDelta): return TimeDelta() elif isinstance(before_column.data_type, Number): return Number()
def test_change_timedeltas(self): rows = (('4:15', '8:18'), ('4h 2m', '2h'), ('4 weeks', '27 days')) timedelta_type = TimeDelta() column_names = ['one', 'two'] column_types = [timedelta_type, timedelta_type] table = Table(rows, column_names, column_types) new_table = table.compute([('test', Change('one', 'two'))]) self.assertIsNot(new_table, table) self.assertEqual(len(new_table.rows), 3) self.assertEqual(len(new_table.columns), 3) self.assertSequenceEqual(new_table.rows[0], (datetime.timedelta(minutes=4, seconds=15), datetime.timedelta(minutes=8, seconds=18), datetime.timedelta(minutes=4, seconds=3))) self.assertEqual(new_table.columns['test'][0], datetime.timedelta(minutes=4, seconds=3)) self.assertEqual(new_table.columns['test'][1], datetime.timedelta(hours=-2, minutes=-2)) self.assertEqual(new_table.columns['test'][2], datetime.timedelta(days=-1))
def setUp(self): self.rows = [ [datetime.datetime(1994, 3, 3, 6, 31)], [datetime.datetime(1994, 3, 3, 6, 30, 30)], [datetime.datetime(1994, 3, 3, 6, 30)], ] self.table = Table(self.rows, ['test', 'null'], [DateTime(), DateTime()]) self.time_delta_rows = [ [datetime.timedelta(seconds=10), None], [datetime.timedelta(seconds=20), None], ] self.time_delta_table = Table(self.time_delta_rows, ['test', 'null'], [TimeDelta(), TimeDelta()])
def setUp(self): self.rows = ( (1, 'a', True, '11/4/2015', '11/4/2015 12:22 PM', '4:15'), (2, u'👍', False, '11/5/2015', '11/4/2015 12:45 PM', '6:18'), (None, 'b', None, None, None, None) ) self.column_names = [ 'number', 'text', 'boolean', 'date', 'datetime', 'timedelta' ] self.column_types = [ Number(), Text(), Boolean(), Date(), DateTime(), TimeDelta() ]
def setUp(self): self.type = TimeDelta()
class TestTimeDelta(unittest.TestCase): def setUp(self): self.type = TimeDelta() def test_test(self): self.assertEqual(self.type.test(None), True) self.assertEqual(self.type.test('N/A'), True) self.assertEqual(self.type.test(True), False) self.assertEqual(self.type.test('True'), False) self.assertEqual(self.type.test(1), False) self.assertEqual(self.type.test(Decimal('1')), False) self.assertEqual(self.type.test('2.7'), False) self.assertEqual(self.type.test(2.7), False) self.assertEqual(self.type.test('3/1/1994'), False) self.assertEqual(self.type.test(datetime.date(1994, 3, 1)), False) self.assertEqual(self.type.test('3/1/1994 12:30 PM'), False) self.assertEqual(self.type.test('2015-01-01 02:34'), False) self.assertEqual(self.type.test(datetime.datetime(1994, 3, 1, 12, 30)), False) self.assertEqual(self.type.test('4:10'), True) self.assertEqual( self.type.test(datetime.timedelta(hours=4, minutes=10)), True) self.assertEqual(self.type.test('a'), False) self.assertEqual(self.type.test('A\nB'), False) self.assertEqual(self.type.test(u'👍'), False) self.assertEqual(self.type.test('05_leslie3d_base'), False) self.assertEqual(self.type.test('2016-12-29'), False) self.assertEqual(self.type.test('2016-12-29T11:43:30Z'), False) self.assertEqual(self.type.test('2016-12-29T11:43:30+06:00'), False) self.assertEqual(self.type.test('2016-12-29T11:43:30-06:00'), False) def test_cast_parser(self): values = ('4:10', '1.2m', '172 hours', '5 weeks, 2 days', 'n/a') casted = tuple(self.type.cast(v) for v in values) self.assertSequenceEqual(casted, (datetime.timedelta(minutes=4, seconds=10), datetime.timedelta(minutes=1, seconds=12), datetime.timedelta(hours=172), datetime.timedelta(weeks=5, days=2), None)) def test_cast_error(self): with self.assertRaises(CastError): self.type.cast('quack')