Back to index

obnam  1.1
forget_policy_tests.py
Go to the documentation of this file.
00001 # Copyright (C) 2010  Lars Wirzenius
00002 #
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import datetime
00018 import unittest
00019 
00020 import obnamlib
00021 
00022 
00023 class ForgetPolicyParseTests(unittest.TestCase):
00024 
00025     def setUp(self):
00026         self.fp = obnamlib.ForgetPolicy()
00027 
00028     def test_raises_error_for_empty_string(self):
00029         self.assertRaises(obnamlib.Error, self.fp.parse, '')
00030 
00031     def test_raises_error_for_unknown_period(self):
00032         self.assertRaises(obnamlib.Error, self.fp.parse, '7x')
00033 
00034     def test_raises_error_if_period_is_duplicated(self):
00035         self.assertRaises(obnamlib.Error, self.fp.parse, '1h,2h')
00036 
00037     def test_raises_error_rules_not_separated_by_comma(self):
00038         self.assertRaises(obnamlib.Error, self.fp.parse, '1h 2d')
00039 
00040     def test_parses_single_rule(self):
00041         self.assertEqual(self.fp.parse('7d'),
00042                          { 'hourly': 0,
00043                            'daily': 7,
00044                            'weekly': 0,
00045                            'monthly': 0,
00046                            'yearly': 0 })
00047 
00048     def test_parses_multiple_rules(self):
00049         self.assertEqual(self.fp.parse('1h,2d,3w,4m,255y'),
00050                          { 'hourly': 1,
00051                            'daily': 2,
00052                            'weekly': 3,
00053                            'monthly': 4,
00054                            'yearly': 255 })
00055 
00056 
00057 class ForgetPolicyMatchTests(unittest.TestCase):
00058 
00059     def setUp(self):
00060         self.fp = obnamlib.ForgetPolicy()
00061 
00062     def match2(self, spec, times):
00063         rules = self.fp.parse(spec)
00064         return [dt for i, dt in self.fp.match(rules, list(enumerate(times)))]
00065 
00066     def test_hourly_matches(self):
00067         h0m0 =  datetime.datetime(2000, 1, 1, 0, 0)
00068         h0m59 = datetime.datetime(2000, 1, 1, 0, 59)
00069         h1m0 =  datetime.datetime(2000, 1, 1, 1, 0)
00070         h1m59 = datetime.datetime(2000, 1, 1, 1, 59)
00071         self.assertEqual(self.match2('1h', [h0m0, h0m59, h1m0, h1m59]),
00072                          [h1m59])
00073 
00074     def test_daily_matches(self):
00075         d1h0 =  datetime.datetime(2000, 1, 1, 0, 0)
00076         d1h23 = datetime.datetime(2000, 1, 1, 23, 0)
00077         d2h0 =  datetime.datetime(2000, 1, 2, 0, 0)
00078         d2h23 = datetime.datetime(2000, 1, 2, 23, 0)
00079         self.assertEqual(self.match2('1d', [d1h0, d1h23, d2h0, d2h23]),
00080                          [d2h23])
00081 
00082     # Not testing weekly matching, since I can't figure out to make
00083     # a sensible test case right now.
00084 
00085     def test_monthly_matches(self):
00086         m1d1 =  datetime.datetime(2000, 1, 1, 0, 0)
00087         m1d28 = datetime.datetime(2000, 1, 28, 0, 0)
00088         m2d1 =  datetime.datetime(2000, 2, 1, 0, 0)
00089         m2d28 = datetime.datetime(2000, 2, 28, 0, 0)
00090         self.assertEqual(self.match2('1m', [m1d1, m1d28, m2d1, m2d28]),
00091                          [m2d28])
00092 
00093     def test_yearly_matches(self):
00094         y1m1 =  datetime.datetime(2000, 1, 1, 0, 0)
00095         y1m12 = datetime.datetime(2000, 12, 1, 0, 0)
00096         y2m1 =  datetime.datetime(2001, 1, 1, 0, 0)
00097         y2m12 = datetime.datetime(2001, 12, 1, 0, 0)
00098         self.assertEqual(self.match2('1y', [y1m1, y1m12, y2m1, y2m12]),
00099                          [y2m12])
00100 
00101     def test_hourly_and_daily_match_together(self):
00102         d1h0m0 = datetime.datetime(2000, 1, 1, 0, 0)
00103         d1h0m1 = datetime.datetime(2000, 1, 1, 0, 1)
00104         d2h0m0 = datetime.datetime(2000, 1, 2, 0, 0)
00105         d2h0m1 = datetime.datetime(2000, 1, 2, 0, 1)
00106         d3h0m0 = datetime.datetime(2000, 1, 3, 0, 0)
00107         d3h0m1 = datetime.datetime(2000, 1, 3, 0, 1)
00108         genlist = list(enumerate([d1h0m0, d1h0m1, d2h0m0, d2h0m1,
00109                                   d3h0m0, d3h0m1]))
00110         rules = self.fp.parse('1h,2d')
00111         self.assertEqual([dt for genid, dt in self.fp.match(rules, genlist)],
00112                          [d2h0m1, d3h0m1])
00113