def test_add(self): """Check if addition with timedelta is working as expected.""" # Without timezone self.assertEqual( RdiffTime('2014-11-08T21:04:30Z'), RdiffTime('2014-11-05T21:04:30Z') + datetime.timedelta(days=3)) # With timezone self.assertEqual( RdiffTime('2014-11-08T21:04:30-04:00'), RdiffTime('2014-11-05T21:04:30-04:00') + datetime.timedelta(days=3))
def test_extract_date(self): self.assertEqual( RdiffTime(1414967021), self.repo._extract_date( b'my_filename.txt.2014-11-02T17:23:41-05:00.diff.gz')) # Check if date with quoted characther are proerply parsed. # On NTFS, colon (:) are not supported. self.assertEqual( RdiffTime(1483443123), self.repo._extract_date( b'my_filename.txt.2017-01-03T06;05832;05803-05;05800.diff.gz'))
def test_init(self): """ Check various constructor. """ t0 = RdiffTime() self.assertAlmostEqual(int(time.time()), t0.epoch(), delta=5000) t1 = RdiffTime(1415221470) self.assertEqual(1415221470, t1.epoch()) t2 = RdiffTime('2014-11-05T21:04:30Z') self.assertEqual(1415221470, t2.epoch()) t3 = RdiffTime('2014-11-05T16:04:30-05:00') self.assertEqual(1415221470, t3.epoch())
def test_init(self): increment = IncrementEntry( self.root_path, b'my_filename.txt.2014-11-02T17:23:41-05:00.diff.gz') self.assertEqual(RdiffTime(1414967021), increment.date) self.assertEqual(b'my_filename.txt', increment.filename) self.assertIsNotNone(increment.repo)
def setUp(self): self.repo = MockRdiffRepo() backup_dates = [ 1414871387, 1414871426, 1414871448, 1414871475, 1414871489, 1414873822, 1414873850, 1414879639, 1414887165, 1414887491, 1414889478, 1414937803, 1414939853, 1414967021, 1415047607, 1415059497, 1415221262, 1415221470, 1415221495, 1415221507 ] self.repo._backup_dates = [RdiffTime(x) for x in backup_dates] self.root_path = self.repo.root_path
def test_change_dates_with_exists(self): """Check if dates are properly sorted.""" increments = [ IncrementEntry( self.root_path, b'my_filename.txt.2014-11-02T17:23:41-05:00.diff.gz'), IncrementEntry( self.root_path, b'my_filename.txt.2014-11-02T09:16:43-05:00.missing'), IncrementEntry( self.root_path, b'my_filename.txt.2014-11-03T19:04:57-05:00.diff.gz') ] entry = DirEntry(self.root_path, b'my_filename.txt', True, increments) self.assertEqual([ RdiffTime('2014-11-02T17:23:41-05:00'), RdiffTime('2014-11-03T19:04:57-05:00') ], entry.change_dates)
def test_change_dates(self): """Check if dates are properly sorted.""" increments = [ IncrementEntry( self.root_path, b'my_filename.txt.2014-11-02T17:23:41-05:00.diff.gz'), IncrementEntry( self.root_path, b'my_filename.txt.2014-11-02T09:16:43-05:00.missing'), IncrementEntry( self.root_path, b'my_filename.txt.2014-11-03T19:04:57-05:00.diff.gz') ] entry = DirEntry(self.root_path, b'my_filename.txt', False, increments) self.assertEqual([ RdiffTime(1414939853), RdiffTime(1414967021), RdiffTime(1415059497) ], entry.change_dates)
def test_sub(self): """Check if addition with timedelta is working as expected.""" # Without timezone self.assertEqual( RdiffTime('2014-11-02T21:04:30Z'), RdiffTime('2014-11-05T21:04:30Z') - datetime.timedelta(days=3)) # With timezone self.assertEqual( RdiffTime('2014-11-02T21:04:30-04:00'), RdiffTime('2014-11-05T21:04:30-04:00') - datetime.timedelta(days=3)) # With datetime self.assertTrue( (RdiffTime('2014-11-02T21:04:30Z') - RdiffTime()).days < 0) self.assertTrue( (RdiffTime() - RdiffTime('2014-11-02T21:04:30Z')).days > 0)
def test_url_for_restore(self): repo = self.app.store.get_user('admin').get_repo(self.REPO) self.assertEqual('/restore/admin/testcases?date=1414967021', url_for('restore', repo, date=RdiffTime(1414967021))) self.assertEqual( '/restore/admin/testcases?date=1414967021', url_for('restore', repo, b'', date=RdiffTime(1414967021))) self.assertEqual( '/restore/admin/testcases?date=1414967021&kind=tar.gz', url_for('restore', repo, b'', date=RdiffTime(1414967021), kind='tar.gz')) self.assertEqual( '/restore/admin/testcases/Revisions?date=1414967021', url_for('restore', repo, b'Revisions', date=RdiffTime(1414967021))) self.assertEqual( '/restore/admin/testcases/R%C3%A9pertoire%20%28%40vec%29%20%7Bc%C3%A0ra%C3%A7t%23%C3%A8r%C3%AB%7D%20%24%C3%A9p%C3%AAcial?date=1414967021', url_for( 'restore', repo, b'R\xc3\xa9pertoire (@vec) {c\xc3\xa0ra\xc3\xa7t#\xc3\xa8r\xc3\xab} $\xc3\xa9p\xc3\xaacial', date=RdiffTime(1414967021)))
def default(self, path=b"", date=None, kind=None, usetar=None): validate_isinstance(path, bytes) validate_isinstance(date, str) validate(kind is None or kind in ARCHIVERS) validate(usetar is None or isinstance(usetar, str)) logger.debug("restoring [%r][%s]", path, date) # Check user access to repo / path. path_obj = self.app.currentuser.get_repo_path(path)[1] # Get the restore date try: RdiffTime(int(date)) except: logger.warning("invalid date %s", date) raise cherrypy.HTTPError(400, _("Invalid date.")) # Get if backup in progress # status = repo_obj.status # if status[0] != 'ok': # raise cherrypy.HTTPError(500, _(status[1] + ' ' + _("""Restores are disabled."""))) # Determine the kind. kind = kind or 'zip' if usetar is not None: kind = 'tar.gz' # Restore file(s) filename, fileobj = path_obj.restore(int(date), kind=kind) # Define content-disposition. cherrypy.response.headers[ "Content-Disposition"] = _content_disposition(filename) # Set content-type based on filename extension content_type = _content_type(filename) cherrypy.response.headers['Content-Type'] = content_type # Stream the data. # Make use of _serve_fileobj() because the fsstat() function on a pipe # return a size of 0 for Content-Length. This behavior brake all the flow. return _serve_fileobj(fileobj, content_type=content_type, content_length=None)
def test_str(self): """Check if __str__ is working.""" self.assertEqual('2014-11-05T21:04:30Z', str(RdiffTime(1415221470))) self.assertEqual('2014-11-05T21:04:30+01:00', str(RdiffTime(1415221470, 3600)))
def test_int(self): """Check if int(RdiffTime) return expected value.""" self.assertEqual(1415221470, int(RdiffTime(1415221470))) self.assertEqual(1415217870, int(RdiffTime(1415221470, 3600)))
def test_compare(self): """Check behaviour of comparison operator operator.""" self.assertTrue( RdiffTime('2014-11-07T21:04:30-04:00') < RdiffTime( '2014-11-08T21:04:30Z')) self.assertTrue( RdiffTime('2014-11-08T21:04:30Z') < RdiffTime( '2014-11-08T21:50:30Z')) self.assertFalse( RdiffTime('2014-11-08T22:04:30Z') < RdiffTime( '2014-11-08T21:50:30Z')) self.assertFalse( RdiffTime('2014-11-07T21:04:30-04:00') > RdiffTime( '2014-11-08T21:04:30Z')) self.assertFalse( RdiffTime('2014-11-08T21:04:30Z') > RdiffTime( '2014-11-08T21:50:30Z')) self.assertTrue( RdiffTime('2014-11-08T22:04:30Z') > RdiffTime( '2014-11-08T21:50:30Z'))
def test_url_for_status(self): repo = self.app.userdb.get_user('admin').get_repo(self.REPO) self.assertEqual('/status?date=1414967021', url_for('status', date=RdiffTime(1414967021))) self.assertEqual('/status/admin/testcases?date=1414967021', url_for('status', repo, date=RdiffTime(1414967021)))