Fix : fix hot module under windows test. (at leat I hope...)
[shinken.git] / test / test_timeperiods.py
blobd67defdd84fe162ab5691ab002656eeb75f79353
1 #!/usr/bin/env python2.6
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 # This file is used to test timeperiods
27 #It's ugly I know....
28 from shinken_test import *
29 from shinken.objects.timeperiod import Timeperiod
31 class TestTimeperiods(ShinkenTest):
33 def test_simple_timeperiod(self):
34 self.print_header()
35 t = Timeperiod()
36 now = time.time()
37 #Get the 12 of july 2010 at 15:00, monday
38 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
39 print july_the_12
41 #First a false test, no results
42 t = Timeperiod()
43 t.timeperiod_name = ''
44 t.resolve_daterange(t.dateranges, '1999-01-28 00:00-24:00')
45 t_next = t.get_next_valid_time_from_t(now)
46 self.assert_(t_next is None)
48 #Then a simple same day
49 t = Timeperiod()
50 t.timeperiod_name = ''
51 t.resolve_daterange(t.dateranges, 'tuesday 16:30-24:00')
52 t_next = t.get_next_valid_time_from_t(july_the_12)
53 t_next = time.asctime(time.localtime(t_next))
54 print t_next
55 self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
60 def test_simple_timeperiod_with_exclude(self):
61 self.print_header()
62 t = Timeperiod()
63 now = time.time()
64 #Get the 12 of july 2010 at 15:00, monday
65 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
66 print july_the_12
68 #First a false test, no results
69 t = Timeperiod()
70 t.timeperiod_name = ''
71 t.resolve_daterange(t.dateranges, '1999-01-28 00:00-24:00')
72 t_next = t.get_next_valid_time_from_t(now)
73 self.assert_(t_next is None)
75 #Then a simple same day
76 t = Timeperiod()
77 t.timeperiod_name = ''
78 t.resolve_daterange(t.dateranges, 'tuesday 16:30-24:00')
79 t_next = t.get_next_valid_time_from_t(july_the_12)
80 t_next = time.asctime(time.localtime(t_next))
81 print t_next
82 self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
84 #Now we add this timeperiod an exception
85 t2 = Timeperiod()
86 t2.timeperiod_name = ''
87 t2.resolve_daterange(t2.dateranges, 'tuesday 08:30-21:00')
88 t.exclude = [t2]
89 #So the next will be after 16:30 and not before 21:00. So
90 #It will be 21:00:01 (first second after invalid is valid)
92 #we clean the cache of previous calc of t ;)
93 t.cache = {}
94 t_next = t.get_next_valid_time_from_t(july_the_12)
95 t_next = time.asctime(time.localtime(t_next))
96 print "T nxt with exclude:", t_next
97 self.assert_(t_next == "Tue Jul 13 21:00:01 2010")
100 def test_dayweek_timeperiod_with_exclude(self):
101 self.print_header()
102 now = time.time()
103 #Get the 12 of july 2010 at 15:00, monday
104 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
106 #Then a simple same day
107 t = Timeperiod()
108 t.timeperiod_name = 'T1'
109 t.resolve_daterange(t.dateranges, 'tuesday 2 16:30-24:00')
110 t_next = t.get_next_valid_time_from_t(july_the_12)
111 t_next = time.asctime(time.localtime(t_next))
112 print "T next", t_next
113 self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
115 #Now we add this timeperiod an exception
116 t2 = Timeperiod()
117 t2.timeperiod_name = 'T2'
118 t2.resolve_daterange(t2.dateranges, 'tuesday 00:00-23:58')
119 t.exclude = [t2]
120 #We are a bad boy : first time period want a tuesday
121 #but exclude do not want it until 23:58. So next is 58 + 1second :)
122 t.cache = {}
123 t_next = t.get_next_valid_time_from_t(july_the_12)
124 t_exclude = t2.get_next_valid_time_from_t(july_the_12)
125 t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12)
127 print "T next raw", t_next
128 t_next = time.asctime(time.localtime(t_next))
129 print "TOTO T next", t_next
131 self.assert_(t_next == 'Tue Jul 13 23:58:01 2010')
134 def test_mondayweek_timeperiod_with_exclude(self):
135 self.print_header()
136 now = time.time()
137 #Get the 12 of july 2010 at 15:00, monday
138 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
140 #Then a simple same day
141 t = Timeperiod()
142 t.timeperiod_name = 'T1'
143 t.resolve_daterange(t.dateranges, 'tuesday 2 16:30-24:00')
144 t_next = t.get_next_valid_time_from_t(july_the_12)
145 t_next = time.asctime(time.localtime(t_next))
146 self.assert_(t_next == "Tue Jul 13 16:30:00 2010")
148 #Now we add this timeperiod an exception
149 #And a good one : from april (so before so agust (after), and full time.
150 #But the 17 is a tuesday, but the 3 of august, so the next 2 tuesday is
151 #..... the Tue Sep 14 :) Yes, we should wait quite a lot :)
152 t2 = Timeperiod()
153 t2.timeperiod_name = 'T2'
154 t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00')
155 #print t2.__dict__
156 t.exclude = [t2]
157 #We are a bad boy : first time period want a tuesday
158 #but exclude do not want it until 23:58. So next is 59 :)
159 t.cache = {}
160 t_next = t.get_next_valid_time_from_t(july_the_12)
161 #print "Check from", time.asctime(time.localtime(july_the_12))
162 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
163 t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12)
164 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
165 print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv))
167 print "T next raw JEAN", t_next
168 print "T next?", time.asctime(time.localtime(t_next))
169 t_next = time.asctime(time.localtime(t_next))
171 self.assert_(t_next == 'Tue Sep 14 16:30:00 2010')
177 def test_mondayweek_timeperiod_with_exclude_bis(self):
178 self.print_header()
179 now = time.time()
180 #Get the 12 of july 2010 at 15:00, monday
181 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
183 #Then a funny daterange
184 print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00'
185 t = Timeperiod()
186 t.timeperiod_name = 'T1'
187 t.resolve_daterange(t.dateranges, 'tuesday -1 - monday 1 16:30-24:00')
188 t_next = t.get_next_valid_time_from_t(july_the_12)
189 t_next = time.asctime(time.localtime(t_next))
190 print "Next without exclude", t_next
191 self.assert_(t_next == "Tue Jul 27 16:30:00 2010")
193 #Now we add this timeperiod an exception
194 #And a good one : from april (so before so agust (after), and full time.
195 #But the 27 is nw not possible? So what next? Add a month!
196 #last tuesday of august, the 31 :)
197 t2 = Timeperiod()
198 t2.timeperiod_name = 'T2'
199 t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00')
200 #print t2.__dict__
201 t.exclude = [t2]
202 #We are a bad boy : first time period want a tuesday
203 #but exclude do not want it until 23:58. So next is 59 :)
204 t.cache = {}
205 t_next = t.get_next_valid_time_from_t(july_the_12)
206 #print "Check from", time.asctime(time.localtime(july_the_12))
207 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
208 t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12)
209 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
210 print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv))
212 print "T next raw JEAN2", t_next
213 print "T next?", time.asctime(time.localtime(t_next))
214 t_next = time.asctime(time.localtime(t_next))
216 self.assert_(t_next == 'Tue Aug 31 16:30:00 2010')
220 def test_funky_mondayweek_timeperiod_with_exclude_and_multiple_daterange(self):
221 self.print_header()
222 now = time.time()
223 #Get the 12 of july 2010 at 15:00, monday
224 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
226 #Then a funny daterange
227 print "Testing daterange", 'tuesday -1 - monday 1 16:30-24:00'
228 t = Timeperiod()
229 t.timeperiod_name = 'T1'
230 t.resolve_daterange(t.dateranges, 'tuesday -1 - monday 1 16:30-24:00')
231 t_next = t.get_next_valid_time_from_t(july_the_12)
232 t_next = time.asctime(time.localtime(t_next))
233 print "Next without exclude", t_next
234 self.assert_(t_next == "Tue Jul 27 16:30:00 2010")
236 #Now we add this timeperiod an exception
237 #And a good one : from april (so before so agust (after), and full time.
238 #But the 27 is nw not possible? So what next? Add a month!
239 #But maybe it's not enoutgth? :)
240 #The withoutthe 2nd exclude, it's the Tues Aug 31, btu it's inside
241 #saturday -1 - monday 1 because saturday -1 is the 28 august, so no.
242 #in september saturday -1 is the 25, and tuesday -1 is 28, so still no
243 #A month again! So now tuesday -1 is 26 and saturday -1 is 30. So ok
244 #for this one! that was quite long isn't it? And funky ! :)
245 t2 = Timeperiod()
246 t2.timeperiod_name = 'T2'
247 t2.resolve_daterange(t2.dateranges, 'april 1 - august 16 00:00-24:00')
248 #Oups, I add a inner daterange ;)
249 t2.resolve_daterange(t2.dateranges, 'saturday -1 - monday 1 16:00-24:00')
250 t.exclude = [t2]
251 #We are a bad boy : first time period want a tuesday
252 #but exclude do not want it until 23:58. So next is 59 :)
253 t.cache = {}
254 t_next = t.get_next_valid_time_from_t(july_the_12)
255 #print "Check from", time.asctime(time.localtime(july_the_12))
256 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
257 t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12)
258 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
259 print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv))
261 print "T next raw", t_next
262 print "T next?", time.asctime(time.localtime(t_next))
263 t_next = time.asctime(time.localtime(t_next))
265 self.assert_(t_next == 'Tue Oct 26 16:30:00 2010')
266 print "Finish this Funky test :)"
270 def test_monweekday_timeperiod_with_exclude(self):
271 self.print_header()
272 now = time.time()
273 #Get the 12 of july 2010 at 15:00, monday
274 july_the_12 = time.mktime(time.strptime("12 Jul 2010 15:00:00", "%d %b %Y %H:%M:%S"))
276 #Then a funny daterange
277 print "Testing daterange", 'tuesday -1 july - monday 1 august 16:30-24:00'
278 t = Timeperiod()
279 t.timeperiod_name = 'T1'
280 t.resolve_daterange(t.dateranges, 'tuesday -1 july - monday 1 september 16:30-24:00')
281 t_next = t.get_next_valid_time_from_t(july_the_12)
282 t_next = time.asctime(time.localtime(t_next))
283 print "Next without exclude", t_next
284 self.assert_(t_next == "Tue Jul 27 16:30:00 2010")
286 #Now we add this timeperiod an exception
287 #and from april (before) to august monday 3 (monday 16 august),
288 #so Jul 17 is no more possible. So just after it, Tue 17
289 t2 = Timeperiod()
290 t2.timeperiod_name = 'T2'
291 t2.resolve_daterange(t2.dateranges, 'thursday 1 april - monday 3 august 00:00-24:00')
292 print t2.dateranges[0].__dict__
293 t.exclude = [t2]
294 #We are a bad boy : first time period want a tuesday
295 #but exclude do not want it until 23:58. So next is 59 :)
296 t.cache = {}
297 t_next = t.get_next_valid_time_from_t(july_the_12)
298 #print "Check from", time.asctime(time.localtime(july_the_12))
299 #t_exclude = t2.get_next_valid_time_from_t(july_the_12)
300 t_exclude_inv = t2.get_next_invalid_time_from_t(july_the_12)
301 #print "T2 next valid", time.asctime(time.localtime(t_exclude))
302 print "Next invalid T2", time.asctime(time.localtime(t_exclude_inv))
304 print "T next raw", t_next
305 print "T next?", time.asctime(time.localtime(t_next))
306 t_next = time.asctime(time.localtime(t_next))
308 self.assert_(t_next == 'Tue Aug 17 16:30:00 2010')
312 if __name__ == '__main__':
313 unittest.main()