import py import vsync DatedPath = vsync.DatedPath parsedate = vsync.parsedate datetime = py.std.datetime.datetime def p(s): t = py.std.time.strptime(s, "%Y-%m-%d") return py.std.datetime.datetime(*t[:6]) def setup_module(mod): mod.root = py.path.local.make_numbered_dir(prefix='vsync-') class TestDateComputationBasics: def test_dateparsing(self): x = root.join('x-2004-10-2') d = vsync.DatedPath(x, p('2004-10-2')) assert d.date == datetime(2004,10,2) assert d.path == x def test_equals(self): d1 = vsync.DatedPath('x-2004-10-2', p('2004-10-2')) d2 = vsync.DatedPath('x-2004-10-2', p('2004-10-2')) assert d1 == d2 def test_datesorting(self): datepaths = [ DatedPath('x-2004-12-2', p('2004-12-2')), DatedPath('x-2004-10-1', p('2004-10-1')), DatedPath('x-2004-11-1', p('2004-11-1')), ] newpaths = datepaths[:] newpaths.sort() assert newpaths[0] == datepaths[1] assert newpaths[1] == datepaths[2] assert newpaths[2] == datepaths[0] def test_dateremoval(self): def p(x): date = vsync.parsedate(x) return vsync.DatedPath('x-' + x, date) testset = [ p('2004-10-01'), p('2004-10-02'), p('2004-10-03'), p('2004-10-04'), p('2004-10-05'), p('2004-10-06'), p('2004-10-07'), p('2004-10-08'), p('2004-10-09-09:00'), p('2004-10-09-17:00'), p('2004-09-10'), p('2004-09-11'), p('2004-09-12'), p('2003-09-12'), p('2003-08-12'), p('2003-03-02'), ] remaining, removable = vsync.sortout( p("2004-10-09-19:00").date, testset, days=3, weeks=3, months=2, years=2) assert p("2004-10-5") in removable remaining.sort() remaining.reverse() # now youngest first print "remaining:" for x in remaining: py.std.pprint.pprint(x.date) print "removable:" for x in removable: py.std.pprint.pprint(x.date) assert remaining[0] == p('2004-10-09-17:00') assert remaining[1] == p('2004-10-08') assert remaining[2] == p('2004-10-07') assert remaining[3] == p('2004-10-02') assert p('2004-10-03') in removable assert remaining[4] == p('2004-10-01') # candidate for third week assert remaining[5] == p('2004-09-10') # candidate for second month assert remaining[6] == p('2003-09-12') # second year def test_dateremoval_years(self): def p(x): date = vsync.parsedate(x) return vsync.DatedPath('x-' + x, date) testset = [ p('2004-10-01'), p('2003-09-01'), p('2002-09-02') ] remaining, removable = vsync.sortout( p("2004-10-01-19:00").date, testset, days=2, weeks=1, months=2, years=2) assert p("2002-09-02") in removable remaining.sort() remaining.reverse() # now youngest first assert remaining[0] == p('2004-10-01') assert remaining[1] == p('2003-09-01') def test_sanity_candidates_stays(self): def p(x): date = vsync.parsedate(x) return vsync.DatedPath('x-' + x, date) testset = [ p('2004-10-01'), p('2004-09-30'), p('2004-09-29'), p('2004-09-02'), ] remaining, removable = vsync.sortout( p("2004-10-01-19:00").date, testset, days=2, weeks=2, months=2, years=2) remaining.sort() remaining.reverse() # now youngest first assert len(remaining) == 4 assert remaining == testset class TestBackupFromConf: def setup_class(cls): d = py.test.ensuretemp('backuptest') class MyTestConf: source = d.mkdir('sourcedir') backupdir = d.mkdir('backupdir') name = 'me' days = 3 weeks = 2 months = 1 years = 1 cls.MyTestConf = MyTestConf def test_make_first_backup(self): vsync.syncbyconf(self.MyTestConf, p('2004-10-01')) backupdir = self.MyTestConf.backupdir assert len(backupdir.listdir()) == 1 def test_make_next_backups(self): backupdir = self.MyTestConf.backupdir vsync.syncbyconf(self.MyTestConf, p('2004-10-02')) assert len(backupdir.listdir()) == 2 vsync.syncbyconf(self.MyTestConf, p('2004-10-03')) assert len(backupdir.listdir()) == 3 def test_fourth_backup(self): backupdir = self.MyTestConf.backupdir vsync.syncbyconf(self.MyTestConf, p('2004-10-04')) assert len(backupdir.listdir()) == 4 def test_10more(self): backupdir = self.MyTestConf.backupdir for i in range(5, 11): date = p("2004-10-%0d" % i) vsync.syncbyconf(self.MyTestConf, date) date = parsedate("2004-10-10-17:00") vsync.cleanupbyconf(self.MyTestConf, date) l = [] for x in backupdir.listdir(): if x.basename.startswith(self.MyTestConf.name): date = vsync.parsedate(x.basename[len(self.MyTestConf.name)+1:]) l.append(DatedPath(x, date)) l.sort() assert l[-1].date == p("2004-10-10") # assert l[-2].date == p("2004-10-09") # assert l[-3].date == p("2004-10-08") # assert l[-4].date == p("2004-10-03") # week 2 assert len(l) == 4