/usr/share/pyshared/zope/index/topic/tests/test_index.py is in python-zope.index 3.6.4-0ubuntu1.
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 | ##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Topic Index tests
"""
import unittest
_marker = object()
class TopicIndexTest(unittest.TestCase):
def _getTargetClass(self):
from zope.index.topic.index import TopicIndex
return TopicIndex
def _get_family(self):
import BTrees
return BTrees.family32
def _makeOne(self, family=_marker):
if family is _marker:
family = self._get_family()
if family is None:
return self._getTargetClass()()
return self._getTargetClass()(family)
def _search(self, index, query, expected, operator='and'):
result = index.search(query, operator)
self.assertEqual(result.keys(), expected)
def _search_or(self, index, query, expected):
return self._search(index, query, expected, 'or')
def _search_and(self, index, query, expected):
return self._search(index, query, expected, 'and')
def _apply(self, index, query, expected, operator='and'):
result = index.apply(query)
self.assertEqual(result.keys(), expected)
def _apply_or(self, index, query, expected):
result = index.apply({'query': query, 'operator': 'or'})
self.assertEqual(result.keys(), expected)
def _apply_and(self, index, query, expected):
result = index.apply({'query': query, 'operator': 'and'})
self.assertEqual(result.keys(), expected)
def test_class_conforms_to_IInjection(self):
from zope.interface.verify import verifyClass
from zope.index.interfaces import IInjection
verifyClass(IInjection, self._getTargetClass())
def test_instance_conforms_to_IInjection(self):
from zope.interface.verify import verifyObject
from zope.index.interfaces import IInjection
verifyObject(IInjection, self._makeOne())
def test_class_conforms_to_IIndexSearch(self):
from zope.interface.verify import verifyClass
from zope.index.interfaces import IIndexSearch
verifyClass(IIndexSearch, self._getTargetClass())
def test_instance_conforms_to_IIndexSearch(self):
from zope.interface.verify import verifyObject
from zope.index.interfaces import IIndexSearch
verifyObject(IIndexSearch, self._makeOne())
def test_class_conforms_to_ITopicQuerying(self):
from zope.interface.verify import verifyClass
from zope.index.topic.interfaces import ITopicQuerying
verifyClass(ITopicQuerying, self._getTargetClass())
def test_instance_conforms_to_ITopicQuerying(self):
from zope.interface.verify import verifyObject
from zope.index.topic.interfaces import ITopicQuerying
verifyObject(ITopicQuerying, self._makeOne())
def test_ctor_defaults(self):
import BTrees
index = self._makeOne(family=None)
self.failUnless(index.family is BTrees.family32)
def test_ctor_explicit_family(self):
import BTrees
index = self._makeOne(family=BTrees.family64)
self.failUnless(index.family is BTrees.family64)
def test_clear_erases_filters(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
index.clear()
self.assertEqual(list(index._filters), [])
def test_addFilter(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
self.assertEqual(list(index._filters), ['foo'])
self.failUnless(index._filters['foo'] is foo)
def test_addFilter_duplicate_replaces(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
foo2 = DummyFilter('foo')
index.addFilter(foo2)
self.assertEqual(list(index._filters), ['foo'])
self.failUnless(index._filters['foo'] is foo2)
def test_delFilter_nonesuch_raises_KeyError(self):
index = self._makeOne()
self.assertRaises(KeyError, index.delFilter, 'nonesuch')
def test_delFilter(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
bar = DummyFilter('bar')
index.addFilter(bar)
index.delFilter('foo')
self.assertEqual(list(index._filters), ['bar'])
self.failUnless(index._filters['bar'] is bar)
def test_clearFilters_empty(self):
index = self._makeOne()
index.clearFilters() # doesn't raise
def test_clearFilters_non_empty(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
bar = DummyFilter('bar')
index.addFilter(bar)
index.clearFilters()
self.failUnless(foo._cleared)
self.failUnless(bar._cleared)
def test_index_doc(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
bar = DummyFilter('bar')
index.addFilter(bar)
obj = object()
index.index_doc(1, obj)
self.assertEqual(foo._indexed, [(1, obj)])
self.assertEqual(bar._indexed, [(1, obj)])
def test_unindex_doc(self):
index = self._makeOne()
foo = DummyFilter('foo')
index.addFilter(foo)
bar = DummyFilter('bar')
index.addFilter(bar)
index.unindex_doc(1)
self.assertEqual(foo._unindexed, [1])
self.assertEqual(bar._unindexed, [1])
def test_search_non_tuple_list_query(self):
index = self._makeOne()
self.assertRaises(TypeError, index.search, {'nonesuch': 'ugh'})
def test_search_bad_operator(self):
index = self._makeOne()
self.assertRaises(TypeError, index.search, ['whatever'], 'maybe')
def test_search_no_filters_list_query(self):
index = self._makeOne()
result = index.search(['nonesuch'])
self.assertEqual(set(result), set())
def test_search_no_filters_tuple_query(self):
index = self._makeOne()
result = index.search(('nonesuch',))
self.assertEqual(set(result), set())
def test_search_no_filters_string_query(self):
index = self._makeOne()
result = index.search('nonesuch')
self.assertEqual(set(result), set())
def test_search_query_matches_one_filter(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.search(['foo'])
self.assertEqual(set(result), set([1, 2, 3]))
def test_search_query_matches_multiple_implicit_operator(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.search(['foo', 'bar'])
self.assertEqual(set(result), set([2, 3]))
def test_search_query_matches_multiple_implicit_op_no_intersect(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [4, 5, 6], self._get_family())
index.addFilter(bar)
result = index.search(['foo', 'bar'])
self.assertEqual(set(result), set())
def test_search_query_matches_multiple_explicit_and(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.search(['foo', 'bar'], operator='and')
self.assertEqual(set(result), set([2, 3]))
def test_search_query_matches_multiple_explicit_or(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.search(['foo', 'bar'], operator='or')
self.assertEqual(set(result), set([1, 2, 3, 4]))
def test_apply_query_matches_multiple_non_dict_query(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.apply(['foo', 'bar'])
self.assertEqual(set(result), set([2, 3]))
def test_apply_query_matches_multiple_implicit_op(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.apply({'query': ['foo', 'bar']})
self.assertEqual(set(result), set([2, 3]))
def test_apply_query_matches_multiple_explicit_and(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.apply({'query': ['foo', 'bar'], 'operator': 'and'})
self.assertEqual(set(result), set([2, 3]))
def test_apply_query_matches_multiple_explicit_or(self):
index = self._makeOne()
foo = DummyFilter('foo', [1, 2, 3], self._get_family())
index.addFilter(foo)
bar = DummyFilter('bar', [2, 3, 4], self._get_family())
index.addFilter(bar)
result = index.apply({'query': ['foo', 'bar'], 'operator': 'or'})
self.assertEqual(set(result), set([1, 2, 3, 4]))
class _NotYet:
def _addFilters(self, index):
from zope.index.topic.filter import PythonFilteredSet
index.addFilter(
PythonFilteredSet('doc1', "context.meta_type == 'doc1'",
index.family))
index.addFilter(
PythonFilteredSet('doc2', "context.meta_type == 'doc2'",
index.family))
index.addFilter(
PythonFilteredSet('doc3', "context.meta_type == 'doc3'",
index.family))
def _populate(self, index):
class O(object):
""" a dummy class """
def __init__(self, meta_type):
self.meta_type = meta_type
index.index_doc(0 , O('doc0'))
index.index_doc(1 , O('doc1'))
index.index_doc(2 , O('doc1'))
index.index_doc(3 , O('doc2'))
index.index_doc(4 , O('doc2'))
index.index_doc(5 , O('doc3'))
index.index_doc(6 , O('doc3'))
def test_unindex(self):
index = self._makeOne()
index.unindex_doc(-99) # should not raise
index.unindex_doc(3)
index.unindex_doc(4)
index.unindex_doc(5)
self._search_or(index, 'doc1', [1,2])
self._search_or(index, 'doc2', [])
self._search_or(index, 'doc3', [6])
self._search_or(index, 'doc4', [])
def test_or(self):
index = self._makeOne()
self._search_or(index, 'doc1', [1,2])
self._search_or(index, ['doc1'],[1,2])
self._search_or(index, 'doc2', [3,4]),
self._search_or(index, ['doc2'],[3,4])
self._search_or(index, ['doc1','doc2'], [1,2,3,4])
def test_and(self):
index = self._makeOne()
self._search_and(index, 'doc1', [1,2])
self._search_and(index, ['doc1'], [1,2])
self._search_and(index, 'doc2', [3,4])
self._search_and(index, ['doc2'], [3,4])
self._search_and(index, ['doc1','doc2'], [])
def test_apply_or(self):
index = self._makeOne()
self._apply_or(index, 'doc1', [1,2])
self._apply_or(index, ['doc1'],[1,2])
self._apply_or(index, 'doc2', [3,4]),
self._apply_or(index, ['doc2'],[3,4])
self._apply_or(index, ['doc1','doc2'], [1,2,3,4])
def test_apply_and(self):
index = self._makeOne()
self._apply_and(index, 'doc1', [1,2])
self._apply_and(index, ['doc1'], [1,2])
self._apply_and(index, 'doc2', [3,4])
self._apply_and(index, ['doc2'], [3,4])
self._apply_and(index, ['doc1','doc2'], [])
def test_apply(self):
index = self._makeOne()
self._apply(index, 'doc1', [1,2])
self._apply(index, ['doc1'], [1,2])
self._apply(index, 'doc2', [3,4])
self._apply(index, ['doc2'], [3,4])
self._apply(index, ['doc1','doc2'], [])
class TopicIndexTest64(TopicIndexTest):
def _get_family(self):
import BTrees
return BTrees.family64
class DummyFilter:
_cleared = False
def __init__(self, id, ids=(), family=None):
self._id = id
self._indexed = []
self._unindexed = []
self._family = family
self._ids = ids
def getId(self):
return self._id
def clear(self):
self._cleared = True
def index_doc(self, docid, obj):
self._indexed.append((docid, obj))
def unindex_doc(self, docid):
self._unindexed.append(docid)
def getIds(self):
if self._family is not None:
return self._family.IF.TreeSet(self._ids)
return set(self._ids)
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(TopicIndexTest),
unittest.makeSuite(TopicIndexTest64),
))
|