/
tests.py
114 lines (87 loc) · 3.52 KB
/
tests.py
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
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# 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.
#
##############################################################################
"""TopicIndex unit tests.
"""
import unittest
from Products.PluginIndexes.TopicIndex.TopicIndex import TopicIndex
class Obj(object):
def __init__(self, id, meta_type=''):
self.id = id
self.meta_type = meta_type
def getId(self):
return self.id
def getPhysicalPath(self):
return self.id
class TestBase(unittest.TestCase):
def _searchAnd(self, query, expected):
return self._search(query, 'and', expected)
def _searchOr(self, query, expected):
return self._search(query, 'or', expected)
def _search(self, query, operator, expected):
res = self.TI._apply_index(
{'topic': {'query': query, 'operator': operator}})
rows = list(res[0].keys())
rows.sort()
expected.sort()
self.assertEqual(rows, expected, query)
return rows
class TestTopicIndex(TestBase):
def setUp(self):
self.TI = TopicIndex("topic")
self.TI.addFilteredSet("doc1", "PythonFilteredSet",
"o.meta_type=='doc1'")
self.TI.addFilteredSet("doc2", "PythonFilteredSet",
"o.meta_type=='doc2'")
self.TI.index_object(0, Obj('0', ))
self.TI.index_object(1, Obj('1', 'doc1'))
self.TI.index_object(2, Obj('2', 'doc1'))
self.TI.index_object(3, Obj('3', 'doc2'))
self.TI.index_object(4, Obj('4', 'doc2'))
self.TI.index_object(5, Obj('5', 'doc3'))
self.TI.index_object(6, Obj('6', 'doc3'))
def test_interfaces(self):
from Products.PluginIndexes.interfaces import ITopicIndex
from Products.PluginIndexes.interfaces import IPluggableIndex
from zope.interface.verify import verifyClass
verifyClass(ITopicIndex, TopicIndex)
verifyClass(IPluggableIndex, TopicIndex)
def testOr(self):
self._searchOr('doc1', [1, 2])
self._searchOr(['doc1'], [1, 2])
self._searchOr('doc2', [3, 4]),
self._searchOr(['doc2'], [3, 4])
self._searchOr(['doc1', 'doc2'], [1, 2, 3, 4])
def testAnd(self):
self._searchAnd('doc1', [1, 2])
self._searchAnd(['doc1'], [1, 2])
self._searchAnd('doc2', [3, 4])
self._searchAnd(['doc2'], [3, 4])
self._searchAnd(['doc1', 'doc2'], [])
def testRemoval(self):
self.TI.index_object(1, Obj('1', 'doc2'))
self._searchOr('doc1', [2])
self._searchOr('doc2', [1, 3, 4])
def test_getCounter(self):
index = self.TI
index._counter.set(0)
self.assertEqual(index.getCounter(), 0)
index.index_object(1, Obj(1, 'doc1'))
self.assertEqual(index.getCounter(), 1)
index.unindex_object(1)
self.assertEqual(index.getCounter(), 2)
# unknown id
index.unindex_object(1)
self.assertEqual(index.getCounter(), 2)
# clear changes the index
index.clear()
self.assertEqual(index.getCounter(), 3)