Example #1
0
 def test_requesting_less_than_available_retrieves_most_recent(self):
     for category, fses in self.fses10.iteritems():
         f = TimeFilter({category: 5}, self.reftime)
         a, r = f.filter(fses)
         assert len(a) == 5
         assert len(r) == 5
         def moddates(fses): return map(lambda fse: fse.moddate, fses)
         assert min(moddates(a)) > max(moddates(r))
Example #2
0
 def test_requesting_one_retrieves_most_recent(self):
     for category, fses in self.fses10.iteritems():
         f = TimeFilter({category: 1}, self.reftime)
         a, r = f.filter(fses)
         assert len(a) == 1
         def moddates(fses): return map(lambda fse: fse.moddate, fses)
         assert a[0].moddate == max(moddates(fses))
         assert len(r) == 9
         assert a[0] not in r
Example #3
0
 def test_hours_one_accepted_one_rejected(self):
     f = TimeFilter(rules={"hours": 1})
     fse1 = FilterItem(modtime=time.time()-60*60*1.5)
     fse2 = FilterItem(modtime=time.time()-60*60*1.6)
     a, r = f.filter(objs=[fse1, fse2])
     r = list(r)
     # The younger one must be accepted.
     assert a[0] == fse1
     assert len(a) == 1
     assert r[0] == fse2
     assert len(r) == 1
Example #4
0
 def test_minimal_functionality_and_types(self):
     # Create filter with reftime self.reftime
     f = TimeFilter(rules={"hours": 1}, reftime=self.reftime)
     # Create mock that is 1.5 hours old. Must end up in accepted list,
     # since it's 1 hour old and one item should be kept from the 1-hour-
     # old-category
     fse = FilterItem(moddate=self.reftime-timedelta(hours=1.5))
     a, r = f.filter([fse])
     # http://stackoverflow.com/a/1952655/145400
     assert isinstance(a, collections.Iterable)
     assert isinstance(r, collections.Iterable)
     assert a[0] == fse
     assert len(r) == 0
Example #5
0
 def test_minimal_functionality_and_types(self):
     # Create filter with reftime NOW (if not specified otherwise)
     # and simple rules.
     f = TimeFilter(rules={"hours": 1})
     # Create mock that is 1.5 hours old. Must end up in accepted list,
     # since it's 1 hour old and one item should be kept from the 1-hour-
     # old-category
     fse = FilterItem(modtime=time.time()-60*60*1.5)
     a, r = f.filter(objs=[fse])
     # http://stackoverflow.com/a/1952655/145400
     assert isinstance(a, collections.Iterable)
     assert isinstance(r, collections.Iterable)
     assert a[0] == fse
     # Rejected list `r` is expected to be an interator, so convert to
     # list before evaluating length.
     assert len(list(r)) == 0
Example #6
0
    def test_requesting_newer_than_available_retrieves_none(self):
        # excluding the "recent" category which will always accept the newest N
        # items.
        categories = ("hours", "days", "weeks", "months", "years")
        # generate items 6-10 per category, in reverse order to increase
        # the chance of discovering order dependencies in the filter.
        fses10to6 = {cat : sorted(self.fses10[cat],
                                  key=lambda x: x.moddate,
                                  reverse=True)[5:]
                     for cat in categories}

        # now ask for the first 5 items of each category.
        for category, fses in fses10to6.iteritems():
            f = TimeFilter({category: 5}, self.reftime)
            a, r = f.filter(fses)
            assert len(a) == 0
            assert set(r) == set(fses)
Example #7
0
 def test_not_iterable(self):
     f = TimeFilter(rules={"days": 1})
     with raises(TypeError):
         # TypeError: 'NoneType' object is not iterable
         f.filter(None)
Example #8
0
 def test_invalid_object(self):
     f = TimeFilter(rules={"days": 1})
     with raises(AttributeError):
         # AttributeError: 'NoneType' object has no attribute 'modtime'
         f.filter([None])
Example #9
0
 def test_requesting_more_than_available_retrieves_all(self):
     for category, fses in self.fses10.iteritems():
         f = TimeFilter({category: 15}, self.reftime)
         a, r = f.filter(fses)
         assert set(a) == set(fses)
         assert len(r) == 0