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

source_changes at macosforge.org source_changes at macosforge.org
Wed Mar 12 11:24:03 PDT 2014


Revision: 11977
          http://trac.calendarserver.org//changeset/11977
Author:   wsanchez at apple.com
Date:     2013-11-19 16:32:27 -0800 (Tue, 19 Nov 2013)
Log Message:
-----------
Add test_indexRecords_positive, test_indexRecords_negative.

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-19 22:51:43 UTC (rev 11976)
+++ CalendarServer/trunk/twext/who/test/test_index.py	2013-11-20 00:32:27 UTC (rev 11977)
@@ -20,8 +20,10 @@
 
 from twisted.trial import unittest
 
+from twext.who.idirectory import FieldName as BaseFieldName
 from twext.who.index import DirectoryService, DirectoryRecord
 from twext.who.test import test_directory
+from twext.who.test.test_directory import RecordStorage
 
 
 
@@ -31,13 +33,79 @@
 
 
 
+# class StubDirectoryService(DirectoryService):
+#     """
+#     Stub directory service with some built-in records and an implementation
+#     of C{recordsFromNonCompoundExpression}.
+#     """
+
+#     def __init__(self):
+#         DirectoryService.__init__(self, u"Stub")
+
+#         self.records = RecordStorage(self, DirectoryRecord)
+
+
+
 class BaseDirectoryServiceTest(test_directory.BaseDirectoryServiceTest):
     """
     Tests for indexed directory services.
     """
+    def test_indexRecords_positive(self):
+        """
+        L{DirectoryService.indexRecords} ensures all record data is in the
+        index.
+        """
+        service = NoLoadDirectoryService(u"")
+        records = RecordStorage(service, DirectoryRecord)
 
+        service.indexRecords(records)
+        index = service.index
 
+        # Verify that the fields that should be indexed are, in fact, indexed
+        # for each record.
+        for record in records:
+            for fieldName in service.indexedFields:
+                values = record.fields.get(fieldName, None)
 
+                if values is None:
+                    continue
+
+                if not BaseFieldName.isMultiValue(fieldName):
+                    values = (values,)
+
+                for value in values:
+                    self.assertIn(fieldName, index)
+                    self.assertIn(value, index[fieldName])
+
+                    indexedRecords = index[fieldName][value]
+                    self.assertIn(record, indexedRecords)
+
+
+    def test_indexRecords_negative(self):
+        """
+        L{DirectoryService.indexRecords} does not have extra data in the index.
+        """
+        service = NoLoadDirectoryService(u"")
+        records = RecordStorage(service, DirectoryRecord)
+
+        service.indexRecords(records)
+        index = service.index
+
+        # Verify that all data in the index cooresponds to the records passed
+        # in.
+        for fieldName, fieldIndex in index.iteritems():
+            for fieldValue, records in fieldIndex.iteritems():
+                for record in records:
+                    self.assertIn(fieldName, record.fields)
+                    values = record.fields[fieldName]
+
+                    if not BaseFieldName.isMultiValue(fieldName):
+                        values = (values,)
+
+                    self.assertIn(fieldValue, values)
+
+
+
 class DirectoryServiceTest(unittest.TestCase, BaseDirectoryServiceTest):
     """
     Tests for L{DirectoryService}.
@@ -77,15 +145,6 @@
         self.assertRaises(NotImplementedError, service.loadRecords)
 
 
-    def test_indexRecords(self):
-        """
-        L{DirectoryService.indexRecords} populates the index.
-        """
-        raise NotImplementedError()
-
-    test_indexRecords.todo = "Unimplemented"
-
-
     def test_flush(self):
         """
         C{flush} sets the index to C{None}.
@@ -198,8 +257,8 @@
         return True
 
     for fieldName, fieldIndex in index.iteritems():
-        for fieldValue, records in fieldIndex:
-            if records:
+        for fieldValue, records in fieldIndex.iteritems():
+            for record in records:
                 return False
 
     return True
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140312/35c1e324/attachment.html>


More information about the calendarserver-changes mailing list