/usr/lib/python2.7/dist-packages/twext/who/test/test_index.py is in python-twext 0.1.b2.dev15059-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 | ##
# Copyright (c) 2013-2015 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
Indexed directory service base implementation tests.
"""
from twisted.trial import unittest
from twisted.internet.defer import inlineCallbacks, returnValue
from ..idirectory import (
FieldName as BaseFieldName, RecordType as BaseRecordType,
QueryNotSupportedError,
)
from ..expression import MatchExpression, MatchType
from ..index import DirectoryService, DirectoryRecord
from ..util import ConstantsContainer
from . import test_directory
from .test_directory import RecordStorage
class TestDirectoryService(DirectoryService):
recordType = ConstantsContainer((BaseRecordType,))
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
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
)
return NoLoadDirectoryService
class BaseDirectoryServiceTest(test_directory.BaseDirectoryServiceTest):
"""
Tests for indexed directory services.
"""
def noLoadServicePopulated(self):
service = self.service(
subClass=noLoadDirectoryService(self.serviceClass)
)
records = RecordStorage(service, DirectoryRecord)
service.indexRecords(records)
service.records = records
return service
def test_indexRecords_positive(self):
"""
L{DirectoryService.indexRecords} ensures all record data is in the
index.
"""
service = self.noLoadServicePopulated()
index = service.index
# Verify that the fields that should be indexed are, in fact, indexed
# for each record.
for record in service.records:
for fieldName in service.indexedFields:
values = record.fields.get(fieldName, None)
if values is None:
continue
if not service.fieldName.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 = self.noLoadServicePopulated()
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 service.fieldName.isMultiValue(fieldName):
values = (values,)
self.assertIn(fieldValue, values)
def test_flush(self):
"""
C{flush} empties the index.
"""
service = self.noLoadServicePopulated()
self.assertFalse(emptyIndex(service.index)) # Test the test
service.flush()
self.assertTrue(emptyIndex(service.index))
@inlineCallbacks
def _test_indexedRecordsFromMatchExpression(
self, inOut, matchType, fieldName=BaseFieldName.shortNames,
recordTypes=None
):
service = self.noLoadServicePopulated()
for subString, uids in (inOut):
records = yield service.indexedRecordsFromMatchExpression(
MatchExpression(
fieldName, subString,
matchType
),
recordTypes=recordTypes,
)
self.assertEquals(
set((record.uid for record in records)),
set(uids)
)
def test_indexedRecordsFromMatchExpression_startsWith(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with a startsWith
expression.
"""
return self._test_indexedRecordsFromMatchExpression(
(
(u"w", (u"__wsanchez__",)), # Duplicates
(u"dr", (u"__dre__", u"__dreid__")), # Multiple
(u"sage", (u"__sagen__",)), # Single
),
MatchType.startsWith
)
def test_indexedRecordsFromMatchExpression_contains(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with a contains
expression.
"""
return self._test_indexedRecordsFromMatchExpression(
(
(u"sanch", (u"__wsanchez__",)), # Duplicates
(u"dr", (u"__dre__", u"__dreid__")), # Multiple
(u"agen", (u"__sagen__",)), # Single
),
MatchType.contains
)
def test_indexedRecordsFromMatchExpression_equals(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with an equals
expression.
"""
return self._test_indexedRecordsFromMatchExpression(
(
(u"wsanchez", (u"__wsanchez__",)), # MultiValue
(u"dre", (u"__dre__",)), # Single value
),
MatchType.equals
)
def test_indexedRecordsFromMatchExpression_notIndexed(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with an
unindexed field name.
"""
result = self._test_indexedRecordsFromMatchExpression(
(
(u"zehcnasw", (u"__wsanchez__",)),
),
MatchType.equals,
fieldName=BaseFieldName.password
)
return self.assertFailure(result, TypeError)
def test_indexedRecordsFromMatchExpression_notMatchExpression(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with a
non-match expression.
"""
result = self._test_indexedRecordsFromMatchExpression(
(
(u"zehcnasw", (u"__wsanchez__",)),
),
"Not a match type we know about"
)
return self.assertFailure(result, NotImplementedError)
def test_indexedRecordsFromMatchExpression_recordTypes(self):
"""
L{DirectoryService.indexedRecordsFromMatchExpression} with recordTypes
"""
return self._test_indexedRecordsFromMatchExpression(
(
(u"c", (u"__calendar-dev__",)), # only groups
(u"d", (u"__developers__",)),
(u"j", ()),
),
MatchType.startsWith,
recordTypes=(BaseRecordType.group,)
)
@inlineCallbacks
def _test_unIndexedRecordsFromMatchExpression(
self, inOut, matchType, fieldName=BaseFieldName.fullNames,
recordTypes=None
):
service = self.noLoadServicePopulated()
for subString, uids in (inOut):
records = yield service.unIndexedRecordsFromMatchExpression(
MatchExpression(
fieldName, subString,
matchType
),
recordTypes=recordTypes
)
self.assertEquals(
set((record.uid for record in records)),
set(uids)
)
def test_unIndexedRecordsFromMatchExpression_startsWith(self):
"""
L{DirectoryService.unIndexedRecordsFromMatchExpression} with a
startsWith expression.
"""
return self._test_unIndexedRecordsFromMatchExpression(
(
(u"Wilfredo", (u"__wsanchez__",)), # Duplicates
(u"A", (u"__alyssa__", u"__dre__")), # Multiple
(u"Andre", (u"__dre__",)), # Single
),
MatchType.startsWith
)
def test_unIndexedRecordsFromMatchExpression_contains(self):
"""
L{DirectoryService.unIndexedRecordsFromMatchExpression} with a contains
expression.
"""
return self._test_unIndexedRecordsFromMatchExpression(
(
(u"Sanchez", (u"__wsanchez__",)), # Duplicates
(u"A", (u"__alyssa__", u"__dre__")), # Multiple
(u"LaBra", (u"__dre__",)), # Single
),
MatchType.contains
)
def test_unIndexedRecordsFromMatchExpression_equals(self):
"""
L{DirectoryService.unIndexedRecordsFromMatchExpression} with an equals
expression.
"""
return self._test_unIndexedRecordsFromMatchExpression(
(
(u"Wilfredo Sanchez", (u"__wsanchez__",)), # MultiValue
(u"Andre LaBranche", (u"__dre__",)), # Single value
),
MatchType.equals
)
def test_unIndexedRecordsFromMatchExpression_indexed(self):
"""
L{DirectoryService.unIndexedRecordsFromMatchExpression} with an
indexed field name.
"""
self._test_unIndexedRecordsFromMatchExpression(
(
(u"wsanchez", (u"__wsanchez__",)),
),
MatchType.equals,
fieldName=BaseFieldName.shortNames
)
def test_unIndexedRecordsFromMatchExpression_notMatchExpression(self):
"""
L{DirectoryService.unIndexedRecordsFromMatchExpression} with a
non-match expression.
"""
result = self._test_unIndexedRecordsFromMatchExpression(
(
(u"zehcnasw", (u"__wsanchez__",)),
),
"Not a match type we know about"
)
return self.assertFailure(result, NotImplementedError)
@inlineCallbacks
def test_unIndexedRecordsFromMatchExpression_limitResults(self):
"""
Make sure limitResults does limit results.
"""
service = self.noLoadServicePopulated()
records = yield service.unIndexedRecordsFromMatchExpression(
MatchExpression(
BaseFieldName.fullNames, u"A",
MatchType.startsWith
),
recordTypes=None,
limitResults=1
)
self.assertEquals(len(records), 1)
records = yield service.unIndexedRecordsFromMatchExpression(
MatchExpression(
BaseFieldName.fullNames, u"A",
MatchType.startsWith
),
recordTypes=None,
limitResults=1000
)
self.assertEquals(len(records), 2)
@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} with a
L{MatchExpression} for an indexed field calls
L{DirectoryRecord.indexedRecordsFromMatchExpression}.
"""
service = yield self._test_recordsFromNonCompoundExpression(
MatchExpression(BaseFieldName.shortNames, u"...")
)
self.assertTrue(getattr(service, "_calledIndexed", False))
self.assertFalse(getattr(service, "_calledUnindexed", False))
@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())
return self.assertFailure(result, QueryNotSupportedError)
class DirectoryServiceTest(unittest.TestCase, BaseDirectoryServiceTest):
"""
Tests for L{DirectoryService}.
"""
serviceClass = TestDirectoryService
directoryRecordClass = DirectoryRecord
def test_init_noIndex(self):
"""
Index starts as C{None}.
"""
service = self.service()
self.assertTrue(emptyIndex(service._index))
def test_index_get(self):
"""
Getting the C{index} property calls C{loadRecords}.
"""
class TestService(self.serviceClass):
loaded = False
def loadRecords(self):
self.loaded = True
service = TestService(u"")
service.index
self.assertTrue(service.loaded)
def test_loadRecords(self):
"""
L{DirectoryService.loadRecords} raises C{NotImplementedError}.
"""
service = self.service()
self.assertRaises(NotImplementedError, service.loadRecords)
def _noop(self):
"""
Does nothing.
"""
test_recordWithUID = _noop
test_recordWithGUID = _noop
test_recordsWithRecordType = _noop
test_recordWithShortName = _noop
test_recordsWithEmailAddress = _noop
class BaseDirectoryServiceImmutableTest(
test_directory.BaseDirectoryServiceImmutableTest
):
"""
Tests for immutable indexed directory services.
"""
class DirectoryServiceImmutableTest(
unittest.TestCase, BaseDirectoryServiceImmutableTest
):
"""
Tests for immutable L{DirectoryService}.
"""
serviceClass = TestDirectoryService
directoryRecordClass = DirectoryRecord
class BaseDirectoryRecordTest(test_directory.BaseDirectoryRecordTest):
"""
Tests for indexed directory records.
"""
class DirectoryRecordTest(unittest.TestCase, BaseDirectoryRecordTest):
"""
Tests for L{DirectoryRecord}.
"""
serviceClass = TestDirectoryService
directoryRecordClass = DirectoryRecord
def _noop(self):
"""
Does nothing.
"""
test_members_group = _noop
test_memberships = _noop
def emptyIndex(index):
"""
Determine whether an index is empty.
@param index: An index.
@type index: L{dict}
@return: true if C{index} is empty, otherwise false.
"""
if not index:
return True
for _ignore_fieldName, fieldIndex in index.iteritems():
for _ignore_fieldValue, records in fieldIndex.iteritems():
for _ignore_record in records:
return False
return True
|