[CalendarServer-changes] [11367] CalendarServer/trunk/twext/python

source_changes at macosforge.org source_changes at macosforge.org
Fri Jun 14 15:04:21 PDT 2013


Revision: 11367
          http://trac.calendarserver.org//changeset/11367
Author:   wsanchez at apple.com
Date:     2013-06-14 15:04:21 -0700 (Fri, 14 Jun 2013)
Log Message:
-----------
Add FilteringLogObserverTests

Modified Paths:
--------------
    CalendarServer/trunk/twext/python/log.py
    CalendarServer/trunk/twext/python/test/test_log.py

Modified: CalendarServer/trunk/twext/python/log.py
===================================================================
--- CalendarServer/trunk/twext/python/log.py	2013-06-14 19:32:07 UTC (rev 11366)
+++ CalendarServer/trunk/twext/python/log.py	2013-06-14 22:04:21 UTC (rev 11367)
@@ -638,7 +638,7 @@
                 return False
             if result == PredicateResult.maybe:
                 continue
-            raise AssertionError("Unknown predicate result: {0}".format(result))
+            raise AssertionError("Unknown predicate result: {0!r}".format(result))
         return True
 
 

Modified: CalendarServer/trunk/twext/python/test/test_log.py
===================================================================
--- CalendarServer/trunk/twext/python/test/test_log.py	2013-06-14 19:32:07 UTC (rev 11366)
+++ CalendarServer/trunk/twext/python/test/test_log.py	2013-06-14 22:04:21 UTC (rev 11367)
@@ -27,6 +27,7 @@
     formatEvent, formatWithCall,
     Logger, LegacyLogger,
     ILogObserver, LogPublisher,
+    FilteringLogObserver, PredicateResult,
 )
 from twistedcaldav.test.util import TestCase
 
@@ -466,6 +467,79 @@
 
 
 
+class FilteringLogObserverTests(SetUpTearDown, TestCase):
+    """
+    Tests for L{FilteringLogObserver}.
+    """
+
+    def test_interface(self):
+        """
+        L{FilteringLogObserver} is an L{ILogObserver}.
+        """
+        observer = FilteringLogObserver(lambda e: PredicateResult.maybe, ())
+        try:
+            verifyObject(ILogObserver, observer)
+        except BrokenMethodImplementation as e:
+            self.fail(e)
+
+
+    def filterWith(self, *filters):
+        events = [
+            dict(count=0),
+            dict(count=1),
+            dict(count=2),
+            dict(count=3),
+        ]
+
+        class Filters(object):
+            @staticmethod
+            def twoMinus(event):
+                if event["count"] <= 2:
+                    return PredicateResult.yes
+                return PredicateResult.maybe
+
+            @staticmethod
+            def twoPlus(event):
+                if event["count"] >= 2:
+                    return PredicateResult.yes
+                return PredicateResult.maybe
+
+            @staticmethod
+            def notTwo(event):
+                if event["count"] == 2:
+                    return PredicateResult.no
+                return PredicateResult.maybe
+
+            @staticmethod
+            def no(event):
+                return PredicateResult.no
+
+        predicates = (getattr(Filters, f) for f in filters)
+        eventsSeen = []
+        trackingObserver = lambda e: eventsSeen.append(e)
+        filteringObserver = FilteringLogObserver(trackingObserver, predicates)
+        for e in events: filteringObserver(e)
+
+        return [e["count"] for e in eventsSeen]
+
+
+    def test_shouldLogEvent_noFilters(self):
+        self.assertEquals(self.filterWith(), [0, 1, 2, 3])
+
+    def test_shouldLogEvent_noFilter(self):
+        self.assertEquals(self.filterWith("notTwo"), [0, 1, 3])
+
+    def test_shouldLogEvent_yesFilter(self):
+        self.assertEquals(self.filterWith("twoPlus"), [0, 1, 2, 3])
+
+    def test_shouldLogEvent_yesNoFilter(self):
+        self.assertEquals(self.filterWith("twoPlus", "no"), [2, 3])
+
+    def test_shouldLogEvent_yesYesNoFilter(self):
+        self.assertEquals(self.filterWith("twoPlus", "twoMinus", "no"), [0, 1, 2, 3])
+
+
+
 class LegacyLoggerTests(SetUpTearDown, TestCase):
     """
     Tests for L{LegacyLogger}.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20130614/bb86b0e3/attachment.html>


More information about the calendarserver-changes mailing list