[CalendarServer-changes] [11986] CalendarServer/trunk/twext/who/test/test_index.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:16:17 PDT 2014


Revision: 11986
          http://trac.calendarserver.org//changeset/11986
Author:   wsanchez at apple.com
Date:     2013-11-20 11:05:25 -0800 (Wed, 20 Nov 2013)
Log Message:
-----------
Add test_recordsFromNonCompoundExpression_*

Modified Paths:
--------------
    CalendarServer/trunk/twext/who/test/test_index.py

Modified: CalendarServer/trunk/twext/who/test/test_index.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_index.py	2013-11-20 08:56:17 UTC (rev 11985)
+++ CalendarServer/trunk/twext/who/test/test_index.py	2013-11-20 19:05:25 UTC (rev 11986)
@@ -19,9 +19,10 @@
 """
 
 from twisted.trial import unittest
-from twisted.internet.defer import inlineCallbacks
+from twisted.internet.defer import inlineCallbacks, returnValue
 
 from twext.who.idirectory import FieldName as BaseFieldName
+from twext.who.idirectory import QueryNotSupportedError
 from twext.who.expression import MatchExpression, MatchType
 from twext.who.index import DirectoryService, DirectoryRecord
 from twext.who.test import test_directory
@@ -30,26 +31,37 @@
 
 
 def noLoadDirectoryService(superClass):
+    """
+    Creates an indexed directory service that has a no-op implementation of
+    L{DirectoryService.loadRecords}.
+
+    @param superClass: The superclass of the new service.
+    @type superClass: subclass of L{DirectoryService}
+
+    @return: A new directory service class.
+    @rtype: subclass of C{superClass}
+    """
+    assert issubclass(superClass, DirectoryService)
+
     class NoLoadDirectoryService(superClass):
         def loadRecords(self):
             pass
 
-    return NoLoadDirectoryService
+        def indexedRecordsFromMatchExpression(self, *args, **kwargs):
+            self._calledIndexed = True
+            return superClass.indexedRecordsFromMatchExpression(
+                self, *args, **kwargs
+            )
 
+        def unIndexedRecordsFromMatchExpression(self, *args, **kwargs):
+            self._calledUnindexed = True
+            return superClass.unIndexedRecordsFromMatchExpression(
+                self, *args, **kwargs
+            )
 
-# class StubDirectoryService(DirectoryService):
-#     """
-#     Stub directory service with some built-in records and an implementation
-#     of C{recordsFromNonCompoundExpression}.
-#     """
+    return NoLoadDirectoryService
 
-#     def __init__(self):
-#         DirectoryService.__init__(self, u"Stub")
 
-#         self.records = RecordStorage(self, DirectoryRecord)
-
-
-
 class BaseDirectoryServiceTest(test_directory.BaseDirectoryServiceTest):
     """
     Tests for indexed directory services.
@@ -128,9 +140,7 @@
 
     @inlineCallbacks
     def _test_indexedRecordsFromMatchExpression(
-        self,
-        inOut, matchType,
-        fieldName=BaseFieldName.shortNames,
+        self, inOut, matchType, fieldName=BaseFieldName.shortNames,
     ):
         service = self.noLoadServicePopulated()
 
@@ -222,9 +232,7 @@
 
     @inlineCallbacks
     def _test_unIndexedRecordsFromMatchExpression(
-        self,
-        inOut, matchType,
-        fieldName=BaseFieldName.fullNames,
+        self, inOut, matchType, fieldName=BaseFieldName.fullNames,
     ):
         service = self.noLoadServicePopulated()
 
@@ -313,16 +321,52 @@
         self.assertFailure(result, NotImplementedError)
 
 
-    def test_recordsFromNonCompoundExpression(self):
+    @inlineCallbacks
+    def _test_recordsFromNonCompoundExpression(self, expression):
+        service = self.noLoadServicePopulated()
+        yield service.recordsFromNonCompoundExpression(expression)
+        returnValue(service)
+
+
+    @inlineCallbacks
+    def test_recordsFromNonCompoundExpression_match_indexed(self):
         """
-        L{DirectoryService.recordsFromNonCompoundExpression} ...
+        L{DirectoryService.recordsFromNonCompoundExpression} with a
+        L{MatchExpression} for an indexed field calls
+        L{DirectoryRecord.indexedRecordsFromMatchExpression}.
         """
-        raise NotImplementedError()
+        service = yield self._test_recordsFromNonCompoundExpression(
+            MatchExpression(BaseFieldName.shortNames, u"...")
+        )
+        self.assertTrue(getattr(service, "_calledIndexed", False))
+        self.assertFalse(getattr(service, "_calledUnindexed", False))
 
-    test_recordsFromNonCompoundExpression.todo = "Unimplemented"
 
+    @inlineCallbacks
+    def test_recordsFromNonCompoundExpression_match_unindexed(self):
+        """
+        L{DirectoryService.recordsFromNonCompoundExpression} with a
+        L{MatchExpression} for an unindexed field calls
+        L{DirectoryRecord.unIndexedRecordsFromMatchExpression}.
+        """
+        service = yield self._test_recordsFromNonCompoundExpression(
+            MatchExpression(BaseFieldName.password, u"...")
+        )
+        self.assertFalse(getattr(service, "_calledIndexed", False))
+        self.assertTrue(getattr(service, "_calledUnindexed", False))
 
 
+    def test_recordsFromNonCompoundExpression_unknown(self):
+        """
+        L{DirectoryService.recordsFromNonCompoundExpression} with a
+        an unknown expression calls superclass, which will result in a
+        L{QueryNotSupportedError}.
+        """
+        result = self._test_recordsFromNonCompoundExpression(object())
+        self.assertFailure(result, QueryNotSupportedError)
+
+
+
 class DirectoryServiceTest(unittest.TestCase, BaseDirectoryServiceTest):
     """
     Tests for L{DirectoryService}.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/19f6bcf8/attachment.html>


More information about the calendarserver-changes mailing list