[CalendarServer-changes] [11974] CalendarServer/trunk/twext/who/test/test_directory.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:21:04 PDT 2014


Revision: 11974
          http://trac.calendarserver.org//changeset/11974
Author:   wsanchez at apple.com
Date:     2013-11-19 14:26:33 -0800 (Tue, 19 Nov 2013)
Log Message:
-----------
Refactor tests

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

Modified: CalendarServer/trunk/twext/who/test/test_directory.py
===================================================================
--- CalendarServer/trunk/twext/who/test/test_directory.py	2013-11-19 20:50:59 UTC (rev 11973)
+++ CalendarServer/trunk/twext/who/test/test_directory.py	2013-11-19 22:26:33 UTC (rev 11974)
@@ -40,6 +40,7 @@
     """
     MixIn that sets up a service appropriate for testing.
     """
+
     realmName = u"xyzzy"
 
 
@@ -54,6 +55,7 @@
     """
     Tests for directory services.
     """
+
     def test_interface(self):
         """
         Service instance conforms to L{IDirectoryService}.
@@ -206,7 +208,7 @@
 
 
 
-class DirectoryServiceTest(unittest.TestCase, BaseDirectoryServiceTest):
+class DirectoryServiceRecordsFromExpressionTest(unittest.TestCase):
     """
     Tests for L{DirectoryService}.
     """
@@ -343,6 +345,18 @@
         self.assertFailure(results, QueryNotSupportedError)
 
 
+
+class DirectoryServiceConvenienceTest(
+    unittest.TestCase,
+    BaseDirectoryServiceTest
+):
+    """
+    Tests for L{DirectoryService} convenience methods.
+    """
+    serviceClass = DirectoryService
+    directoryRecordClass = DirectoryRecord
+
+
     def test_recordWithUID(self):
         """
         C{recordWithUID} fails with L{QueryNotSupportedError}.
@@ -752,8 +766,52 @@
 
 
 
-class StubDirectoryService(DirectoryService):
+class RFNCEMixIn(object):
     """
+    Mixin class that implements C{recordsFromNonCompoundExpression}.
+
+    This class also sets a C{seenExpressions} attribute to C{[]} when
+    C{recordsFromExpression} is called and appends the expressions seen when
+    C{recordsFromNonCompoundExpression} is subsequently called to that list.
+    """
+
+    def recordsFromExpression(self, expression):
+        self.seenExpressions = []
+
+        return DirectoryService.recordsFromExpression(self, expression)
+
+
+    def recordsFromNonCompoundExpression(self, expression, records=None):
+        """
+        This implementation handles three expressions:
+
+        The expression C{u"None"} will match no records.
+
+        The expressions C{u"twistedmatrix.com"} and C{u"calendarserver.org"}
+        will match records that have an email address ending with the
+        given expression.
+        """
+        self.seenExpressions.append(expression)
+
+        if expression == u"None":
+            return succeed([])
+
+        if expression in (u"twistedmatrix.com", u"calendarserver.org"):
+            result = []
+            for record in self.records:
+                for email in record.emailAddresses:
+                    if email.endswith(expression):
+                        result.append(record)
+                        break
+            return succeed(result)
+
+        return DirectoryService.recordsFromNonCompoundExpression(
+            self, expression, records=records
+        )
+
+
+class StubDirectoryService(RFNCEMixIn, DirectoryService):
+    """
     Stub directory service with some built-in records and an implementation
     of C{recordsFromNonCompoundExpression}.
     """
@@ -761,15 +819,26 @@
     def __init__(self):
         DirectoryService.__init__(self, u"Stub")
 
+        self.records = RecordStorage(self, DirectoryRecord)
+
+
+class RecordStorage(object):
+    """
+    Container for directory records.
+    """
+    def __init__(self, service, recordClass):
+        self.service = service
+        self.recordClass = recordClass
         self.records = []
-        self._addRecords()
 
+        self.addDefaultRecords()
 
-    def _addRecords(self):
+
+    def addDefaultRecords(self):
         """
         Add a known set of records to this service.
         """
-        self._addUser(
+        self.addUser(
             shortNames=[u"wsanchez", u"wilfredo_sanchez"],
             fullNames=[u"Wilfredo S\xe1nchez Vega"],
             emailAddresses=[
@@ -778,7 +847,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"glyph"],
             fullNames=[u"Glyph Lefkowitz"],
             emailAddresses=[
@@ -787,7 +856,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"sagen"],
             fullNames=[u"Morgen Sagen"],
             emailAddresses=[
@@ -796,7 +865,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"cdaboo"],
             fullNames=[u"Cyrus Daboo"],
             emailAddresses=[
@@ -804,7 +873,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"dre"],
             fullNames=[u"Andre LaBranche"],
             emailAddresses=[
@@ -813,7 +882,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"exarkun"],
             fullNames=[u"Jean-Paul Calderone"],
             emailAddresses=[
@@ -821,7 +890,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"dreid"],
             fullNames=[u"David Reid"],
             emailAddresses=[
@@ -829,7 +898,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"joe"],
             fullNames=[u"Joe Schmoe"],
             emailAddresses=[
@@ -837,7 +906,7 @@
             ],
         )
 
-        self._addUser(
+        self.addUser(
             shortNames=[u"alyssa"],
             fullNames=[u"Alyssa P. Hacker"],
             emailAddresses=[
@@ -846,7 +915,7 @@
         )
 
 
-    def _addUser(self, shortNames, fullNames, emailAddresses=[]):
+    def addUser(self, shortNames, fullNames, emailAddresses=[]):
         """
         Add a user record with the given field information.
 
@@ -859,42 +928,24 @@
         @param emailAddresses: Record email addresses.
         @type emailAddresses: L{list} of L{unicode}s
         """
-        self.records.append(DirectoryRecord(self, {
-            self.fieldName.recordType: self.recordType.user,
-            self.fieldName.uid: u"__{0}__".format(shortNames[0]),
-            self.fieldName.shortNames: shortNames,
-            self.fieldName.fullNames: fullNames,
-            self.fieldName.password: u"".join(reversed(shortNames[0])),
-            self.fieldName.emailAddresses: emailAddresses,
+        service = self.service
+        fieldName = service.fieldName
+        recordType = service.recordType
+        self.records.append(self.recordClass(self.service, {
+            fieldName.recordType: recordType.user,
+            fieldName.uid: u"__{0}__".format(shortNames[0]),
+            fieldName.shortNames: shortNames,
+            fieldName.fullNames: fullNames,
+            fieldName.password: u"".join(reversed(shortNames[0])),
+            fieldName.emailAddresses: emailAddresses,
         }))
 
 
-    def recordsFromExpression(self, expression):
-        self.seenExpressions = []
+    def __iter__(self):
+        return iter(self.records)
 
-        return DirectoryService.recordsFromExpression(self, expression)
 
 
-    def recordsFromNonCompoundExpression(self, expression, records=None):
-        self.seenExpressions.append(expression)
-
-        if expression == u"None":
-            return succeed([])
-
-        if expression in (u"twistedmatrix.com", u"calendarserver.org"):
-            result = []
-            for record in self.records:
-                for email in record.emailAddresses:
-                    if email.endswith(expression):
-                        result.append(record)
-                        break
-            return succeed(result)
-
-        return DirectoryService.recordsFromNonCompoundExpression(
-            self, expression, records=records
-        )
-
-
 class WackyOperand(Names):
     """
     Wacky operands.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/643f385d/attachment.html>


More information about the calendarserver-changes mailing list