def check_state(starttime, currenttime, invocation_interval_mins): expected = dict() for cat in ("years", "months", "weeks", "days", "hours"): expected[cat] = 0 # Calculate how old starttime is compared to currenttime. delta = _Timedelta(t=starttime, ref=currenttime) # Implement the rules recent6,hours23,days7,weeks4 if delta.hours < 1: expected["recent"] = 1 + int( delta.hours_exact * 60 // invocation_interval_mins) elif delta.hours < 23: expected["recent"] = 6 expected["hours"] = delta.hours elif delta.days < 6: expected["recent"] = 6 expected["hours"] = 23 expected["days"] = delta.days elif delta.weeks < 4: expected["recent"] = 6 expected["hours"] = 23 expected["days"] = 6 expected["weeks"] = delta.weeks # Check whether the files fulfill the expectations. check_files(expected, reftime=currenttime)
def check_state(starttime, currenttime, invocation_interval_mins): expected = dict() for cat in ("years", "months", "weeks", "days", "hours"): expected[cat] = 0 # Calculate how old starttime is compared to currenttime. delta = _Timedelta(t=starttime, ref=currenttime) # Implement the rules recent6,hours23,days7,weeks4 if delta.hours < 1: expected["recent"] = 1 + int( delta.hours_exact*60 // invocation_interval_mins) elif delta.hours < 23: expected["recent"] = 6 expected["hours"] = delta.hours elif delta.days < 6: expected["recent"] = 6 expected["hours"] = 23 expected["days"] = delta.days elif delta.weeks < 4: expected["recent"] = 6 expected["hours"] = 23 expected["days"] = 6 expected["weeks"] = delta.weeks # Check whether the files fulfill the expectations. check_files(expected, reftime=currenttime)
def test_types_math_hour(self): dt=datetime d = _Timedelta(t=dt(1915, 2, 24, 9, 35), ref=dt(1915, 2, 24, 10, 35)) assert d.years == 0 assert isinstance(d.years, int) assert d.months == 0 assert isinstance(d.months, int) assert d.weeks == 0 assert isinstance(d.weeks, int) assert d.days == 0 assert isinstance(d.days, int) assert d.hours == 1 assert isinstance(d.hours, int)
def test_types_math_year(self): dt=datetime d = _Timedelta(t=dt(2015, 3, 1), ref=dt(2016, 2, 29)) assert d.years == 1 assert isinstance(d.years, int) assert d.months == 11 assert isinstance(d.months, int) assert d.weeks == 53 assert isinstance(d.weeks, int) assert d.days == 365 assert isinstance(d.days, int) assert d.hours == 365 * 24 assert isinstance(d.hours, int)
def check_files(expected, reftime): log.debug("What we expect: %s", expected) # Build a mapping, for each category/timecount bucket we # save whether we have found a corresponding file or not. # Eventually, all expected category/timecount buckets should # be populated -- if not, that's an error. notrecent_found_mapping = dict() for cat in ("years", "months", "weeks", "days", "hours"): notrecent_found_mapping[cat] = { n: False for n in range(1, expected[cat] + 1) } # Go through the files, and categorize them. # TODO: this is still not matching the real categorization, # here "older" categories have higher priority than "younger" one. # Therefore, this check cannot deal with category overlaps so far. # However, it is sufficient for tight boundary tests, i.e. these # rules: recentN,hours23,days6,weeks2 fns = glob.glob("%s/*.testfile" % self.rundir) log.debug("Found these files: %s", fns) modtimes = [modtime_for_path(fn) for fn in fns] deltas = [_Timedelta(t=t, ref=reftime) for t in modtimes] for delta, filename in zip(deltas, fns): if delta.hours < 1: # That is a recent item. log.debug("Recent item detected: %s", filename) expected["recent"] -= 1 for cat in ("years", "months", "weeks", "days", "hours"): timecount = getattr(delta, cat) if timecount > 0: log.debug("File fits %s/%s.", cat, timecount) if notrecent_found_mapping[cat][timecount]: log.error("Already found before: %s/%s", cat, timecount) assert False log.debug("Mark %s/%s as found.", cat, timecount) notrecent_found_mapping[cat][timecount] = True break # Validate result. # The "recent" count must have been decremented to 0: assert expected["recent"] == 0 for cat, boolmap in notrecent_found_mapping.items(): for timecount, found in boolmap.items(): if not found == True: log.error("No file found for %s/%s", cat, timecount) assert False
def check_files(expected, reftime): log.debug("What we expect: %s", expected) # Build a mapping, for each category/timecount bucket we # save whether we have found a corresponding file or not. # Eventually, all expected category/timecount buckets should # be populated -- if not, that's an error. notrecent_found_mapping = dict() for cat in ("years", "months", "weeks", "days", "hours"): notrecent_found_mapping[cat] = { n:False for n in range(1,expected[cat]+1)} # Go through the files, and categorize them. # TODO: this is still not matching the real categorization, # here "older" categories have higher priority than "younger" one. # Therefore, this check cannot deal with category overlaps so far. # However, it is sufficient for tight boundary tests, i.e. these # rules: recentN,hours23,days6,weeks2 fns = glob.glob("%s/*.testfile" % self.rundir) log.debug("Found these files: %s", fns) modtimes = [modtime_for_path(fn) for fn in fns] deltas = [_Timedelta(t=t, ref=reftime) for t in modtimes] for delta, filename in zip(deltas, fns): if delta.hours < 1: # That is a recent item. log.debug("Recent item detected: %s", filename) expected["recent"] -= 1 for cat in ("years", "months", "weeks", "days", "hours"): timecount = getattr(delta, cat) if timecount > 0: log.debug("File fits %s/%s.", cat, timecount) if notrecent_found_mapping[cat][timecount]: log.error("Already found before: %s/%s", cat, timecount) assert False log.debug("Mark %s/%s as found.", cat, timecount) notrecent_found_mapping[cat][timecount] = True break # Validate result. # The "recent" count must have been decremented to 0: assert expected["recent"] == 0 for cat, boolmap in notrecent_found_mapping.items(): for timecount, found in boolmap.items(): if not found == True: log.error("No file found for %s/%s", cat, timecount) assert False
def test_types_math_hour(self): hour_seconds = 60 * 60 d = _Timedelta(t=0.0, ref=hour_seconds) assert d.years == 0 assert isinstance(d.years, int) assert d.years_exact == 1.0 / (365 * 24) assert isinstance(d.years_exact, float) assert d.months == 0 assert isinstance(d.months, int) assert d.months_exact == 1.0 / (30 * 24) assert isinstance(d.months_exact, float) assert d.weeks == 0 assert isinstance(d.weeks, int) assert d.weeks_exact == 1.0 / (7 * 24) assert isinstance(d.weeks_exact, float) assert d.days == 0 assert isinstance(d.days, int) assert d.days_exact == 1.0 / 24 assert isinstance(d.days_exact, float) assert d.hours == 1 assert isinstance(d.hours, int) assert d.hours_exact == 1.0 assert isinstance(d.hours_exact, float)
def test_types_math_year(self): year_seconds = 60 * 60 * 24 * 365 d = _Timedelta(t=0.0, ref=year_seconds) assert d.years == 1 assert isinstance(d.years, int) assert d.years_exact == 1.0 assert isinstance(d.years_exact, float) assert d.months == 12 assert isinstance(d.months, int) assert d.months_exact == 365.0 / 30 assert isinstance(d.months_exact, float) assert d.weeks == 52 assert isinstance(d.weeks, int) assert d.weeks_exact == 365.0 / 7 assert isinstance(d.weeks_exact, float) assert d.days == 365 assert isinstance(d.days, int) assert d.days_exact == 365.0 assert isinstance(d.days_exact, float) assert d.hours == 365 * 24 assert isinstance(d.hours, int) assert d.hours_exact == 365 * 24.0 assert isinstance(d.hours_exact, float)
def test_future(self): # Time `t` later than reference. with raises(TimeFilterError): _Timedelta(t=1.0, ref=0)
def test_floatdiff(self): # Difference of time `t` and reference must be float. with raises(AssertionError): _Timedelta(t=0, ref=0)
def test_wrongtypes(self): with raises(TypeError): # unsupported operand type(s) for -: 'str' and 'NoneType' _Timedelta(t=None, ref="a")
def test_future(self): # Time `t` later than reference. with raises(TimeFilterError): _Timedelta(t=datetime.now(), ref=datetime.now() - timedelta(hours=10))
def test_wrongtypes(self): with raises(Exception): _Timedelta(t=None, ref="a") with raises(Exception): _Timedelta(t=0.0, ref=100.0)