My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
# -*- coding: utf8 -*-

import sys
import unittest
import tempfile
import os
import shutil

from src.armado import compressed_index
from src.armado import easy_index


class TestIndex(unittest.TestCase):
'''Base class.'''

def setUp(self):
self.tempdir = tempfile.mkdtemp()

def tearDown(self):
shutil.rmtree(self.tempdir)

def create_index(self, info):
'''Creates an index and then opens it again to return it.'''
self.index.create(self.tempdir, info)
return self.index(self.tempdir)


class TestItems(TestIndex):
'''Test the .items method.'''

def test_nothing(self):
'''Nothing in the index.'''
idx = self.create_index([])
items = list(idx.items())
self.assertEqual(items, [])

def test_one_item(self):
'''Only one item.'''
idx = self.create_index([("a", 3)])
items = sorted(idx.items())
self.assertEqual(items, [("a", [3])])

def test_several_items(self):
'''Several items stored.'''
idx = self.create_index([("a", 3), ("b", 5)])
items = sorted(idx.items())
self.assertEqual(items, [("a", [3]), ("b", [5])])

def test_same_keys(self):
'''Two items with the same key.'''
idx = self.create_index([("a", 3), ("a", 5)])
items = sorted(idx.items())
self.assertEqual(items, [("a", [3, 5])])

def test_mixed(self):
'''Two items with the same key and something else.'''
idx = self.create_index([("a", 3), (u"ñ", 7), ("a", 5)])
items = sorted(idx.items())
self.assertEqual(items, [("a", [3, 5]), (u"ñ", [7])])


class TestValues(TestIndex):
'''Test the .values method.'''

def test_nothing(self):
'''Nothing in the index.'''
idx = self.create_index([])
values = list(idx.values())
self.assertEqual(values, [])

def test_one_item(self):
'''Only one item.'''
idx = self.create_index([("a", 3)])
values = sorted(idx.values())
self.assertEqual(values, [3])

def test_several_values(self):
'''Several values stored.'''
idx = self.create_index([("a", 3), ("b", 5)])
values = sorted(idx.values())
self.assertEqual(values, [3, 5])

def test_same_keys(self):
'''Two values with the same key.'''
idx = self.create_index([("a", 3), ("a", 5)])
values = sorted(idx.values())
self.assertEqual(values, [3, 5])

def test_mixed(self):
'''Two values with the same key and something else.'''
idx = self.create_index([(u"ñ", 3), ("b", 7), (u"ñ", 5)])
values = sorted(idx.values())
self.assertEqual(values, [3, 5, 7])


class TestRandom(TestIndex):
'''Test the .random method.'''

def test_one_item(self):
'''Only one item.'''
idx = self.create_index([("a", 3)])
value = idx.random()
self.assertEqual(value, 3)

def test_several_values(self):
'''Several values stored.'''
idx = self.create_index([("a", 3), ("b", 5)])
value = idx.random()
self.assertTrue(value in (3, 5))


class TestContains(TestIndex):
'''Test the "in" functionality.'''

def test_nothing(self):
'''Nothing in the index.'''
idx = self.create_index([])
self.assertFalse("a" in idx)

def test_one_item(self):
'''Only one item.'''
idx = self.create_index([("a", 3)])
self.assertTrue("a" in idx)
self.assertFalse("b" in idx)

def test_several_values(self):
'''Several values stored.'''
idx = self.create_index([("a", 3), (u"ñ", 5)])
self.assertTrue("a" in idx)
self.assertTrue(u"ñ" in idx)
self.assertFalse("c" in idx)


class TestSearch(TestIndex):
'''Test the .search method.'''

def test_nothing(self):
'''Nothing in the index.'''
idx = self.create_index([])
res = idx.search(["a"])
self.assertEqual(list(res), [])

def test_one_item(self):
'''Only one item.'''
idx = self.create_index([("a", 3)])
res = idx.search(["a"])
self.assertEqual(list(res), [3])
res = idx.search(["b"])
self.assertEqual(list(res), [])

def test_several_values(self):
'''Several values stored.'''
idx = self.create_index([("a", 3), ("b", 5)])
res = idx.search(["a"])
self.assertEqual(list(res), [3])
res = idx.search(["b"])
self.assertEqual(list(res), [5])
res = idx.search(["c"])
self.assertEqual(list(res), [])

def test_same_keys(self):
'''Two values with the same key.'''
idx = self.create_index([("a", 3), ("a", 5)])
res = idx.search(["a"])
self.assertEqual(list(res), [3, 5])
res = idx.search(["b"])
self.assertEqual(list(res), [])

def test_mixed(self):
'''Two values with the same key and something else.'''
idx = self.create_index([("a", 3), (u"ñ", 7), ("a", 5)])
res = idx.search(["a"])
self.assertEqual(list(res), [3, 5])
res = idx.search([u"ñ"])
self.assertEqual(list(res), [7])
res = idx.search(["c"])
self.assertEqual(list(res), [])

def test_nopartial(self):
'''Does not find partial values.'''
idx = self.create_index([("aa", 3)])
res = idx.search(["a"])
self.assertEqual(list(res), [])

def test_and(self):
'''Check that AND is applied.'''
idx = self.create_index([("a", 3), ("b", 3), ("a", 5), ("c", 5)])
res = idx.search(["a", "b"])
self.assertEqual(list(res), [3])
res = idx.search(["b", "c"])
self.assertEqual(list(res), [])


class TestPartialSearch(TestIndex):
'''Test the .partial_search method.'''

def test_nothing(self):
'''Nothing in the index.'''
idx = self.create_index([])
res = idx.partial_search(["a"])
self.assertEqual(list(res), [])

def test_prefix(self):
'''Match its prefix.'''
idx = self.create_index([(u"abñc", 3)])
res = idx.partial_search(["ab"])
self.assertEqual(list(res), [3])
res = idx.partial_search(["ad"])
self.assertEqual(list(res), [])

def test_suffix(self):
'''Match its suffix.'''
idx = self.create_index([(u"abñd", 3)])
res = idx.partial_search([u"ñd"])
self.assertEqual(list(res), [3])
res = idx.partial_search(["ad"])
self.assertEqual(list(res), [])

def test_middle(self):
'''Match in the middle.'''
idx = self.create_index([(u"abñd", 3)])
res = idx.partial_search([u"bñ"])
self.assertEqual(list(res), [3])
res = idx.partial_search(["cb"])
self.assertEqual(list(res), [])

def test_exact(self):
'''Exact match.'''
idx = self.create_index([("abcd", 3)])
res = idx.partial_search(["abcd"])
self.assertEqual(list(res), [3])

def test_several_values(self):
'''Several values stored.'''
idx = self.create_index([("aa", 3), ("bc", 5), ("dbj", 7), ("ab", 9)])
res = idx.partial_search(["a"])
self.assertEqual(list(res), [3, 9])
res = idx.partial_search(["b"])
self.assertEqual(list(res), [5, 7, 9])
res = idx.partial_search(["c"])
self.assertEqual(list(res), [5])
res = idx.partial_search(["d"])
self.assertEqual(list(res), [7])

def test_and(self):
'''Check that AND is applied.'''
idx = self.create_index([("oao", 3), ("bll", 3),
("nga", 5), ("xxc", 5), ("ooa", 7)])
res = idx.partial_search(["a", "b"])
self.assertEqual(list(res), [3])
res = idx.partial_search(["b", "c"])
self.assertEqual(list(res), [])
res = idx.partial_search(["a", "o"])
self.assertEqual(list(res), [3, 7])


class TestCreate(TestIndex):
'''Test the .create method in the non-working cases.'''

def test_non_iterable(self):
'''It must iterate on what receives.'''
self.assertRaises(TypeError, self.index.create, self.tempdir, None)

def test_key_string(self):
'''Keys can be string.'''
self.index.create(self.tempdir, [("aa", 33)])

def test_key_unicode(self):
'''Keys can be unicode.'''
self.index.create(self.tempdir, [(u"año", 33)])

def test_key_badtype(self):
'''Keys must be strings or unicode.'''
self.assertRaises(TypeError, self.index.create, self.tempdir, [(1, 3)])

def test_return_quantity(self):
'''Must return the quantity indexed.'''
q = self.index.create(self.tempdir, [])
self.assertEqual(q, 0)
q = self.index.create(self.tempdir, [("a", 1)])
self.assertEqual(q, 1)
q = self.index.create(self.tempdir, [("a", 1), ("b", 2)])
self.assertEqual(q, 2)
q = self.index.create(self.tempdir, [("a", 1), ("a", 2)])
self.assertEqual(q, 2)

_testcases = [TestIndex, TestItems, TestValues, TestRandom, TestContains,
TestSearch, TestPartialSearch, TestCreate]

# Para correr los tests de los distintos indices (compressed y easy) y no duplicar
# las clases de los tests, generamos las clases dinamicamente con la funcion/metaclase
# indexed_testcase

def indexed_testcase(testcase, name, index):
new_testcase = type(testcase.__name__ + name, (testcase, ), {})
new_testcase.index = index
return new_testcase

_compressed_testcases = [indexed_testcase(testcase, "Compressed", \
compressed_index.Index) \
for testcase in _testcases]
_easy_testcases = [indexed_testcase(testcase, "Easy", easy_index.Index) \
for testcase in _testcases]

_all_testcases = _compressed_testcases + _easy_testcases

def suite():
suite = unittest.TestSuite()
for test_case in _all_testcases:
suite.addTest(unittest.makeSuite(test_case))
return suite

def load_tests(loader, tests, pattern):
return suite()

if __name__ == '__main__':
unittest.main(defaultTest="suite")

Change log

r414 by gringotumadre on Dec 11, 2011   Diff
Fixed unittest discovery support of
index_tests
Go to: 
Project members, sign in to write a code review

Older revisions

r374 by gringotumadre on Jul 17, 2011   Diff
Ahora se testea tanto el
compressed_index como el easy_index
automaticamente.
r151 by facundobatista on Nov 14, 2009   Diff
Easy index con el test que lo valida.

All revisions of this file

File info

Size: 10255 bytes, 317 lines
Powered by Google Project Hosting