<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[11974] CalendarServer/trunk/twext/who/test/test_directory.py</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/11974">11974</a></dd>
<dt>Author</dt> <dd>wsanchez@apple.com</dd>
<dt>Date</dt> <dd>2013-11-19 14:26:33 -0800 (Tue, 19 Nov 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor tests</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunktwextwhotesttest_directorypy">CalendarServer/trunk/twext/who/test/test_directory.py</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunktwextwhotesttest_directorypy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/twext/who/test/test_directory.py (11973 => 11974)</h4>
<pre class="diff"><span>
<span class="info">--- 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)
</span><span class="lines">@@ -40,6 +40,7 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     MixIn that sets up a service appropriate for testing.
</span><span class="cx">     &quot;&quot;&quot;
</span><ins>+
</ins><span class="cx">     realmName = u&quot;xyzzy&quot;
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -54,6 +55,7 @@
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Tests for directory services.
</span><span class="cx">     &quot;&quot;&quot;
</span><ins>+
</ins><span class="cx">     def test_interface(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Service instance conforms to L{IDirectoryService}.
</span><span class="lines">@@ -206,7 +208,7 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class DirectoryServiceTest(unittest.TestCase, BaseDirectoryServiceTest):
</del><ins>+class DirectoryServiceRecordsFromExpressionTest(unittest.TestCase):
</ins><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Tests for L{DirectoryService}.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -343,6 +345,18 @@
</span><span class="cx">         self.assertFailure(results, QueryNotSupportedError)
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+
+class DirectoryServiceConvenienceTest(
+    unittest.TestCase,
+    BaseDirectoryServiceTest
+):
+    &quot;&quot;&quot;
+    Tests for L{DirectoryService} convenience methods.
+    &quot;&quot;&quot;
+    serviceClass = DirectoryService
+    directoryRecordClass = DirectoryRecord
+
+
</ins><span class="cx">     def test_recordWithUID(self):
</span><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         C{recordWithUID} fails with L{QueryNotSupportedError}.
</span><span class="lines">@@ -752,8 +766,52 @@
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-class StubDirectoryService(DirectoryService):
</del><ins>+class RFNCEMixIn(object):
</ins><span class="cx">     &quot;&quot;&quot;
</span><ins>+    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.
+    &quot;&quot;&quot;
+
+    def recordsFromExpression(self, expression):
+        self.seenExpressions = []
+
+        return DirectoryService.recordsFromExpression(self, expression)
+
+
+    def recordsFromNonCompoundExpression(self, expression, records=None):
+        &quot;&quot;&quot;
+        This implementation handles three expressions:
+
+        The expression C{u&quot;None&quot;} will match no records.
+
+        The expressions C{u&quot;twistedmatrix.com&quot;} and C{u&quot;calendarserver.org&quot;}
+        will match records that have an email address ending with the
+        given expression.
+        &quot;&quot;&quot;
+        self.seenExpressions.append(expression)
+
+        if expression == u&quot;None&quot;:
+            return succeed([])
+
+        if expression in (u&quot;twistedmatrix.com&quot;, u&quot;calendarserver.org&quot;):
+            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):
+    &quot;&quot;&quot;
</ins><span class="cx">     Stub directory service with some built-in records and an implementation
</span><span class="cx">     of C{recordsFromNonCompoundExpression}.
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="lines">@@ -761,15 +819,26 @@
</span><span class="cx">     def __init__(self):
</span><span class="cx">         DirectoryService.__init__(self, u&quot;Stub&quot;)
</span><span class="cx"> 
</span><ins>+        self.records = RecordStorage(self, DirectoryRecord)
+
+
+class RecordStorage(object):
+    &quot;&quot;&quot;
+    Container for directory records.
+    &quot;&quot;&quot;
+    def __init__(self, service, recordClass):
+        self.service = service
+        self.recordClass = recordClass
</ins><span class="cx">         self.records = []
</span><del>-        self._addRecords()
</del><span class="cx"> 
</span><ins>+        self.addDefaultRecords()
</ins><span class="cx"> 
</span><del>-    def _addRecords(self):
</del><ins>+
+    def addDefaultRecords(self):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Add a known set of records to this service.
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;wsanchez&quot;, u&quot;wilfredo_sanchez&quot;],
</span><span class="cx">             fullNames=[u&quot;Wilfredo S\xe1nchez Vega&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -778,7 +847,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;glyph&quot;],
</span><span class="cx">             fullNames=[u&quot;Glyph Lefkowitz&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -787,7 +856,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;sagen&quot;],
</span><span class="cx">             fullNames=[u&quot;Morgen Sagen&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -796,7 +865,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;cdaboo&quot;],
</span><span class="cx">             fullNames=[u&quot;Cyrus Daboo&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -804,7 +873,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;dre&quot;],
</span><span class="cx">             fullNames=[u&quot;Andre LaBranche&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -813,7 +882,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;exarkun&quot;],
</span><span class="cx">             fullNames=[u&quot;Jean-Paul Calderone&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -821,7 +890,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;dreid&quot;],
</span><span class="cx">             fullNames=[u&quot;David Reid&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -829,7 +898,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;joe&quot;],
</span><span class="cx">             fullNames=[u&quot;Joe Schmoe&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -837,7 +906,7 @@
</span><span class="cx">             ],
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-        self._addUser(
</del><ins>+        self.addUser(
</ins><span class="cx">             shortNames=[u&quot;alyssa&quot;],
</span><span class="cx">             fullNames=[u&quot;Alyssa P. Hacker&quot;],
</span><span class="cx">             emailAddresses=[
</span><span class="lines">@@ -846,7 +915,7 @@
</span><span class="cx">         )
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def _addUser(self, shortNames, fullNames, emailAddresses=[]):
</del><ins>+    def addUser(self, shortNames, fullNames, emailAddresses=[]):
</ins><span class="cx">         &quot;&quot;&quot;
</span><span class="cx">         Add a user record with the given field information.
</span><span class="cx"> 
</span><span class="lines">@@ -859,42 +928,24 @@
</span><span class="cx">         @param emailAddresses: Record email addresses.
</span><span class="cx">         @type emailAddresses: L{list} of L{unicode}s
</span><span class="cx">         &quot;&quot;&quot;
</span><del>-        self.records.append(DirectoryRecord(self, {
-            self.fieldName.recordType: self.recordType.user,
-            self.fieldName.uid: u&quot;__{0}__&quot;.format(shortNames[0]),
-            self.fieldName.shortNames: shortNames,
-            self.fieldName.fullNames: fullNames,
-            self.fieldName.password: u&quot;&quot;.join(reversed(shortNames[0])),
-            self.fieldName.emailAddresses: emailAddresses,
</del><ins>+        service = self.service
+        fieldName = service.fieldName
+        recordType = service.recordType
+        self.records.append(self.recordClass(self.service, {
+            fieldName.recordType: recordType.user,
+            fieldName.uid: u&quot;__{0}__&quot;.format(shortNames[0]),
+            fieldName.shortNames: shortNames,
+            fieldName.fullNames: fullNames,
+            fieldName.password: u&quot;&quot;.join(reversed(shortNames[0])),
+            fieldName.emailAddresses: emailAddresses,
</ins><span class="cx">         }))
</span><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def recordsFromExpression(self, expression):
-        self.seenExpressions = []
</del><ins>+    def __iter__(self):
+        return iter(self.records)
</ins><span class="cx"> 
</span><del>-        return DirectoryService.recordsFromExpression(self, expression)
</del><span class="cx"> 
</span><span class="cx"> 
</span><del>-    def recordsFromNonCompoundExpression(self, expression, records=None):
-        self.seenExpressions.append(expression)
-
-        if expression == u&quot;None&quot;:
-            return succeed([])
-
-        if expression in (u&quot;twistedmatrix.com&quot;, u&quot;calendarserver.org&quot;):
-            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
-        )
-
-
</del><span class="cx"> class WackyOperand(Names):
</span><span class="cx">     &quot;&quot;&quot;
</span><span class="cx">     Wacky operands.
</span></span></pre>
</div>
</div>

</body>
</html>